initial commit
This commit is contained in:
25
.dockerignore
Normal file
25
.dockerignore
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
**/.dockerignore
|
||||||
|
**/.env
|
||||||
|
**/.git
|
||||||
|
**/.gitignore
|
||||||
|
**/.project
|
||||||
|
**/.settings
|
||||||
|
**/.toolstarget
|
||||||
|
**/.vs
|
||||||
|
**/.vscode
|
||||||
|
**/.idea
|
||||||
|
**/*.*proj.user
|
||||||
|
**/*.dbmdl
|
||||||
|
**/*.jfm
|
||||||
|
**/azds.yaml
|
||||||
|
**/bin
|
||||||
|
**/charts
|
||||||
|
**/docker-compose*
|
||||||
|
**/Dockerfile*
|
||||||
|
**/node_modules
|
||||||
|
**/npm-debug.log
|
||||||
|
**/obj
|
||||||
|
**/secrets.dev.yaml
|
||||||
|
**/values.dev.yaml
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
/packages/
|
||||||
|
riderModule.iml
|
||||||
|
/_ReSharper.Caches/
|
||||||
28
CodeReviewApp.sln
Normal file
28
CodeReviewApp.sln
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeReviewApp", "CodeReviewApp\CodeReviewApp.csproj", "{5DD91EDA-2CD0-494E-8EBA-2EBA26A636CC}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DAL", "DAL\DAL.csproj", "{1FE01AB7-A762-45A7-8B49-49C821DB5D99}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csproj", "{B9EB6FA6-7E87-4F6A-9B04-944DB2030D6D}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{5DD91EDA-2CD0-494E-8EBA-2EBA26A636CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{5DD91EDA-2CD0-494E-8EBA-2EBA26A636CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{5DD91EDA-2CD0-494E-8EBA-2EBA26A636CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{5DD91EDA-2CD0-494E-8EBA-2EBA26A636CC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{1FE01AB7-A762-45A7-8B49-49C821DB5D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{1FE01AB7-A762-45A7-8B49-49C821DB5D99}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{1FE01AB7-A762-45A7-8B49-49C821DB5D99}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{1FE01AB7-A762-45A7-8B49-49C821DB5D99}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B9EB6FA6-7E87-4F6A-9B04-944DB2030D6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B9EB6FA6-7E87-4F6A-9B04-944DB2030D6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B9EB6FA6-7E87-4F6A-9B04-944DB2030D6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B9EB6FA6-7E87-4F6A-9B04-944DB2030D6D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
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": "*"
|
||||||
|
}
|
||||||
15
DAL/Database/DatabaseContext.cs
Normal file
15
DAL/Database/DatabaseContext.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using Domain.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DAL.Database;
|
||||||
|
|
||||||
|
public class DatabaseContext: DbContext
|
||||||
|
{
|
||||||
|
public DbSet<Product> Products { get; set; }
|
||||||
|
public DbSet<Description> Descriptions { get; set; }
|
||||||
|
|
||||||
|
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
DAL/IRepository.cs
Normal file
10
DAL/IRepository.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace DAL;
|
||||||
|
|
||||||
|
public interface IRepository<T>
|
||||||
|
{
|
||||||
|
Task<T> Get(int id, int cacheLifetime, CancellationToken token);
|
||||||
|
Task<List<T>> GetAll(int cacheLifeTime, CancellationToken token);
|
||||||
|
Task Delete(int id, CancellationToken token);
|
||||||
|
Task Update(T entity, int cacheLifetime, CancellationToken token);
|
||||||
|
Task Create(T entity, int cacheLifeTime, CancellationToken token);
|
||||||
|
}
|
||||||
8
DAL/Repositories/IProductRepository.cs
Normal file
8
DAL/Repositories/IProductRepository.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using Domain.Models;
|
||||||
|
|
||||||
|
namespace DAL.Repositories;
|
||||||
|
|
||||||
|
public interface IProductRepository: IRepository<Product>
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
149
DAL/Repositories/ProductRepository.cs
Normal file
149
DAL/Repositories/ProductRepository.cs
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using DAL.Database;
|
||||||
|
using Domain.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace DAL.Repositories;
|
||||||
|
|
||||||
|
public class ProductRepository: IProductRepository
|
||||||
|
{
|
||||||
|
private readonly DatabaseContext _db;
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IDistributedCache _cache;
|
||||||
|
|
||||||
|
public ProductRepository(DatabaseContext db, ILogger logger, IDistributedCache cache)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
_logger = logger;
|
||||||
|
_cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Product> Get(int id, int cacheLifetime, CancellationToken token)
|
||||||
|
{
|
||||||
|
var productCache = await _cache.GetAsync(id.ToString(), token);
|
||||||
|
var product = new Product();
|
||||||
|
|
||||||
|
if(productCache is not null)
|
||||||
|
product = JsonSerializer.Deserialize<Product>(productCache);
|
||||||
|
|
||||||
|
if (product is not null)
|
||||||
|
return product;
|
||||||
|
|
||||||
|
product = await _db.Products.FindAsync(id) ??
|
||||||
|
throw new KeyNotFoundException("Product not found");
|
||||||
|
|
||||||
|
_db.Entry(product).Reference(p => p.Description).Load();
|
||||||
|
|
||||||
|
await _cache.SetStringAsync(
|
||||||
|
product.Id.ToString(),
|
||||||
|
JsonSerializer.Serialize(product),
|
||||||
|
new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(cacheLifetime)
|
||||||
|
}, token);
|
||||||
|
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<Product>> GetAll(int cacheLifeTime, CancellationToken token)
|
||||||
|
{
|
||||||
|
var productsCache = await _cache.GetAsync("Products", token);
|
||||||
|
var products = new List<Product>();
|
||||||
|
|
||||||
|
|
||||||
|
if (productsCache is not null)
|
||||||
|
products = JsonSerializer.Deserialize<List<Product>> (productsCache);
|
||||||
|
|
||||||
|
if(products is not null && products.Count() > 0)
|
||||||
|
return products;
|
||||||
|
|
||||||
|
products = await _db.Products
|
||||||
|
.Include(p => p.Description)
|
||||||
|
.ToListAsync(token)
|
||||||
|
?? throw new NullReferenceException("Database is empty");
|
||||||
|
|
||||||
|
await _cache.SetStringAsync("Products",
|
||||||
|
JsonSerializer.Serialize(products),
|
||||||
|
new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(cacheLifeTime)
|
||||||
|
}, token);
|
||||||
|
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Delete(int id, CancellationToken token)
|
||||||
|
{
|
||||||
|
var product = await _db.Products.FindAsync(id) ??
|
||||||
|
throw new KeyNotFoundException("Product not found");
|
||||||
|
|
||||||
|
_db.Products.Remove(product);
|
||||||
|
await _cache.RemoveAsync(id.ToString(), token);
|
||||||
|
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Update(Product entity, int cacheLifetime, CancellationToken token)
|
||||||
|
{
|
||||||
|
var product = await _db.Products.FindAsync(entity.Id) ??
|
||||||
|
throw new KeyNotFoundException("Product not found");
|
||||||
|
|
||||||
|
_db.Entry(product).Reference(p => p.Description).Load();
|
||||||
|
|
||||||
|
_db.Entry(product).CurrentValues.SetValues(entity);
|
||||||
|
_db.Entry(product.Description).CurrentValues.SetValues(entity.Description);
|
||||||
|
|
||||||
|
await _cache.RemoveAsync(product.Id.ToString(), token);
|
||||||
|
await _cache.SetStringAsync(product.Id.ToString(),
|
||||||
|
JsonSerializer.Serialize(entity),
|
||||||
|
new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(cacheLifetime)
|
||||||
|
}, token);
|
||||||
|
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task Create(Product entity, int cacheLifeTime, CancellationToken token)
|
||||||
|
{
|
||||||
|
using (var transaction = _db.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _db.Products.AddAsync(entity);
|
||||||
|
|
||||||
|
await _db.Descriptions.AddAsync(entity.Description);
|
||||||
|
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
|
||||||
|
var products = await _db.Products
|
||||||
|
.Include(p => p.Description)
|
||||||
|
.ToListAsync()
|
||||||
|
?? throw new NullReferenceException("Database is empty");
|
||||||
|
|
||||||
|
await _cache.RemoveAsync("Products", token);
|
||||||
|
await _cache.SetStringAsync("Products",
|
||||||
|
JsonSerializer.Serialize(products),
|
||||||
|
new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(cacheLifeTime)
|
||||||
|
}, token);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex.Message);
|
||||||
|
transaction.Rollback();
|
||||||
|
|
||||||
|
throw new Exception(ex.Message);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
transaction.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Domain/Models/Description.cs
Normal file
10
Domain/Models/Description.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Domain.Models;
|
||||||
|
|
||||||
|
public class Description
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int Price { get; set; }
|
||||||
|
public string Currency { get; set; }
|
||||||
|
public string Metadata { get; set; }
|
||||||
|
public int ProductId { get; set; }
|
||||||
|
}
|
||||||
9
Domain/Models/Product.cs
Normal file
9
Domain/Models/Product.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Domain.Models;
|
||||||
|
|
||||||
|
public class Product
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Code { get; set; }
|
||||||
|
public Description Description { get; set; }
|
||||||
|
}
|
||||||
7
global.json
Normal file
7
global.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"sdk": {
|
||||||
|
"version": "7.0.0",
|
||||||
|
"rollForward": "latestMinor",
|
||||||
|
"allowPrerelease": false
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user