6 Commits

Author SHA1 Message Date
e191851982 Final fixes for demo 2025-06-27 23:10:38 -06:00
9effaf3b22 Added new endpoint for variants and temporal fix for reset cache 2025-06-27 17:41:12 -06:00
55475e0f97 Fixes in parameter names 2025-06-23 01:56:11 -06:00
eae1e1580e Fix in variable names 2025-06-23 01:55:30 -06:00
0f9a4c2af2 fixes in controller 2025-06-22 23:15:53 -06:00
25cbdd25fc Fixes in endpoints etc 2025-06-22 22:23:39 -06:00
10 changed files with 178 additions and 49 deletions

View File

@@ -40,12 +40,12 @@ namespace Core.Inventory.DAL.API.Controllers
/// Gets a furniture base record by ID. /// Gets a furniture base record by ID.
/// </summary> /// </summary>
[HttpGet] [HttpGet]
[Route("{id}")] [Route("{mongoId}")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)] [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByIdAsync([FromRoute] string id, CancellationToken cancellationToken) public async Task<IActionResult> GetByIdAsync([FromRoute] string mongoId, CancellationToken cancellationToken)
{ {
var result = await service.GetByIdAsync(id, cancellationToken); var result = await service.GetByIdAsync(mongoId, cancellationToken);
return result is not null ? Ok(result) : NotFound("Entity not found"); return result is not null ? Ok(result) : NotFound("Entity not found");
} }
@@ -80,11 +80,11 @@ namespace Core.Inventory.DAL.API.Controllers
/// Changes the status of a furniture base record. /// Changes the status of a furniture base record.
/// </summary> /// </summary>
[HttpPatch] [HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")] [Route("{mongoId}/{newStatus}/ChangeStatus")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)] [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangeStatusAsync([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) public async Task<IActionResult> ChangeStatusAsync([FromRoute] string mongoId, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{ {
var result = await service.ChangeStatusAsync(id, newStatus, cancellationToken); var result = await service.ChangeStatusAsync(mongoId, newStatus, cancellationToken);
return Ok(result); return Ok(result);
} }
} }

View File

