diff --git a/Core.Blueprint.DAL.API/Controllers/MongoBlueprintController.cs b/Core.Blueprint.DAL.API/Controllers/MongoSampleController.cs similarity index 54% rename from Core.Blueprint.DAL.API/Controllers/MongoBlueprintController.cs rename to Core.Blueprint.DAL.API/Controllers/MongoSampleController.cs index c93c1fd..1f8d566 100644 --- a/Core.Blueprint.DAL.API/Controllers/MongoBlueprintController.cs +++ b/Core.Blueprint.DAL.API/Controllers/MongoSampleController.cs @@ -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 CreateBlueprint([FromBody] BlueprintRequest entity, CancellationToken cancellationToken) + public async Task 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 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 GetBlueprint([FromRoute] string _id, CancellationToken cancellationToken) + public async Task 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 UpdateBlueprint([FromRoute] string _id, [FromBody] BlueprintCollection entity, CancellationToken cancellationToken) + public async Task 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 DeleteBlueprint([FromRoute] string _id, CancellationToken cancellationToken) + public async Task 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(); diff --git a/Core.Blueprint.DAL.API/Controllers/UserProjectController.cs b/Core.Blueprint.DAL.API/Controllers/SqlSampleController.cs similarity index 55% rename from Core.Blueprint.DAL.API/Controllers/UserProjectController.cs rename to Core.Blueprint.DAL.API/Controllers/SqlSampleController.cs index 3d767b3..ed27c0e 100644 --- a/Core.Blueprint.DAL.API/Controllers/UserProjectController.cs +++ b/Core.Blueprint.DAL.API/Controllers/SqlSampleController.cs @@ -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> CreateEntity([FromBody] UserProjectRequest request) + public async Task> 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>> GetEntities() + public async Task>> GetEntities() { - var result = await service.GetAllUserProjects().ConfigureAwait(false); + var result = await service.GetAllSamples().ConfigureAwait(false); return Ok(result); } [HttpGet("{id}/GetById")] - public async Task> GetEntity(int id) + public async Task> 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> UpdateEntity(int id, UserProject entity) + public async Task> 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 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(); diff --git a/Core.Blueprint.DAL.API/Extensions/ConfigurationExtension.cs b/Core.Blueprint.DAL.API/Extensions/ConfigurationExtension.cs index cbaef4e..f41118a 100644 --- a/Core.Blueprint.DAL.API/Extensions/ConfigurationExtension.cs +++ b/Core.Blueprint.DAL.API/Extensions/ConfigurationExtension.cs @@ -17,12 +17,12 @@ namespace Core.Blueprint.DAL.API.Extensions public static IServiceCollection AddDALLayerServices(this IServiceCollection services, IConfiguration configuration) { //Mongo - services.AddScoped(); - services.AddScoped>(); + services.AddScoped(); + services.AddScoped>(); //SQL services.AddDbContext(options => options.UseSqlServer(configuration.GetConnectionString("SQLServer"))); - services.AddScoped(); + services.AddScoped(); //Storage services.AddScoped(); diff --git a/Core.Blueprint.DAL.Mongo/Contracts/IBlueprintService.cs b/Core.Blueprint.DAL.Mongo/Contracts/IBlueprintService.cs deleted file mode 100644 index 90502b2..0000000 --- a/Core.Blueprint.DAL.Mongo/Contracts/IBlueprintService.cs +++ /dev/null @@ -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 CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken); - - ValueTask GetBlueprintById(string _id, CancellationToken cancellationToken); - - ValueTask> GetAllBlueprints(CancellationToken cancellationToken); - - ValueTask UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken); - - ValueTask DeleteBlueprint(string _id, CancellationToken cancellationToken); - } -} diff --git a/Core.Blueprint.DAL.Mongo/Contracts/IMongoSampleService.cs b/Core.Blueprint.DAL.Mongo/Contracts/IMongoSampleService.cs new file mode 100644 index 0000000..0c752fa --- /dev/null +++ b/Core.Blueprint.DAL.Mongo/Contracts/IMongoSampleService.cs @@ -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 CreateSample(SampleRequest newSample, CancellationToken cancellationToken); + + ValueTask GetSampleById(string _id, CancellationToken cancellationToken); + + ValueTask> GetAllSamples(CancellationToken cancellationToken); + + ValueTask UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken); + + ValueTask DeleteSample(string _id, CancellationToken cancellationToken); + } +} diff --git a/Core.Blueprint.DAL.Mongo/Entities/Collections/BlueprintCollection.cs b/Core.Blueprint.DAL.Mongo/Entities/Collections/SampleCollection.cs similarity index 68% rename from Core.Blueprint.DAL.Mongo/Entities/Collections/BlueprintCollection.cs rename to Core.Blueprint.DAL.Mongo/Entities/Collections/SampleCollection.cs index 30b23b4..4f71c1b 100644 --- a/Core.Blueprint.DAL.Mongo/Entities/Collections/BlueprintCollection.cs +++ b/Core.Blueprint.DAL.Mongo/Entities/Collections/SampleCollection.cs @@ -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; } diff --git a/Core.Blueprint.DAL.Mongo/Entities/Requests/BlueprintRequest.cs b/Core.Blueprint.DAL.Mongo/Entities/Requests/SampleRequest.cs similarity index 83% rename from Core.Blueprint.DAL.Mongo/Entities/Requests/BlueprintRequest.cs rename to Core.Blueprint.DAL.Mongo/Entities/Requests/SampleRequest.cs index 3cf6881..5f19c7d 100644 --- a/Core.Blueprint.DAL.Mongo/Entities/Requests/BlueprintRequest.cs +++ b/Core.Blueprint.DAL.Mongo/Entities/Requests/SampleRequest.cs @@ -1,6 +1,6 @@ namespace Core.Blueprint.DAL.Mongo.Entities.Requests { - public class BlueprintRequest + public class SampleRequest { public string Name { get; set; } = null!; diff --git a/Core.Blueprint.DAL.Mongo/Service/BlueprintService.cs b/Core.Blueprint.DAL.Mongo/Service/BlueprintService.cs deleted file mode 100644 index 6aeb023..0000000 --- a/Core.Blueprint.DAL.Mongo/Service/BlueprintService.cs +++ /dev/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 repository; - private readonly CacheSettings cacheSettings; - private readonly IRedisCacheProvider cacheProvider; - - public BlueprintService(CollectionRepository repository, - IRedisCacheProvider cacheProvider, IOptions cacheSettings) - { - this.repository = repository; - this.repository.CollectionInitialization(); - this.cacheSettings = cacheSettings.Value; - this.cacheProvider = cacheProvider; - } - - public async ValueTask CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken) - { - var blueprintCollection = newBlueprint.Adapt(); - - await this.repository.InsertOneAsync(blueprintCollection); - - return blueprintCollection; - } - - public async ValueTask GetBlueprintById(string _id, CancellationToken cancellationToken) - { - - var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetBlueprintById", _id); - var cachedData = await cacheProvider.GetAsync(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> GetAllBlueprints(CancellationToken cancellationToken) - { - - var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllBlueprints"); - var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; - - if (cachedData.Any()) return cachedData; - - var blueprints = await this.repository.AsQueryable(); - - await cacheProvider.SetAsync(cacheKey, blueprints); - - return blueprints; - } - - public async ValueTask UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken) - { - await this.repository.ReplaceOneAsync(entity); - - return entity; - } - - public async ValueTask DeleteBlueprint(string _id, CancellationToken cancellationToken) - { - var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id); - - return entity; - } - } -} diff --git a/Core.Blueprint.DAL.Mongo/Service/MongoSampleService.cs b/Core.Blueprint.DAL.Mongo/Service/MongoSampleService.cs new file mode 100644 index 0000000..ad993d4 --- /dev/null +++ b/Core.Blueprint.DAL.Mongo/Service/MongoSampleService.cs @@ -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 repository; + private readonly CacheSettings cacheSettings; + private readonly IRedisCacheProvider cacheProvider; + + public MongoSampleService(CollectionRepository repository, + IRedisCacheProvider cacheProvider, IOptions cacheSettings) + { + this.repository = repository; + this.repository.CollectionInitialization(); + this.cacheSettings = cacheSettings.Value; + this.cacheProvider = cacheProvider; + } + + public async ValueTask CreateSample(SampleRequest newSample, CancellationToken cancellationToken) + { + var sampleCollection = newSample.Adapt(); + + await this.repository.InsertOneAsync(sampleCollection); + + return sampleCollection; + } + + public async ValueTask GetSampleById(string _id, CancellationToken cancellationToken) + { + + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", _id); + var cachedData = await cacheProvider.GetAsync(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> GetAllSamples(CancellationToken cancellationToken) + { + + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples"); + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var samples = await this.repository.AsQueryable(); + + await cacheProvider.SetAsync(cacheKey, samples); + + return samples; + } + + public async ValueTask UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken) + { + await this.repository.ReplaceOneAsync(entity); + + return entity; + } + + public async ValueTask DeleteSample(string _id, CancellationToken cancellationToken) + { + var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id); + + return entity; + } + } +} diff --git a/Core.Blueprint.DAL.SQLServer/Context/SqlServerContext.cs b/Core.Blueprint.DAL.SQLServer/Context/SqlServerContext.cs index 2eb2f2c..3568cbe 100644 --- a/Core.Blueprint.DAL.SQLServer/Context/SqlServerContext.cs +++ b/Core.Blueprint.DAL.SQLServer/Context/SqlServerContext.cs @@ -7,7 +7,7 @@ namespace Core.Blueprint.DAL.SQLServer.Context public sealed class SqlServerContext : DbContext { public SqlServerContext(DbContextOptions options) : base(options) { } - public DbSet UserProjects { get; set; } + public DbSet UserProjects { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/Core.Blueprint.DAL.SQLServer/Contracts/ISqlSampleService.cs b/Core.Blueprint.DAL.SQLServer/Contracts/ISqlSampleService.cs new file mode 100644 index 0000000..8d9203a --- /dev/null +++ b/Core.Blueprint.DAL.SQLServer/Contracts/ISqlSampleService.cs @@ -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> GetAllSamples(); + + Task GetSampleById(int id); + + Task AddSample(SampleRequest newSample); + + Task UpdateSample(Sample sample); + + Task DeleteSample(int id); + + } +} diff --git a/Core.Blueprint.DAL.SQLServer/Contracts/IUserProjectService.cs b/Core.Blueprint.DAL.SQLServer/Contracts/IUserProjectService.cs deleted file mode 100644 index 04dc118..0000000 --- a/Core.Blueprint.DAL.SQLServer/Contracts/IUserProjectService.cs +++ /dev/null @@ -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> GetAllUserProjects(); - - Task GetUserProjectById(int id); - - Task AddUserProject(UserProjectRequest newUserProject); - - Task UpdateUserProject(UserProject userProject); - - Task DeleteUserProject(int id); - - } -} diff --git a/Core.Blueprint.DAL.SQLServer/Entities/Request/SampleRequest.cs b/Core.Blueprint.DAL.SQLServer/Entities/Request/SampleRequest.cs new file mode 100644 index 0000000..c90df21 --- /dev/null +++ b/Core.Blueprint.DAL.SQLServer/Entities/Request/SampleRequest.cs @@ -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!; + } +} diff --git a/Core.Blueprint.DAL.SQLServer/Entities/Request/UserProjectRequest.cs b/Core.Blueprint.DAL.SQLServer/Entities/Request/UserProjectRequest.cs deleted file mode 100644 index 4d1d8a7..0000000 --- a/Core.Blueprint.DAL.SQLServer/Entities/Request/UserProjectRequest.cs +++ /dev/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!; - } -} diff --git a/Core.Blueprint.DAL.SQLServer/Entities/Sample.cs b/Core.Blueprint.DAL.SQLServer/Entities/Sample.cs new file mode 100644 index 0000000..6fe93ae --- /dev/null +++ b/Core.Blueprint.DAL.SQLServer/Entities/Sample.cs @@ -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!; + } +} \ No newline at end of file diff --git a/Core.Blueprint.DAL.SQLServer/Entities/UserProject.cs b/Core.Blueprint.DAL.SQLServer/Entities/UserProject.cs deleted file mode 100644 index 43fb1e5..0000000 --- a/Core.Blueprint.DAL.SQLServer/Entities/UserProject.cs +++ /dev/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!; - } -} \ No newline at end of file diff --git a/Core.Blueprint.DAL.SQLServer/Services/SqlSampleService.cs b/Core.Blueprint.DAL.SQLServer/Services/SqlSampleService.cs new file mode 100644 index 0000000..34c7a51 --- /dev/null +++ b/Core.Blueprint.DAL.SQLServer/Services/SqlSampleService.cs @@ -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 _sqlSampleRepository; + private readonly CacheSettings cacheSettings; + private readonly IRedisCacheProvider cacheProvider; + + public SqlSampleService(IEntityRepository sqlSampleRepository, IRedisCacheProvider cacheProvider, IOptions cacheSettings) + { + _sqlSampleRepository = sqlSampleRepository; + this.cacheSettings = cacheSettings.Value; + this.cacheProvider = cacheProvider; + } + + public async Task> GetAllSamples() + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples"); + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var samples = await _sqlSampleRepository.GetAllAsync(); + + await cacheProvider.SetAsync(cacheKey, samples); + + return samples; + } + + public async Task GetSampleById(int id) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", id); + var cachedData = await cacheProvider.GetAsync(cacheKey); + + if (cachedData is not null) { return cachedData; } + + var sample = await _sqlSampleRepository.GetByIdAsync(id); + + await cacheProvider.SetAsync(cacheKey, sample); + + return sample; + } + + public async Task AddSample(SampleRequest newSample) + { + var sample = newSample.Adapt(); + + await _sqlSampleRepository.AddAsync(sample); + await _sqlSampleRepository.SaveAsync(); + return sample; + } + + public async Task 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 DeleteSample(int id) + { + var sample = await _sqlSampleRepository.GetByIdAsync(id); + if (sample == null) + return null; + + _sqlSampleRepository.Delete(sample); + await _sqlSampleRepository.SaveAsync(); + return sample; + } +} diff --git a/Core.Blueprint.DAL.SQLServer/Services/UserProjectService.cs b/Core.Blueprint.DAL.SQLServer/Services/UserProjectService.cs deleted file mode 100644 index 615b47b..0000000 --- a/Core.Blueprint.DAL.SQLServer/Services/UserProjectService.cs +++ /dev/null @@ -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 _userProjectRepository; - private readonly CacheSettings cacheSettings; - private readonly IRedisCacheProvider cacheProvider; - - public UserProjectService(IEntityRepository userProjectRepository, IRedisCacheProvider cacheProvider, IOptions cacheSettings) - { - _userProjectRepository = userProjectRepository; - this.cacheSettings = cacheSettings.Value; - this.cacheProvider = cacheProvider; - } - - public async Task> GetAllUserProjects() - { - var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllUserProjects"); - var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; - - if (cachedData.Any()) return cachedData; - - var userProjects = await _userProjectRepository.GetAllAsync(); - - await cacheProvider.SetAsync(cacheKey, userProjects); - - return userProjects; - } - - public async Task GetUserProjectById(int id) - { - var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetUserProjectById", id); - var cachedData = await cacheProvider.GetAsync(cacheKey); - - if (cachedData is not null) { return cachedData; } - - var userProject = await _userProjectRepository.GetByIdAsync(id); - - await cacheProvider.SetAsync(cacheKey, userProject); - - return userProject; - } - - public async Task AddUserProject(UserProjectRequest newUserProject) - { - var userProject = newUserProject.Adapt(); - - await _userProjectRepository.AddAsync(userProject); - await _userProjectRepository.SaveAsync(); - return userProject; - } - - public async Task 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 DeleteUserProject(int id) - { - var userProject = await _userProjectRepository.GetByIdAsync(id); - if (userProject == null) - return null; - - _userProjectRepository.Delete(userProject); - await _userProjectRepository.SaveAsync(); - return userProject; - } -}