First version of DAL
This commit is contained in:
		
							
								
								
									
										58
									
								
								Core.Inventory.Provider/Contracts/IFurnitureBaseProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								Core.Inventory.Provider/Contracts/IFurnitureBaseProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="IFurnitureBaseProvider.cs"> | ||||
| //     Core.Inventory | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Contracts | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Interface for managing base furniture models. | ||||
|     /// </summary> | ||||
|     public interface IFurnitureBaseProvider | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Creates a new FurnitureBase entity. | ||||
|         /// </summary> | ||||
|         /// <param name="newFurniture">The DTO representing the furniture base.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The created <see cref="FurnitureBase"/>.</returns> | ||||
|         ValueTask<FurnitureBase> CreateAsync(FurnitureBaseRequest newFurniture, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a furniture base entity by its ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The unique identifier (_id) of the furniture base.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The corresponding <see cref="FurnitureBase"/>.</returns> | ||||
|         ValueTask<FurnitureBase> GetByIdAsync(string id, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Retrieves all furniture base entries. | ||||
|         /// </summary> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>A list of <see cref="FurnitureBase"/>.</returns> | ||||
|         ValueTask<IEnumerable<FurnitureBase>> GetAllAsync(CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates an existing furniture base by ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture base identifier.</param> | ||||
|         /// <param name="entity">The updated entity data.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureBase"/>.</returns> | ||||
|         ValueTask<FurnitureBase> UpdateAsync(string id, FurnitureBase entity, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a furniture base entity. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The entity identifier.</param> | ||||
|         /// <param name="newStatus">The new status to apply.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureBase"/>.</returns> | ||||
|         ValueTask<FurnitureBase> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,59 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="IFurnitureVariantProvider.cs"> | ||||
| //     Core.Inventory | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Contracts | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Interface for managing furniture variants associated with a base furniture model. | ||||
|     /// </summary> | ||||
|     public interface IFurnitureVariantProvider | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Creates a new FurnitureVariant entity. | ||||
|         /// </summary> | ||||
|         /// <param name="newVariant">The DTO representing the furniture variant.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The created <see cref="FurnitureVariant"/>.</returns> | ||||
|         ValueTask<FurnitureVariant> CreateAsync(FurnitureVariantRequest newVariant, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a furniture variant entity by its ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The unique identifier (_id) of the furniture variant.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns> | ||||
|         ValueTask<FurnitureVariant> GetByIdAsync(string id, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Retrieves all furniture variants associated with a base model. | ||||
|         /// </summary> | ||||
|         /// <param name="modelId">The ID of the base furniture model.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>A list of <see cref="FurnitureVariant"/>.</returns> | ||||
|         ValueTask<IEnumerable<FurnitureVariant>> GetAllByModelIdAsync(string modelId, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates an existing furniture variant by ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The variant identifier.</param> | ||||
|         /// <param name="entity">The updated entity data.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureVariant"/>.</returns> | ||||
|         ValueTask<FurnitureVariant> UpdateAsync(string id, FurnitureVariant entity, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a furniture variant entity. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The entity identifier.</param> | ||||
|         /// <param name="newStatus">The new status to apply.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureVariant"/>.</returns> | ||||
|         ValueTask<FurnitureVariant> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										20
									
								
								Core.Inventory.Provider/Core.Inventory.Provider.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Core.Inventory.Provider/Core.Inventory.Provider.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net8.0</TargetFramework> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|     <Nullable>enable</Nullable> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.3" /> | ||||
|     <PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" /> | ||||
|     <PackageReference Include="Core.Blueprint.Redis" Version="1.0.1" /> | ||||
|     <PackageReference Include="Mapster" Version="7.4.0" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <ProjectReference Include="..\Core.Inventory.Domain\Core.Inventory.Domain.csproj" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
| @@ -0,0 +1,114 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="FurnitureBaseProvider.cs"> | ||||
| //     Core.Inventory | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Blueprint.Redis; | ||||
| using Core.Blueprint.Redis.Helpers; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
| using Core.Inventory.Provider.Contracts; | ||||
| using Mapster; | ||||
| using Microsoft.Extensions.Options; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Providers.Inventory | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all operations related to <see cref="FurnitureBase"/>. | ||||
|     /// </summary> | ||||
|     public class FurnitureBaseProvider : IFurnitureBaseProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<FurnitureBase> repository; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|         private readonly CacheSettings cacheSettings; | ||||
|  | ||||
|         public FurnitureBaseProvider( | ||||
|             CollectionRepository<FurnitureBase> repository, | ||||
|             IRedisCacheProvider cacheProvider, | ||||
|             IOptions<CacheSettings> cacheSettings | ||||
|             ) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheProvider = cacheProvider; | ||||
|             this.cacheSettings = cacheSettings.Value; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a FurnitureBase entity. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture base identifier.</param> | ||||
|         /// <param name="newStatus">The new status to apply.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureBase"/>.</returns> | ||||
|         public async ValueTask<FurnitureBase> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.FindByIdAsync(id); | ||||
|             entity.Status = newStatus; | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new FurnitureBase entity. | ||||
|         /// </summary> | ||||
|         /// <param name="newFurniture">The DTO representing the base furniture model.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The created <see cref="FurnitureBase"/>.</returns> | ||||
|         public async ValueTask<FurnitureBase> CreateAsync(FurnitureBaseRequest newFurniture, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var furnitureCollection = newFurniture.Adapt<FurnitureBase>(); | ||||
|             await repository.InsertOneAsync(furnitureCollection); | ||||
|             return furnitureCollection; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Retrieves all FurnitureBase entries. | ||||
|         /// </summary> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>A list of <see cref="FurnitureBase"/>.</returns> | ||||
|         public async ValueTask<IEnumerable<FurnitureBase>> GetAllAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllAsync)); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureBase>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var data = await repository.AsQueryable(); | ||||
|             await cacheProvider.SetAsync(cacheKey, data); | ||||
|             return data; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a FurnitureBase entity by its ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture base identifier.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The corresponding <see cref="FurnitureBase"/>.</returns> | ||||
|         public async ValueTask<FurnitureBase> GetByIdAsync(string id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), id); | ||||
|             var cached = await cacheProvider.GetAsync<FurnitureBase>(cacheKey); | ||||
|  | ||||
|             if (cached is not null) return cached; | ||||
|  | ||||
|             var result = await repository.FindByIdAsync(id); | ||||
|             await cacheProvider.SetAsync(cacheKey, result); | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a FurnitureBase entity by ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture base identifier.</param> | ||||
|         /// <param name="entity">The updated entity data.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureBase"/>.</returns> | ||||
|         public async ValueTask<FurnitureBase> UpdateAsync(string id, FurnitureBase entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,114 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="FurnitureVariantProvider.cs"> | ||||
| //     Core.Inventory | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Blueprint.Redis; | ||||
| using Core.Blueprint.Redis.Helpers; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
| using Core.Inventory.Provider.Contracts; | ||||
| using Mapster; | ||||
| using Microsoft.Extensions.Options; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Providers.Inventory | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all operations related to <see cref="FurnitureVariant"/>. | ||||
|     /// </summary> | ||||
|     public class FurnitureVariantProvider : IFurnitureVariantProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<FurnitureVariant> repository; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|         private readonly CacheSettings cacheSettings; | ||||
|  | ||||
|         public FurnitureVariantProvider( | ||||
|             CollectionRepository<FurnitureVariant> repository, | ||||
|             IRedisCacheProvider cacheProvider, | ||||
|             IOptions<CacheSettings> cacheSettings | ||||
|             ) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheProvider = cacheProvider; | ||||
|             this.cacheSettings = cacheSettings.Value; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a FurnitureVariant entity. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture variant identifier.</param> | ||||
|         /// <param name="newStatus">The new status to apply.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureVariant"/>.</returns> | ||||
|         public async ValueTask<FurnitureVariant> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.FindByIdAsync(id); | ||||
|             entity.Status = newStatus; | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new FurnitureVariant entity. | ||||
|         /// </summary> | ||||
|         /// <param name="newVariant">The DTO representing the furniture variant.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The created <see cref="FurnitureVariant"/>.</returns> | ||||
|         public async ValueTask<FurnitureVariant> CreateAsync(FurnitureVariantRequest newVariant, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var variantCollection = newVariant.Adapt<FurnitureVariant>(); | ||||
|             await repository.InsertOneAsync(variantCollection); | ||||
|             return variantCollection; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Retrieves all FurnitureVariant entries. | ||||
|         /// </summary> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>A list of <see cref="FurnitureVariant"/>.</returns> | ||||
|         public async ValueTask<IEnumerable<FurnitureVariant>> GetAllByModelIdAsync(string modelId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllByModelIdAsync)); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureVariant>>(cacheKey); | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var data = await repository.AsQueryable(); | ||||
|             await cacheProvider.SetAsync(cacheKey, data); | ||||
|             return data; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a FurnitureVariant entity by its ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture variant identifier.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns> | ||||
|         public async ValueTask<FurnitureVariant> GetByIdAsync(string id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), id); | ||||
|             var cached = await cacheProvider.GetAsync<FurnitureVariant>(cacheKey); | ||||
|  | ||||
|             if (cached is not null) return cached; | ||||
|  | ||||
|             var result = await repository.FindByIdAsync(id); | ||||
|             await cacheProvider.SetAsync(cacheKey, result); | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a FurnitureVariant entity by ID. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The furniture variant identifier.</param> | ||||
|         /// <param name="entity">The updated entity data.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token.</param> | ||||
|         /// <returns>The updated <see cref="FurnitureVariant"/>.</returns> | ||||
|         public async ValueTask<FurnitureVariant> UpdateAsync(string id, FurnitureVariant entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|             return entity; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										24
									
								
								Core.Inventory.Provider/ServiceCollectionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Core.Inventory.Provider/ServiceCollectionExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Provider.Contracts; | ||||
| using Core.Inventory.Provider.Providers.Inventory; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
|  | ||||
| namespace Core.Inventory.Provider | ||||
| { | ||||
|     public static class ServiceCollectionExtensions | ||||
|     { | ||||
|         public static IServiceCollection AddDALLayerServices(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             services.AddScoped<IFurnitureBaseProvider, FurnitureBaseProvider>(); | ||||
|             services.AddScoped<CollectionRepository<FurnitureBase>>(); | ||||
|  | ||||
|             services.AddScoped<IFurnitureVariantProvider, FurnitureVariantProvider>(); | ||||
|             services.AddScoped<CollectionRepository<FurnitureVariant>>(); | ||||
|  | ||||
|             return services; | ||||
|  | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user