@@ -25,12 +25,25 @@ namespace Core.Inventory.DAL.API.Controllers
[AllowAnonymous] [AllowAnonymous]
public class FurnitureVariantController(IFurnitureVariantProvider service) : ControllerBase public class FurnitureVariantController(IFurnitureVariantProvider service) : ControllerBase
{ {
/// <summary> /// <summary>
/// Gets all furniture variant records. /// Gets all furniture variant records.
/// </summary> /// </summary>
[HttpGet] [HttpGet]
[ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllVariantsByModelIdAsync([FromRoute] string modelId, CancellationToken cancellationToken) public async Task<IActionResult> GetAllAsync(CancellationToken cancellationToken)
{
var result = await service.GetAllAsync(cancellationToken);
return Ok(result);
}
/// <summary>
/// Gets all furniture variant records by ModelId.
/// </summary>
[HttpGet]
[Route("ByModel/{modelId}")]
[ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllVariantsByModelIdAsync(string modelId, CancellationToken cancellationToken)
{ {
var result = await service.GetAllByModelIdAsync(modelId, cancellationToken).ConfigureAwait(false); var result = await service.GetAllByModelIdAsync(modelId, cancellationToken).ConfigureAwait(false);
@@ -46,15 +59,38 @@ namespace Core.Inventory.DAL.API.Controllers
/// Gets a furniture variant record by ID. /// Gets a furniture variant record by ID.
/// </summary> /// </summary>
[HttpGet] [HttpGet]
[Route("{id}")] [Route("{mongoId}")]
[ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)] [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByIdAsync([FromRoute] string id, CancellationToken cancellationToken) public async Task<IActionResult> GetByIdAsync([FromRoute] string mongoId, CancellationToken cancellationToken)
{ {
var result = await service.GetByIdAsync(id, cancellationToken); var result = await service.GetByIdAsync(mongoId, cancellationToken);
return result is not null ? Ok(result) : NotFound("Entity not found"); return result is not null ? Ok(result) : NotFound("Entity not found");
} }
/// <summary>
/// Gets multiple furniture variants by their identifiers.
/// </summary>
/// <param name="request">List of variant IDs.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A list of <see cref="FurnitureVariant"/>.</returns>
[HttpPost("ByIds")]
[ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetVariantsByIdsAsync([FromBody] string[] ids, CancellationToken cancellationToken)
{
if (ids is null || ids.Length == 0)
return BadRequest("At least one variant ID must be provided.");
var result = await service.GetAllByIdsAsync(ids, cancellationToken);
if (result is null || !result.Any())
return NoContent();
return Ok(result);
}
/// <summary> /// <summary>
/// Creates a new furniture variant. /// Creates a new furniture variant.
/// </summary> /// </summary>
@@ -86,11 +122,11 @@ namespace Core.Inventory.DAL.API.Controllers
/// Changes the status of a furniture variant record. /// Changes the status of a furniture variant record.
/// </summary> /// </summary>
[HttpPatch] [HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")] [Route("{mongoId}/{newStatus}/ChangeStatus")]
[ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)] [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangeStatusAsync([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) public async Task<IActionResult> ChangeStatusAsync([FromRoute] string mongoId, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{ {
var result = await service.ChangeStatusAsync(id, newStatus, cancellationToken); var result = await service.ChangeStatusAsync(mongoId, newStatus, cancellationToken);
return Ok(result); return Ok(result);
} }
} }

View File

@@ -12,7 +12,7 @@
}, },
"MongoDb": { "MongoDb": {
"DatabaseName": "Inventory", "DatabaseName": "Inventory",
"LocalAudience": "" "LocalAudience": "InventotyDev"
}, },
"DetailedErrors": true, "DetailedErrors": true,
"UseRedisCache": true, "UseRedisCache": true,

View File

@@ -12,7 +12,7 @@
}, },
"MongoDb": { "MongoDb": {
"DatabaseName": "Inventory", "DatabaseName": "Inventory",
"LocalAudience": "" "LocalAudience": "InventotyLocal"
}, },
"DetailedErrors": true, "DetailedErrors": true,
"UseRedisCache": true, "UseRedisCache": true,

View File

@@ -37,7 +37,7 @@ namespace Core.Inventory.Domain.Contexts.Inventory.Request
[BsonElement("currency")] [BsonElement("currency")]
[BsonRepresentation(BsonType.String)] [BsonRepresentation(BsonType.String)]
[JsonPropertyName("currency")] [JsonPropertyName("currency")]
public string Currency { get; set; } = "MXN"; public string Currency { get; set; } = "USD";
/// <summary> /// <summary>
/// Gets or sets the category identifier the item belongs to. /// Gets or sets the category identifier the item belongs to.
@@ -45,7 +45,7 @@ namespace Core.Inventory.Domain.Contexts.Inventory.Request
[BsonElement("categoryId")] [BsonElement("categoryId")]
[BsonRepresentation(BsonType.String)] [BsonRepresentation(BsonType.String)]
[JsonPropertyName("categoryId")] [JsonPropertyName("categoryId")]
public Guid CategoryId { get; set; } public string CategoryId { get; set; } = string.Empty;
/// <summary> /// <summary>
/// Gets or sets the provider or vendor identifier of the item. /// Gets or sets the provider or vendor identifier of the item.
@@ -53,7 +53,7 @@ namespace Core.Inventory.Domain.Contexts.Inventory.Request
[BsonElement("providerId")] [BsonElement("providerId")]
[BsonRepresentation(BsonType.String)] [BsonRepresentation(BsonType.String)]
[JsonPropertyName("providerId")] [JsonPropertyName("providerId")]
public Guid ProviderId { get; set; } public string ProviderId { get; set; } = string.Empty;
/// <summary> /// <summary>
/// Gets or sets additional customizable attributes. /// Gets or sets additional customizable attributes.
@@ -61,6 +61,6 @@ namespace Core.Inventory.Domain.Contexts.Inventory.Request
/// </summary> /// </summary>
[BsonElement("attributes")] [BsonElement("attributes")]
[JsonPropertyName("attributes")] [JsonPropertyName("attributes")]
public Dictionary<string, object> Attributes { get; set; } = []; public Dictionary<string, string> Attributes { get; set; } = [];
} }
} }

