Skip to content

Commit

Permalink
Adding of new project
Browse files Browse the repository at this point in the history
  • Loading branch information
Clifftech123 committed Feb 13, 2025
1 parent ffd9613 commit a838b7c
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 0 deletions.
22 changes: 22 additions & 0 deletions DependencyInjection.API/DependencyInjection.API.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection.API", "DependencyInjection.API\DependencyInjection.API.csproj", "{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D4BBDF-9ACB-40C3-8560-0C4230A396C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DependencyInjection.API.Interface;
using Microsoft.AspNetCore.Mvc;

namespace DependencyInjection.API.Controllers
{
[ApiController]
[Route("[controller]")]
public class LifetimeDemoController : Controller
{
private readonly ITimestampService _timestampService;
private readonly IUserSessionService _userSessionService;
private readonly IGuidGeneratorService _guidGeneratorService;
private IRandomNumberService _randomNumberService;


public LifetimeDemoController(ITimestampService timestampService, IUserSessionService userSessionService, IGuidGeneratorService guidGeneratorService, IRandomNumberService randomNumberService)
{
_timestampService = timestampService;
_userSessionService = userSessionService;
_guidGeneratorService = guidGeneratorService;
_randomNumberService = randomNumberService;
}

[HttpGet]
public IActionResult Get()
{
return Ok(new
{
Timestamp = _timestampService.GetTimeStamp(),
Session = _userSessionService.GetSessionId(),
RandNumber = _randomNumberService.GetRandomNumber(),
Guid = _guidGeneratorService.GenerateGuid(),
}
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;

namespace DependencyInjection.API.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@DependencyInjection.API_HostAddress = http://localhost:5029

GET {{DependencyInjection.API_HostAddress}}/weatherforecast/
Accept: application/json

###
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DependencyInjection.API.Interface
{
public interface IGuidGeneratorService
{
Guid GenerateGuid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DependencyInjection.API.Interface
{
public interface IRandomNumberService
{
int GetRandomNumber();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DependencyInjection.API.Interface
{
public interface ITimestampService
{
DateTime GetTimeStamp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DependencyInjection.API.Interface
{
public interface IUserSessionService
{
string GetSessionId();
}
}
33 changes: 33 additions & 0 deletions DependencyInjection.API/DependencyInjection.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using DependencyInjection.API.Interface;
using DependencyInjection.API.Service;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

builder.Services.AddScoped<IRandomNumberService, RandomNumberService>();
builder.Services.AddScoped<ITimestampService, TimestampService>();
builder.Services.AddTransient<IUserSessionService, UserSessionService>();
builder.Services.AddTransient<IGuidGeneratorService, GuidGeneratorService>();



var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5029",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7029;http://localhost:5029",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using DependencyInjection.API.Interface;

namespace DependencyInjection.API.Service
{
public class GuidGeneratorService : IGuidGeneratorService
{

public Guid GenerateGuid() => Guid.NewGuid();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using DependencyInjection.API.Interface;

namespace DependencyInjection.API.Service
{
public class RandomNumberService : IRandomNumberService
{
private int _randomNumber;

public RandomNumberService()
{
_randomNumber = new Random().Next(1, 200) + 1;
}
public int GetRandomNumber()
{
return _randomNumber;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using DependencyInjection.API.Interface;

namespace DependencyInjection.API.Service
{
public class TimestampService : ITimestampService
{
private readonly DateTime _timestamp;
public TimestampService()
{

_timestamp = DateTime.UtcNow;

}

public DateTime GetTimeStamp() => _timestamp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DependencyInjection.API.Interface;

namespace DependencyInjection.API.Service
{
public class UserSessionService : IUserSessionService
{
private readonly string _userId;

public UserSessionService()
{
_userId = $"Session_{DateTime.UtcNow.Ticks}";
}
public string GetSessionId() => _userId;

}
}
13 changes: 13 additions & 0 deletions DependencyInjection.API/DependencyInjection.API/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DependencyInjection.API
{
public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit a838b7c

Please sign in to comment.