initial commit
This commit is contained in:
26
CodeReviewApp/CodeReviewApp.csproj
Normal file
26
CodeReviewApp/CodeReviewApp.csproj
Normal file
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.14"/>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DAL\DAL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
14
CodeReviewApp/ConfiguringManager/Configurations.cs
Normal file
14
CodeReviewApp/ConfiguringManager/Configurations.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace CodeReviewApp.ConfiguringManager;
|
||||
|
||||
public static class Configurations
|
||||
{
|
||||
|
||||
public static DatabaseSettings Database { get; set; } = new DatabaseSettings();
|
||||
public static RedisConfiguration Redis { get; set; } = new RedisConfiguration();
|
||||
|
||||
public static void SetProperties(IConfiguration configuration)
|
||||
{
|
||||
Database = configuration.GetSection("Database").Get<DatabaseSettings>();
|
||||
Redis = configuration.GetSection("Redis").Get<RedisConfiguration>();
|
||||
}
|
||||
}
|
||||
7
CodeReviewApp/ConfiguringManager/DatabaseSettings.cs
Normal file
7
CodeReviewApp/ConfiguringManager/DatabaseSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace CodeReviewApp.ConfiguringManager;
|
||||
|
||||
public class DatabaseSettings
|
||||
{
|
||||
public string ConnectionString { get; set; } = default!;
|
||||
public int TimeOut { get; set; }
|
||||
}
|
||||
7
CodeReviewApp/ConfiguringManager/RedisConfiguration.cs
Normal file
7
CodeReviewApp/ConfiguringManager/RedisConfiguration.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace CodeReviewApp.ConfiguringManager;
|
||||
|
||||
public class RedisConfiguration
|
||||
{
|
||||
public string Connection { get; set; } = default!;
|
||||
public int CacheLifeTime { get; set; }
|
||||
}
|
||||
129
CodeReviewApp/Controllers/ProductController.cs
Normal file
129
CodeReviewApp/Controllers/ProductController.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using AutoMapper;
|
||||
using CodeReviewApp.ConfiguringManager;
|
||||
using CodeReviewApp.Models;
|
||||
using DAL.Repositories;
|
||||
using Domain.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace CodeReviewApp.Controllers;
|
||||
|
||||
public class ProductController: ControllerBase
|
||||
{
|
||||
private readonly IProductRepository _productRepository;
|
||||
private readonly ILogger<ProductController> _logger;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ProductController(IProductRepository productRepository, ILogger<ProductController> logger, IMapper mapper)
|
||||
{
|
||||
_productRepository = productRepository;
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll()
|
||||
{
|
||||
_logger.LogInformation($"Api method {Request.Path.Value} was started");
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
var token = cts.Token;
|
||||
|
||||
return Ok(await _productRepository.GetAll(
|
||||
Configurations.Redis.CacheLifeTime, token));
|
||||
}
|
||||
|
||||
[HttpPost("add")]
|
||||
public async Task<IActionResult> Add([FromBody] ProductRequest productRequest)
|
||||
{
|
||||
_logger.LogInformation($"Api method {Request.Path.Value} was started");
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
_logger.LogWarning($"Api method {Request.Path.Value}, {ModelState}");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Configurations.Database.TimeOut));
|
||||
var token = cts.Token;
|
||||
|
||||
var entity = _mapper.Map<Product>(productRequest);
|
||||
|
||||
try
|
||||
{
|
||||
await _productRepository.Create(entity,
|
||||
Configurations.Redis.CacheLifeTime, token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public async Task<IActionResult> GetById(int id)
|
||||
{
|
||||
_logger.LogInformation($"Api method {Request.Path.Value} was started");
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Configurations.Database.TimeOut));
|
||||
var token = cts.Token;
|
||||
|
||||
return Ok(await _productRepository.Get(id, Configurations.Redis.CacheLifeTime, token));
|
||||
}
|
||||
|
||||
[HttpPost("delete/{id:int}")]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
_logger.LogInformation($"Api method {Request.Path.Value} was started");
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Configurations.Database.TimeOut));
|
||||
var token = cts.Token;
|
||||
|
||||
try
|
||||
{
|
||||
await _productRepository.Delete(id, token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("update")]
|
||||
public async Task<IActionResult> Update([FromBody] Product productRequest)
|
||||
{
|
||||
_logger.LogInformation($"Api method {Request.Path.Value} was started");
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
_logger.LogWarning($"Api method {Request.Path.Value}, {ModelState}");
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Configurations.Database.TimeOut));
|
||||
var token = cts.Token;
|
||||
|
||||
try
|
||||
{
|
||||
await _productRepository.Update(productRequest,
|
||||
Configurations.Redis.CacheLifeTime, token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
22
CodeReviewApp/Dockerfile
Normal file
22
CodeReviewApp/Dockerfile
Normal file
@@ -0,0 +1,22 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
EXPOSE 443
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["CodeReviewApp/CodeReviewApp.csproj", "CodeReviewApp/"]
|
||||
RUN dotnet restore "CodeReviewApp/CodeReviewApp.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/CodeReviewApp"
|
||||
RUN dotnet build "CodeReviewApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "CodeReviewApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "CodeReviewApp.dll"]
|
||||
16
CodeReviewApp/Models/ProductRequest.cs
Normal file
16
CodeReviewApp/Models/ProductRequest.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace CodeReviewApp.Models;
|
||||
|
||||
public class ProductRequest
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Code { get; set; }
|
||||
public Description Description { get; set; }
|
||||
}
|
||||
|
||||
public class Description
|
||||
{
|
||||
public int Price { get; set; }
|
||||
public string Currency { get; set; }
|
||||
public string Metadata { get; set; }
|
||||
public int ProductId { get; set; }
|
||||
}
|
||||
49
CodeReviewApp/Program.cs
Normal file
49
CodeReviewApp/Program.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using CodeReviewApp.ConfiguringManager;
|
||||
using DAL.Database;
|
||||
using DAL.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
Configurations.SetProperties(config);
|
||||
|
||||
builder.Services.AddStackExchangeRedisCache(options =>
|
||||
{
|
||||
options.Configuration = Configurations.Redis.Connection;
|
||||
});
|
||||
|
||||
builder.Services.AddDbContext<DatabaseContext>(
|
||||
options => options.UseNpgsql(Configurations.Database.ConnectionString)
|
||||
);
|
||||
|
||||
builder.Services.AddAutoMapper(typeof(Program).Assembly);
|
||||
|
||||
builder.Services.AddScoped<IProductRepository, ProductRepository>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
41
CodeReviewApp/Properties/launchSettings.json
Normal file
41
CodeReviewApp/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:35916",
|
||||
"sslPort": 44301
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5085",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7081;http://localhost:5085",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
CodeReviewApp/appsettings.Development.json
Normal file
8
CodeReviewApp/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
CodeReviewApp/appsettings.json
Normal file
9
CodeReviewApp/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user