View File

@@ -25,10 +25,10 @@ namespace Core.Inventory.Provider.Contracts
/// <summary> /// <summary>
/// Gets a furniture base entity by its ID. /// Gets a furniture base entity by its ID.
/// </summary> /// </summary>
/// <param name="id">The unique identifier (_id) of the furniture base.</param> /// <param name="_id">The unique identifier (_id) of the furniture base.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The corresponding <see cref="FurnitureBase"/>.</returns> /// <returns>The corresponding <see cref="FurnitureBase"/>.</returns>
ValueTask<FurnitureBase> GetByIdAsync(string id, CancellationToken cancellationToken); ValueTask<FurnitureBase> GetByIdAsync(string _id, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Retrieves all furniture base entries. /// Retrieves all furniture base entries.
@@ -49,10 +49,10 @@ namespace Core.Inventory.Provider.Contracts
/// <summary> /// <summary>
/// Changes the status of a furniture base entity. /// Changes the status of a furniture base entity.
/// </summary> /// </summary>
/// <param name="id">The entity identifier.</param> /// <param name="_id">The entity identifier.</param>
/// <param name="newStatus">The new status to apply.</param> /// <param name="newStatus">The new status to apply.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The updated <see cref="FurnitureBase"/>.</returns> /// <returns>The updated <see cref="FurnitureBase"/>.</returns>
ValueTask<FurnitureBase> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken); ValueTask<FurnitureBase> ChangeStatusAsync(string _id, StatusEnum newStatus, CancellationToken cancellationToken);
} }
} }

View File

@@ -25,10 +25,18 @@ namespace Core.Inventory.Provider.Contracts
/// <summary> /// <summary>
/// Gets a furniture variant entity by its ID. /// Gets a furniture variant entity by its ID.
/// </summary> /// </summary>
/// <param name="id">The unique identifier (_id) of the furniture variant.</param> /// <param name="_id">The unique identifier (_id) of the furniture variant.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns> /// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns>
ValueTask<FurnitureVariant> GetByIdAsync(string id, CancellationToken cancellationToken); ValueTask<FurnitureVariant> GetByIdAsync(string _id, CancellationToken cancellationToken);
/// <summary>
/// Retrieves all furniture variants by a list of variant IDs.
/// </summary>
/// <param name="ids">Array of variant IDs.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A list of <see cref="FurnitureVariant"/> matching the specified IDs.</returns>
ValueTask<IEnumerable<FurnitureVariant>> GetAllByIdsAsync(string[] ids, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Retrieves all furniture variants associated with a base model. /// Retrieves all furniture variants associated with a base model.
@@ -50,10 +58,17 @@ namespace Core.Inventory.Provider.Contracts
/// <summary> /// <summary>
/// Changes the status of a furniture variant entity. /// Changes the status of a furniture variant entity.
/// </summary> /// </summary>
/// <param name="id">The entity identifier.</param> /// <param name="_id">The entity identifier.</param>
/// <param name="newStatus">The new status to apply.</param> /// <param name="newStatus">The new status to apply.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The updated <see cref="FurnitureVariant"/>.</returns> /// <returns>The updated <see cref="FurnitureVariant"/>.</returns>
ValueTask<FurnitureVariant> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken); ValueTask<FurnitureVariant> ChangeStatusAsync(string _id, StatusEnum newStatus, CancellationToken cancellationToken);
/// <summary>
/// Retrieves all furniture variant entities.
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A list of all <see cref="FurnitureVariant"/> entities.</returns>
ValueTask<IEnumerable<FurnitureVariant>> GetAllAsync(CancellationToken cancellationToken);
} }
} }

