Add sample in mongo and sql layers
This commit is contained in:
parent
ca35645488
commit
10f2083590
@ -12,26 +12,26 @@ namespace Core.Blueprint.DAL.API.Controllers
|
||||
[Produces("application/json")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class MongoBlueprintController(IBlueprintService service) : ControllerBase
|
||||
public class MongoSampleController(IMongoSampleService service) : ControllerBase
|
||||
{
|
||||
[HttpPost("Create")]
|
||||
public async Task<IActionResult> CreateBlueprint([FromBody] BlueprintRequest entity, CancellationToken cancellationToken)
|
||||
public async Task<IActionResult> CreateSample([FromBody] SampleRequest entity, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await service.CreateBlueprint(entity, cancellationToken).ConfigureAwait(false);
|
||||
var result = await service.CreateSample(entity, cancellationToken).ConfigureAwait(false);
|
||||
return Created("CreatedWithIdAsync", result);
|
||||
}
|
||||
|
||||
[HttpGet("GetAll")]
|
||||
public async Task<IActionResult> GetEntities(CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await service.GetAllBlueprints(cancellationToken).ConfigureAwait(false);
|
||||
var result = await service.GetAllSamples(cancellationToken).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("{_id}/GetBy_Id")]
|
||||
public async Task<IActionResult> GetBlueprint([FromRoute] string _id, CancellationToken cancellationToken)
|
||||
public async Task<IActionResult> GetSample([FromRoute] string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await service.GetBlueprintById(_id, cancellationToken).ConfigureAwait(false);
|
||||
var result = await service.GetSampleById(_id, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
@ -42,22 +42,22 @@ namespace Core.Blueprint.DAL.API.Controllers
|
||||
}
|
||||
|
||||
[HttpPut("{_id}/Update")]
|
||||
public async Task<IActionResult> UpdateBlueprint([FromRoute] string _id, [FromBody] BlueprintCollection entity, CancellationToken cancellationToken)
|
||||
public async Task<IActionResult> UpdateSample([FromRoute] string _id, [FromBody] SampleCollection entity, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_id != entity._Id?.ToString())
|
||||
{
|
||||
return BadRequest("Blueprint ID mismatch");
|
||||
return BadRequest("Sample ID mismatch");
|
||||
}
|
||||
|
||||
var result = await service.UpdateBlueprint(_id, entity, cancellationToken).ConfigureAwait(false);
|
||||
var result = await service.UpdateSample(_id, entity, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{_id}/Delete")]
|
||||
public async Task<IActionResult> DeleteBlueprint([FromRoute] string _id, CancellationToken cancellationToken)
|
||||
public async Task<IActionResult> DeleteSample([FromRoute] string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await service.DeleteBlueprint(_id, cancellationToken).ConfigureAwait(false);
|
||||
var result = await service.DeleteSample(_id, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result is null) return NotFound();
|
||||
|
||||
@ -12,48 +12,48 @@ namespace Core.Blueprint.DAL.API.Controllers
|
||||
[Produces("application/json")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class UserProjectController(IUserProjectService service) : ControllerBase
|
||||
public class SqlSampleController(ISqlSampleService service) : ControllerBase
|
||||
{
|
||||
[HttpPost("Create")]
|
||||
public async Task<ActionResult<UserProject>> CreateEntity([FromBody] UserProjectRequest request)
|
||||
public async Task<ActionResult<Sample>> CreateEntity([FromBody] SampleRequest request)
|
||||
{
|
||||
var result = await service.AddUserProject(request).ConfigureAwait(false);
|
||||
var result = await service.AddSample(request).ConfigureAwait(false);
|
||||
return Created("CreatedWithIdAsync", result);
|
||||
}
|
||||
|
||||
[HttpGet("GetAll")]
|
||||
public async Task<ActionResult<IEnumerable<UserProject>>> GetEntities()
|
||||
public async Task<ActionResult<IEnumerable<Sample>>> GetEntities()
|
||||
{
|
||||
var result = await service.GetAllUserProjects().ConfigureAwait(false);
|
||||
var result = await service.GetAllSamples().ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/GetById")]
|
||||
public async Task<ActionResult<UserProject>> GetEntity(int id)
|
||||
public async Task<ActionResult<Sample>> GetEntity(int id)
|
||||
{
|
||||
var result = await service.GetUserProjectById(id).ConfigureAwait(false);
|
||||
var result = await service.GetSampleById(id).ConfigureAwait(false);
|
||||
|
||||
if (result is null) return NotFound("User Project not found");
|
||||
if (result is null) return NotFound("sample not found");
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpPut("{id}/Update")]
|
||||
public async Task<ActionResult<UserProject>> UpdateEntity(int id, UserProject entity)
|
||||
public async Task<ActionResult<Sample>> UpdateEntity(int id, Sample entity)
|
||||
{
|
||||
if (id != entity.Id)
|
||||
{
|
||||
return BadRequest("ID mismatch");
|
||||
}
|
||||
|
||||
var result = await service.UpdateUserProject(entity).ConfigureAwait(false);
|
||||
var result = await service.UpdateSample(entity).ConfigureAwait(false);
|
||||
return Ok(entity);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}/Delete")]
|
||||
public async Task<IActionResult> DeleteEntity(int id)
|
||||
{
|
||||
var result = await service.DeleteUserProject(id).ConfigureAwait(false);
|
||||
var result = await service.DeleteSample(id).ConfigureAwait(false);
|
||||
|
||||
if (result is null) return NotFound();
|
||||
|
||||
@ -17,12 +17,12 @@ namespace Core.Blueprint.DAL.API.Extensions
|
||||
public static IServiceCollection AddDALLayerServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
//Mongo
|
||||
services.AddScoped<IBlueprintService, BlueprintService>();
|
||||
services.AddScoped<CollectionRepository<BlueprintCollection>>();
|
||||
services.AddScoped<IMongoSampleService, MongoSampleService>();
|
||||
services.AddScoped<CollectionRepository<SampleCollection>>();
|
||||
|
||||
//SQL
|
||||
services.AddDbContext<SqlServerContext>(options => options.UseSqlServer(configuration.GetConnectionString("SQLServer")));
|
||||
services.AddScoped<IUserProjectService, UserProjectService>();
|
||||
services.AddScoped<ISqlSampleService, SqlSampleService>();
|
||||
|
||||
//Storage
|
||||
services.AddScoped<IBlobStorageService, BlobStorageService>();
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Collections;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Requests;
|
||||
|
||||
namespace Core.Blueprint.DAL.Mongo.Contracts
|
||||
{
|
||||
public interface IBlueprintService
|
||||
{
|
||||
ValueTask<BlueprintCollection> CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<BlueprintCollection> GetBlueprintById(string _id, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<IEnumerable<BlueprintCollection>> GetAllBlueprints(CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<BlueprintCollection> UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<BlueprintCollection> DeleteBlueprint(string _id, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
18
Core.Blueprint.DAL.Mongo/Contracts/IMongoSampleService.cs
Normal file
18
Core.Blueprint.DAL.Mongo/Contracts/IMongoSampleService.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Collections;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Requests;
|
||||
|
||||
namespace Core.Blueprint.DAL.Mongo.Contracts
|
||||
{
|
||||
public interface IMongoSampleService
|
||||
{
|
||||
ValueTask<SampleCollection> CreateSample(SampleRequest newSample, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<SampleCollection> GetSampleById(string _id, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<IEnumerable<SampleCollection>> GetAllSamples(CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<SampleCollection> UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken);
|
||||
|
||||
ValueTask<SampleCollection> DeleteSample(string _id, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
namespace Core.Blueprint.DAL.Mongo.Entities.Collections
|
||||
{
|
||||
[CollectionAttributeName("Blueprints")]
|
||||
public class BlueprintCollection : Document
|
||||
[CollectionAttributeName("Samples")]
|
||||
public class SampleCollection : Document
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
public string? Description { get; set; }
|
||||
@ -1,6 +1,6 @@
|
||||
namespace Core.Blueprint.DAL.Mongo.Entities.Requests
|
||||
{
|
||||
public class BlueprintRequest
|
||||
public class SampleRequest
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
@ -1,80 +0,0 @@
|
||||
using Core.Blueprint.DAL.Mongo.Contracts;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Collections;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Requests;
|
||||
using Core.Blueprint.Mongo;
|
||||
using Core.Blueprint.Redis;
|
||||
using Core.Blueprint.Redis.Helpers;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Core.Blueprint.DAL.Mongo.Service
|
||||
{
|
||||
public class BlueprintService : IBlueprintService
|
||||
{
|
||||
private readonly CollectionRepository<BlueprintCollection> repository;
|
||||
private readonly CacheSettings cacheSettings;
|
||||
private readonly IRedisCacheProvider cacheProvider;
|
||||
|
||||
public BlueprintService(CollectionRepository<BlueprintCollection> repository,
|
||||
IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.repository.CollectionInitialization();
|
||||
this.cacheSettings = cacheSettings.Value;
|
||||
this.cacheProvider = cacheProvider;
|
||||
}
|
||||
|
||||
public async ValueTask<BlueprintCollection> CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken)
|
||||
{
|
||||
var blueprintCollection = newBlueprint.Adapt<BlueprintCollection>();
|
||||
|
||||
await this.repository.InsertOneAsync(blueprintCollection);
|
||||
|
||||
return blueprintCollection;
|
||||
}
|
||||
|
||||
public async ValueTask<BlueprintCollection> GetBlueprintById(string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetBlueprintById", _id);
|
||||
var cachedData = await cacheProvider.GetAsync<BlueprintCollection>(cacheKey);
|
||||
|
||||
if (cachedData is not null) { return cachedData; }
|
||||
|
||||
var blueprint = await this.repository.FindByIdAsync(_id);
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, blueprint);
|
||||
|
||||
return blueprint;
|
||||
}
|
||||
|
||||
public async ValueTask<IEnumerable<BlueprintCollection>> GetAllBlueprints(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllBlueprints");
|
||||
var cachedData = await cacheProvider.GetAsync<IEnumerable<BlueprintCollection>>(cacheKey) ?? [];
|
||||
|
||||
if (cachedData.Any()) return cachedData;
|
||||
|
||||
var blueprints = await this.repository.AsQueryable();
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, blueprints);
|
||||
|
||||
return blueprints;
|
||||
}
|
||||
|
||||
public async ValueTask<BlueprintCollection> UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken)
|
||||
{
|
||||
await this.repository.ReplaceOneAsync(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async ValueTask<BlueprintCollection> DeleteBlueprint(string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
80
Core.Blueprint.DAL.Mongo/Service/MongoSampleService.cs
Normal file
80
Core.Blueprint.DAL.Mongo/Service/MongoSampleService.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using Core.Blueprint.DAL.Mongo.Contracts;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Collections;
|
||||
using Core.Blueprint.DAL.Mongo.Entities.Requests;
|
||||
using Core.Blueprint.Mongo;
|
||||
using Core.Blueprint.Redis;
|
||||
using Core.Blueprint.Redis.Helpers;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Core.Blueprint.DAL.Mongo.Service
|
||||
{
|
||||
public class MongoSampleService : IMongoSampleService
|
||||
{
|
||||
private readonly CollectionRepository<SampleCollection> repository;
|
||||
private readonly CacheSettings cacheSettings;
|
||||
private readonly IRedisCacheProvider cacheProvider;
|
||||
|
||||
public MongoSampleService(CollectionRepository<SampleCollection> repository,
|
||||
IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.repository.CollectionInitialization();
|
||||
this.cacheSettings = cacheSettings.Value;
|
||||
this.cacheProvider = cacheProvider;
|
||||
}
|
||||
|
||||
public async ValueTask<SampleCollection> CreateSample(SampleRequest newSample, CancellationToken cancellationToken)
|
||||
{
|
||||
var sampleCollection = newSample.Adapt<SampleCollection>();
|
||||
|
||||
await this.repository.InsertOneAsync(sampleCollection);
|
||||
|
||||
return sampleCollection;
|
||||
}
|
||||
|
||||
public async ValueTask<SampleCollection> GetSampleById(string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", _id);
|
||||
var cachedData = await cacheProvider.GetAsync<SampleCollection>(cacheKey);
|
||||
|
||||
if (cachedData is not null) { return cachedData; }
|
||||
|
||||
var sample = await this.repository.FindByIdAsync(_id);
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, sample);
|
||||
|
||||
return sample;
|
||||
}
|
||||
|
||||
public async ValueTask<IEnumerable<SampleCollection>> GetAllSamples(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples");
|
||||
var cachedData = await cacheProvider.GetAsync<IEnumerable<SampleCollection>>(cacheKey) ?? [];
|
||||
|
||||
if (cachedData.Any()) return cachedData;
|
||||
|
||||
var samples = await this.repository.AsQueryable();
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, samples);
|
||||
|
||||
return samples;
|
||||
}
|
||||
|
||||
public async ValueTask<SampleCollection> UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken)
|
||||
{
|
||||
await this.repository.ReplaceOneAsync(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public async ValueTask<SampleCollection> DeleteSample(string _id, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@ namespace Core.Blueprint.DAL.SQLServer.Context
|
||||
public sealed class SqlServerContext : DbContext
|
||||
{
|
||||
public SqlServerContext(DbContextOptions<SqlServerContext> options) : base(options) { }
|
||||
public DbSet<UserProject> UserProjects { get; set; }
|
||||
public DbSet<Sample> UserProjects { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
||||
19
Core.Blueprint.DAL.SQLServer/Contracts/ISqlSampleService.cs
Normal file
19
Core.Blueprint.DAL.SQLServer/Contracts/ISqlSampleService.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Core.Blueprint.DAL.SQLServer.Entities;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities.Request;
|
||||
|
||||
namespace Core.Blueprint.DAL.SQLServer.Contracts
|
||||
{
|
||||
public interface ISqlSampleService
|
||||
{
|
||||
Task<IEnumerable<Sample>> GetAllSamples();
|
||||
|
||||
Task<Sample?> GetSampleById(int id);
|
||||
|
||||
Task<Sample> AddSample(SampleRequest newSample);
|
||||
|
||||
Task<Sample?> UpdateSample(Sample sample);
|
||||
|
||||
Task<Sample?> DeleteSample(int id);
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
using Core.Blueprint.DAL.SQLServer.Entities;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities.Request;
|
||||
|
||||
namespace Core.Blueprint.DAL.SQLServer.Contracts
|
||||
{
|
||||
public interface IUserProjectService
|
||||
{
|
||||
Task<IEnumerable<UserProject>> GetAllUserProjects();
|
||||
|
||||
Task<UserProject?> GetUserProjectById(int id);
|
||||
|
||||
Task<UserProject> AddUserProject(UserProjectRequest newUserProject);
|
||||
|
||||
Task<UserProject?> UpdateUserProject(UserProject userProject);
|
||||
|
||||
Task<UserProject?> DeleteUserProject(int id);
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
namespace Core.Blueprint.DAL.SQLServer.Entities.Request
|
||||
{
|
||||
public class SampleRequest
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
public string Description { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
@ -1,9 +0,0 @@
|
||||
namespace Core.Blueprint.DAL.SQLServer.Entities.Request
|
||||
{
|
||||
public class UserProjectRequest
|
||||
{
|
||||
public string ProjectCode { get; set; } = null!;
|
||||
public string ProjectDescription { get; set; } = null!;
|
||||
public string UserId { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
12
Core.Blueprint.DAL.SQLServer/Entities/Sample.cs
Normal file
12
Core.Blueprint.DAL.SQLServer/Entities/Sample.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Core.Blueprint.SQLServer.Entities;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Core.Blueprint.DAL.SQLServer.Entities
|
||||
{
|
||||
[Table("Samples")]
|
||||
public class Sample : BaseSQLAdapter
|
||||
{
|
||||
public string Name { get; set; } = null!;
|
||||
public string Description { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
using Core.Blueprint.SQLServer.Entities;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Core.Blueprint.DAL.SQLServer.Entities
|
||||
{
|
||||
[Table("UserProjects")]
|
||||
public class UserProject : BaseSQLAdapter
|
||||
{
|
||||
public string ProjectCode { get; set; } = null!;
|
||||
public string ProjectDescription { get; set; } = null!;
|
||||
public string UserId { get; set; } = null!;
|
||||
}
|
||||
}
|
||||
82
Core.Blueprint.DAL.SQLServer/Services/SqlSampleService.cs
Normal file
82
Core.Blueprint.DAL.SQLServer/Services/SqlSampleService.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using Core.Blueprint.DAL.SQLServer;
|
||||
using Core.Blueprint.DAL.SQLServer.Context;
|
||||
using Core.Blueprint.DAL.SQLServer.Contracts;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities.Request;
|
||||
using Core.Blueprint.Redis;
|
||||
using Core.Blueprint.Redis.Helpers;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
public class SqlSampleService : ISqlSampleService
|
||||
{
|
||||
private readonly IEntityRepository<Sample, SqlServerContext> _sqlSampleRepository;
|
||||
private readonly CacheSettings cacheSettings;
|
||||
private readonly IRedisCacheProvider cacheProvider;
|
||||
|
||||
public SqlSampleService(IEntityRepository<Sample, SqlServerContext> sqlSampleRepository, IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
|
||||
{
|
||||
_sqlSampleRepository = sqlSampleRepository;
|
||||
this.cacheSettings = cacheSettings.Value;
|
||||
this.cacheProvider = cacheProvider;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Sample>> GetAllSamples()
|
||||
{
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples");
|
||||
var cachedData = await cacheProvider.GetAsync<IEnumerable<Sample>>(cacheKey) ?? [];
|
||||
|
||||
if (cachedData.Any()) return cachedData;
|
||||
|
||||
var samples = await _sqlSampleRepository.GetAllAsync();
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, samples);
|
||||
|
||||
return samples;
|
||||
}
|
||||
|
||||
public async Task<Sample?> GetSampleById(int id)
|
||||
{
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", id);
|
||||
var cachedData = await cacheProvider.GetAsync<Sample>(cacheKey);
|
||||
|
||||
if (cachedData is not null) { return cachedData; }
|
||||
|
||||
var sample = await _sqlSampleRepository.GetByIdAsync(id);
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, sample);
|
||||
|
||||
return sample;
|
||||
}
|
||||
|
||||
public async Task<Sample> AddSample(SampleRequest newSample)
|
||||
{
|
||||
var sample = newSample.Adapt<Sample>();
|
||||
|
||||
await _sqlSampleRepository.AddAsync(sample);
|
||||
await _sqlSampleRepository.SaveAsync();
|
||||
return sample;
|
||||
}
|
||||
|
||||
public async Task<Sample?> UpdateSample(Sample sample)
|
||||
{
|
||||
var existingEntity = await _sqlSampleRepository.GetByIdAsync(sample.Id);
|
||||
|
||||
if (existingEntity is null) return existingEntity;
|
||||
|
||||
_sqlSampleRepository.Update(sample);
|
||||
await _sqlSampleRepository.SaveAsync();
|
||||
return sample;
|
||||
}
|
||||
|
||||
public async Task<Sample?> DeleteSample(int id)
|
||||
{
|
||||
var sample = await _sqlSampleRepository.GetByIdAsync(id);
|
||||
if (sample == null)
|
||||
return null;
|
||||
|
||||
_sqlSampleRepository.Delete(sample);
|
||||
await _sqlSampleRepository.SaveAsync();
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
@ -1,82 +0,0 @@
|
||||
using Core.Blueprint.DAL.SQLServer;
|
||||
using Core.Blueprint.DAL.SQLServer.Context;
|
||||
using Core.Blueprint.DAL.SQLServer.Contracts;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities;
|
||||
using Core.Blueprint.DAL.SQLServer.Entities.Request;
|
||||
using Core.Blueprint.Redis;
|
||||
using Core.Blueprint.Redis.Helpers;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
public class UserProjectService : IUserProjectService
|
||||
{
|
||||
private readonly IEntityRepository<UserProject, SqlServerContext> _userProjectRepository;
|
||||
private readonly CacheSettings cacheSettings;
|
||||
private readonly IRedisCacheProvider cacheProvider;
|
||||
|
||||
public UserProjectService(IEntityRepository<UserProject, SqlServerContext> userProjectRepository, IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
|
||||
{
|
||||
_userProjectRepository = userProjectRepository;
|
||||
this.cacheSettings = cacheSettings.Value;
|
||||
this.cacheProvider = cacheProvider;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserProject>> GetAllUserProjects()
|
||||
{
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllUserProjects");
|
||||
var cachedData = await cacheProvider.GetAsync<IEnumerable<UserProject>>(cacheKey) ?? [];
|
||||
|
||||
if (cachedData.Any()) return cachedData;
|
||||
|
||||
var userProjects = await _userProjectRepository.GetAllAsync();
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, userProjects);
|
||||
|
||||
return userProjects;
|
||||
}
|
||||
|
||||
public async Task<UserProject?> GetUserProjectById(int id)
|
||||
{
|
||||
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetUserProjectById", id);
|
||||
var cachedData = await cacheProvider.GetAsync<UserProject>(cacheKey);
|
||||
|
||||
if (cachedData is not null) { return cachedData; }
|
||||
|
||||
var userProject = await _userProjectRepository.GetByIdAsync(id);
|
||||
|
||||
await cacheProvider.SetAsync(cacheKey, userProject);
|
||||
|
||||
return userProject;
|
||||
}
|
||||
|
||||
public async Task<UserProject> AddUserProject(UserProjectRequest newUserProject)
|
||||
{
|
||||
var userProject = newUserProject.Adapt<UserProject>();
|
||||
|
||||
await _userProjectRepository.AddAsync(userProject);
|
||||
await _userProjectRepository.SaveAsync();
|
||||
return userProject;
|
||||
}
|
||||
|
||||
public async Task<UserProject?> UpdateUserProject(UserProject userProject)
|
||||
{
|
||||
var existingEntity = await _userProjectRepository.GetByIdAsync(userProject.Id);
|
||||
|
||||
if (existingEntity is null) return existingEntity;
|
||||
|
||||
_userProjectRepository.Update(userProject);
|
||||
await _userProjectRepository.SaveAsync();
|
||||
return userProject;
|
||||
}
|
||||
|
||||
public async Task<UserProject?> DeleteUserProject(int id)
|
||||
{
|
||||
var userProject = await _userProjectRepository.GetByIdAsync(id);
|
||||
if (userProject == null)
|
||||
return null;
|
||||
|
||||
_userProjectRepository.Delete(userProject);
|
||||
await _userProjectRepository.SaveAsync();
|
||||
return userProject;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user