View File

@@ -7,9 +7,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Adapters.Lib" Version="1.0.3" /> <PackageReference Include="Adapters.Lib" Version="1.0.8" />
<PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" /> <PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" />
<PackageReference Include="Core.Blueprint.Redis" Version="1.0.1" /> <PackageReference Include="Core.Blueprint.Redis" Version="1.0.2" />
<PackageReference Include="Mapster" Version="7.4.0" /> <PackageReference Include="Mapster" Version="7.4.0" />
</ItemGroup> </ItemGroup>

View File

@@ -22,6 +22,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
private readonly CollectionRepository<FurnitureBase> repository; private readonly CollectionRepository<FurnitureBase> repository;
private readonly IRedisCacheProvider cacheProvider; private readonly IRedisCacheProvider cacheProvider;
private readonly CacheSettings cacheSettings; private readonly CacheSettings cacheSettings;
private const string getAllCache = "GetAllFurnitureBases";
public FurnitureBaseProvider( public FurnitureBaseProvider(
CollectionRepository<FurnitureBase> repository, CollectionRepository<FurnitureBase> repository,
@@ -38,15 +39,16 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary> /// <summary>
/// Changes the status of a FurnitureBase entity. /// Changes the status of a FurnitureBase entity.
/// </summary> /// </summary>
/// <param name="id">The furniture base identifier.</param> /// <param name="mongoId">The furniture base identifier.</param>
/// <param name="newStatus">The new status to apply.</param> /// <param name="newStatus">The new status to apply.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The updated <see cref="FurnitureBase"/>.</returns> /// <returns>The updated <see cref="FurnitureBase"/>.</returns>
public async ValueTask<FurnitureBase> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken) public async ValueTask<FurnitureBase> ChangeStatusAsync(string mongoId, StatusEnum newStatus, CancellationToken cancellationToken)
{ {
var entity = await repository.FindByIdAsync(id); var entity = await repository.FindByIdAsync(mongoId);
entity.Status = newStatus; entity.Status = newStatus;
await repository.ReplaceOneAsync(entity); await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity; return entity;
} }
@@ -60,6 +62,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
{ {
var furnitureCollection = newFurniture.Adapt<FurnitureBase>(); var furnitureCollection = newFurniture.Adapt<FurnitureBase>();
await repository.InsertOneAsync(furnitureCollection); await repository.InsertOneAsync(furnitureCollection);
await ResetCollectionCache();
return furnitureCollection; return furnitureCollection;
} }
@@ -70,7 +73,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <returns>A list of <see cref="FurnitureBase"/>.</returns> /// <returns>A list of <see cref="FurnitureBase"/>.</returns>
public async ValueTask<IEnumerable<FurnitureBase>> GetAllAsync(CancellationToken cancellationToken) public async ValueTask<IEnumerable<FurnitureBase>> GetAllAsync(CancellationToken cancellationToken)
{ {
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllAsync)); var cacheKey = CacheKeyHelper.GenerateCacheKey(this, getAllCache);
var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureBase>>(cacheKey) ?? []; var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureBase>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData; if (cachedData.Any()) return cachedData;
@@ -83,17 +86,17 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary> /// <summary>
/// Gets a FurnitureBase entity by its ID. /// Gets a FurnitureBase entity by its ID.
/// </summary> /// </summary>
/// <param name="id">The furniture base identifier.</param> /// <param name="mongoId">The furniture base identifier.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The corresponding <see cref="FurnitureBase"/>.</returns> /// <returns>The corresponding <see cref="FurnitureBase"/>.</returns>
public async ValueTask<FurnitureBase> GetByIdAsync(string id, CancellationToken cancellationToken) public async ValueTask<FurnitureBase> GetByIdAsync(string mongoId, CancellationToken cancellationToken)
{ {
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), id); var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), mongoId);
var cached = await cacheProvider.GetAsync<FurnitureBase>(cacheKey); var cached = await cacheProvider.GetAsync<FurnitureBase>(cacheKey);
if (cached is not null) return cached; if (cached is not null) return cached;
var result = await repository.FindByIdAsync(id); var result = await repository.FindByIdAsync(mongoId);
await cacheProvider.SetAsync(cacheKey, result); await cacheProvider.SetAsync(cacheKey, result);
return result; return result;
} }
@@ -108,7 +111,20 @@ namespace Core.Inventory.Provider.Providers.Inventory
public async ValueTask<FurnitureBase> UpdateAsync(string id, FurnitureBase entity, CancellationToken cancellationToken) public async ValueTask<FurnitureBase> UpdateAsync(string id, FurnitureBase entity, CancellationToken cancellationToken)
{ {
await repository.ReplaceOneAsync(entity); await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity; return entity;
} }
/// <summary>
/// Temporary method to "reset" collections cache
/// </summary>
/// <returns></returns>
private async Task ResetCollectionCache()
{
//TODO: remove this method when necessary.
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, getAllCache);
await cacheProvider.SetAsync(cacheKey, Enumerable.Empty<FurnitureBase>(), null);
}
} }
} }

View File

@@ -11,6 +11,7 @@ using Core.Inventory.Domain.Contexts.Inventory.Request;
using Core.Inventory.Provider.Contracts; using Core.Inventory.Provider.Contracts;
using Mapster; using Mapster;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace Core.Inventory.Provider.Providers.Inventory namespace Core.Inventory.Provider.Providers.Inventory
{ {
@@ -22,6 +23,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
private readonly CollectionRepository<FurnitureVariant> repository; private readonly CollectionRepository<FurnitureVariant> repository;
private readonly IRedisCacheProvider cacheProvider; private readonly IRedisCacheProvider cacheProvider;
private readonly CacheSettings cacheSettings; private readonly CacheSettings cacheSettings;
private const string getAllVariantsCache = "GetAllFurnitureVariants";
public FurnitureVariantProvider( public FurnitureVariantProvider(
CollectionRepository<FurnitureVariant> repository, CollectionRepository<FurnitureVariant> repository,
@@ -38,15 +40,16 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary> /// <summary>
/// Changes the status of a FurnitureVariant entity. /// Changes the status of a FurnitureVariant entity.
/// </summary> /// </summary>
/// <param name="id">The furniture variant identifier.</param> /// <param name="mongoId">The furniture variant identifier.</param>
/// <param name="newStatus">The new status to apply.</param> /// <param name="newStatus">The new status to apply.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The updated <see cref="FurnitureVariant"/>.</returns> /// <returns>The updated <see cref="FurnitureVariant"/>.</returns>
public async ValueTask<FurnitureVariant> ChangeStatusAsync(string id, StatusEnum newStatus, CancellationToken cancellationToken) public async ValueTask<FurnitureVariant> ChangeStatusAsync(string mongoId, StatusEnum newStatus, CancellationToken cancellationToken)
{ {
var entity = await repository.FindByIdAsync(id); var entity = await repository.FindByIdAsync(mongoId);
entity.Status = newStatus; entity.Status = newStatus;
await repository.ReplaceOneAsync(entity); await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity; return entity;
} }
@@ -60,6 +63,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
{ {
var variantCollection = newVariant.Adapt<FurnitureVariant>(); var variantCollection = newVariant.Adapt<FurnitureVariant>();
await repository.InsertOneAsync(variantCollection); await repository.InsertOneAsync(variantCollection);
await ResetCollectionCache();
return variantCollection; return variantCollection;
} }
@@ -70,34 +74,62 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <returns>A list of <see cref="FurnitureVariant"/>.</returns> /// <returns>A list of <see cref="FurnitureVariant"/>.</returns>
public async ValueTask<IEnumerable<FurnitureVariant>> GetAllByModelIdAsync(string modelId, CancellationToken cancellationToken) public async ValueTask<IEnumerable<FurnitureVariant>> GetAllByModelIdAsync(string modelId, CancellationToken cancellationToken)
{ {
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllByModelIdAsync)); var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllByModelIdAsync), modelId);
var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureVariant>>(cacheKey); var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureVariant>>(cacheKey);
if (cachedData is not null && cachedData.Any())
return cachedData;
if (cachedData.Any()) return cachedData; var filter = Builders<FurnitureVariant>.Filter.Eq(x => x.ModelId, modelId);
var variants = await repository.FilterByMongoFilterAsync(filter);
var data = await repository.AsQueryable(); if (variants is not null && variants.Any())
await cacheProvider.SetAsync(cacheKey, data); await cacheProvider.SetAsync(cacheKey, variants);
return data;
return variants ?? [];
} }
/// <summary> /// <summary>
/// Gets a FurnitureVariant entity by its ID. /// Gets a FurnitureVariant entity by its ID.
/// </summary> /// </summary>
/// <param name="id">The furniture variant identifier.</param> /// <param name="mongoId">The furniture variant identifier.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns> /// <returns>The corresponding <see cref="FurnitureVariant"/>.</returns>
public async ValueTask<FurnitureVariant> GetByIdAsync(string id, CancellationToken cancellationToken) public async ValueTask<FurnitureVariant> GetByIdAsync(string mongoId, CancellationToken cancellationToken)
{ {
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), id); var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetByIdAsync), mongoId);
var cached = await cacheProvider.GetAsync<FurnitureVariant>(cacheKey); var cached = await cacheProvider.GetAsync<FurnitureVariant>(cacheKey);
if (cached is not null) return cached; if (cached is not null) return cached;
var result = await repository.FindByIdAsync(id); var result = await repository.FindByIdAsync(mongoId);
await cacheProvider.SetAsync(cacheKey, result); await cacheProvider.SetAsync(cacheKey, result);
return result; return result;
} }
/// <summary>
/// Retrieves all furniture variants by a list of variant IDs.
/// </summary>
/// <param name="ids">Array of variant IDs.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A list of <see cref="FurnitureVariant"/> matching the specified IDs.</returns>
public async ValueTask<IEnumerable<FurnitureVariant>> GetAllByIdsAsync(string[] ids, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllByIdsAsync), ids);
var cachedData = await cacheProvider.GetAsync<IEnumerable<FurnitureVariant>>(cacheKey);
if (cachedData is not null && cachedData.Any())
return cachedData;
var filter = Builders<FurnitureVariant>.Filter.In(x => x.Id, ids);
var variants = await repository.FilterByMongoFilterAsync(filter);
if (variants is not null && variants.Any())
await cacheProvider.SetAsync(cacheKey, variants);
return variants ?? [];
}
/// <summary> /// <summary>
/// Updates a FurnitureVariant entity by ID. /// Updates a FurnitureVariant entity by ID.
/// </summary> /// </summary>
@@ -108,7 +140,37 @@ namespace Core.Inventory.Provider.Providers.Inventory
public async ValueTask<FurnitureVariant> UpdateAsync(string id, FurnitureVariant entity, CancellationToken cancellationToken) public async ValueTask<FurnitureVariant> UpdateAsync(string id, FurnitureVariant entity, CancellationToken cancellationToken)
{ {
await repository.ReplaceOneAsync(entity); await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity; return entity;
} }
/// <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>> GetAllAsync(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, getAllVariantsCache);
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>
/// Temporary method to "reset" collections cache
/// </summary>
/// <returns></returns>
private async Task ResetCollectionCache()
{
//TODO: remove this method when necessary.
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, getAllVariantsCache);
await cacheProvider.SetAsync(cacheKey, Enumerable.Empty<FurnitureVariant>(), null);
}
} }
} }