10 Commits

Author SHA1 Message Date
1f9bae385c Merge pull request 'Add Tag CRUD' (#2) from feature/add-tag-crud into development
Reviewed-on: #2
Reviewed-by: efrain_marin <efrain.marin@agilewebs.com>
Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com>
2025-08-01 22:12:26 +00:00
Oscar Morales
53a420ffd1 Add Tag CRUD 2025-08-01 11:45:04 -06:00
406ff07f62 Merge pull request 'Add TagType CRUD' (#1) from feature/add-tag-type-crud into development
Reviewed-on: #1
Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com>
2025-08-01 14:46:22 +00:00
Oscar Morales
54dd38cfd6 Add TagType CRUD 2025-07-31 19:07:22 -06:00
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
19 changed files with 1114 additions and 49 deletions

View File

@@ -40,12 +40,12 @@ namespace Core.Inventory.DAL.API.Controllers
/// Gets a furniture base record by ID.
/// </summary>
[HttpGet]
[Route("{id}")]
[Route("{mongoId}")]
[ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)]
[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");
}
@@ -80,11 +80,11 @@ namespace Core.Inventory.DAL.API.Controllers
/// Changes the status of a furniture base record.
/// </summary>
[HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")]
[Route("{mongoId}/{newStatus}/ChangeStatus")]
[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);
}
}

View File

@@ -25,12 +25,25 @@ namespace Core.Inventory.DAL.API.Controllers
[AllowAnonymous]
public class FurnitureVariantController(IFurnitureVariantProvider service) : ControllerBase
{
/// <summary>
/// Gets all furniture variant records.
/// </summary>
[HttpGet]
[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);
@@ -46,15 +59,38 @@ namespace Core.Inventory.DAL.API.Controllers
/// Gets a furniture variant record by ID.
/// </summary>
[HttpGet]
[Route("{id}")]
[Route("{mongoId}")]
[ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)]
[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");
}
/// <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>
/// Creates a new furniture variant.
/// </summary>
@@ -86,11 +122,11 @@ namespace Core.Inventory.DAL.API.Controllers
/// Changes the status of a furniture variant record.
/// </summary>
[HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")]
[Route("{mongoId}/{newStatus}/ChangeStatus")]
[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);
}
}

View File

@@ -0,0 +1,190 @@
using Asp.Versioning;
using Core.Adapters.Lib;
using Core.Blueprint.Logging;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
using Core.Inventory.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Inventory.DAL.API.Controllers
{
/// <summary>
/// Handles all requests for Tag authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
[AllowAnonymous]
public class TagController(ITagProvider service) : ControllerBase
{
/// <summary>
/// Gets all the Tags.
/// </summary>
/// <returns>The <see cref="IEnumerable{TagAdapter}"/> found entities.</returns>
/// <response code="200">The Tags found.</response>
/// <response code="404">The Tags not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagsAsync(CancellationToken cancellationToken)
{
var result = await service.GetAllTags(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets all the Tags by Tag identifiers.
/// </summary>
/// <param name="Tags">The list of Tag identifiers.</param>
/// <returns>The <see cref="IEnumerable{TagAdapter}"/> found entities.</returns>
/// <response code="200">The Tags found.</response>
/// <response code="404">The Tags not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route("GetTagList")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagsByList([FromBody] string[] tags, CancellationToken cancellationToken)
{
if (tags == null || !tags.Any())
{
return BadRequest("Tag identifiers are required.");
}
var result = await service.GetAllTagsByList(tags, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets the Tag by identifier.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <returns>The <see cref="TagAdapter"/> found entity.</returns>
/// <response code="200">The Tag found.</response>
/// <response code="404">The Tag not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> GetTagByIdAsync([FromRoute] string id, CancellationToken cancellationToken)
{
var result = await service.GetTagById(id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return NotFound("Entity not found");
}
return Ok(result);
}
/// <summary>
/// Creates a new Tag.
/// </summary>
/// <param name="newTag">The Tag to be added.</param>
/// <returns>The <see cref="TagAdapter"/> created entity.</returns>
/// <response code="201">The Tag created.</response>
/// <response code="422">The Tag could not be created.</response>
/// <response code="500">The service internal e|ror.</response>
[HttpPost]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status201Created)]
public async Task<IActionResult> CreateTagAsync([FromBody] TagRequest newTag, CancellationToken cancellationToken)
{
var result = await service.CreateTag(newTag, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
/// <summary>
/// Updates a full Tag by identifier.
/// </summary>
/// <param name="entity">The Tag to update.</param>
/// <param name="id">The Tag identifier.</param>
/// <returns>The <see cref="TagAdapter"/> updated entity.</returns>
/// <response code="200">The Tag updated.</response>
/// <response code="404">The Tag not found.</response>
/// <response code="422">The Tag could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> UpdateTagAsync([FromRoute] string id, TagAdapter entity, CancellationToken cancellationToken)
{
if (id != entity.Id?.ToString())
{
return BadRequest("Tag ID mismatch");
}
var result = await service.UpdateTag(entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Changes the status of the Tag.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <param name="newStatus">The new status of the Tag.</param>
/// <returns>The <see cref="TagAdapter"/> updated entity.</returns>
/// <response code="200">The Tag updates.</response>
/// <response code="404">The Tag not found.</response>
/// <response code="422">The Tag could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangeTagStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{
var result = await service.ChangeTagStatus(id, newStatus, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Adds a parentTag to the tag.
/// </summary>
/// <param name="tagId">The tag identifier.</param>
/// <param name="parentTagId">The parentTag identifier to add.</param>
/// <returns>The updated <see cref="TagAdapter"/> entity.</returns>
/// <response code="200">The tag with the updated parentTags.</response>
/// <response code="404">The tag or parentTag not found.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route("{tagId}/ParentTags/{parentTagId}/Add")]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> AddParentTagAsync([FromRoute] string tagId, [FromRoute] string parentTagId, CancellationToken cancellationToken)
{
var result = await service.AddParentTag(tagId, parentTagId, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Remove a parentTag to the tag.
/// </summary>
/// <param name="tagId">The tag identifier.</param>
/// <param name="parentTagId">The parentTag identifier to remove.</param>
/// <returns>The updated <see cref="TagAdapter"/> entity.</returns>
/// <response code="200">The tag with the updated parentTags.</response>
/// <response code="404">The tag or parentTag not found.</response>
/// <response code="500">The service internal error.</response>
[HttpDelete]
[Route("{tagId}/ParentTags/{parentTagId}/Remove")]
[ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> RemoveParentTagAsync([FromRoute] string tagId, [FromRoute] string parentTagId, CancellationToken cancellationToken)
{
var result = await service.RemoveParentTag(tagId, parentTagId, cancellationToken).ConfigureAwait(false); ;
return Ok(result);
}
}
}

View File

@@ -0,0 +1,154 @@
using Asp.Versioning;
using Core.Adapters.Lib;
using Core.Blueprint.Logging;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
using Core.Inventory.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Inventory.DAL.API.Controllers
{
/// <summary>
/// Handles all requests for TagType authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
[AllowAnonymous]
public class TagTypeController(ITagTypeProvider service) : ControllerBase
{
/// <summary>
/// Gets all the TagTypes.
/// </summary>
/// <returns>The <see cref="IEnumerable{TagTypeAdapter}"/> found entities.</returns>
/// <response code="200">The tagTypes found.</response>
/// <response code="404">The tagTypes not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagTypeAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagTypesAsync(CancellationToken cancellationToken)
{
var result = await service.GetAllTagTypes(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets all the TagTypes by TagType identifiers.
/// </summary>
/// <param name="TagTypes">The list of TagType identifiers.</param>
/// <returns>The <see cref="IEnumerable{TagTypeAdapter}"/> found entities.</returns>
/// <response code="200">The TagTypes found.</response>
/// <response code="404">The TagTypes not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route("GetTagTypeList")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagTypeAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagTypesByList([FromBody] string[] tagTypes, CancellationToken cancellationToken)
{
if (tagTypes == null || !tagTypes.Any())
{
return BadRequest("TagType identifiers are required.");
}
var result = await service.GetAllTagTypesByList(tagTypes, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets the TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>The <see cref="TagTypeAdapter"/> found entity.</returns>
/// <response code="200">The TagType found.</response>
/// <response code="404">The TagType not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> GetTagTypeByIdAsync([FromRoute] string id, CancellationToken cancellationToken)
{
var result = await service.GetTagTypeById(id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return NotFound("Entity not found");
}
return Ok(result);
}
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="newTagType">The TagType to be added.</param>
/// <returns>The <see cref="TagTypeAdapter"/> created entity.</returns>
/// <response code="201">The TagType created.</response>
/// <response code="422">The TagType could not be created.</response>
/// <response code="500">The service internal e|ror.</response>
[HttpPost]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status201Created)]
public async Task<IActionResult> CreateTagTypeAsync([FromBody] TagTypeRequest newTagType, CancellationToken cancellationToken)
{
var result = await service.CreateTagType(newTagType, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
/// <summary>
/// Updates a full TagType by identifier.
/// </summary>
/// <param name="entity">The TagType to update.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <response code="200">The TagType updated.</response>
/// <response code="404">The TagType not found.</response>
/// <response code="422">The TagType could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> UpdateTagTypeAsync([FromRoute] string id, TagTypeAdapter entity, CancellationToken cancellationToken)
{
if (id != entity.Id?.ToString())
{
return BadRequest("TagType ID mismatch");
}
var result = await service.UpdateTagType(entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Changes the status of the TagType.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <param name="newStatus">The new status of the TagType.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <response code="200">The TagType updates.</response>
/// <response code="404">The TagType not found.</response>
/// <response code="422">The TagType could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangeTagTypeStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{
var result = await service.ChangeTagTypeStatus(id, newStatus, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
}
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,67 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.Text.Json.Serialization;
namespace Core.Inventory.Domain.Contexts.Inventory.Request
{
/// <summary>
/// Data transfer object (DTO) for adding Tag.
/// </summary>
public class TagRequest
{
/// <summary>
/// Gets or sets the tenantId of the Tag.
/// </summary>
[BsonElement("tenantId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("tenantId")]
public string TenantId { get; set; } = null!;
/// <summary>
/// Gets or sets the name of the Tag.
/// </summary>
[BsonElement("tagName")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("tagName")]
public string TagName { get; set; } = null!;
/// <summary>
/// Gets or sets the typeId of the Tag.
/// </summary>
[BsonElement("typeId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("typeId")]
public string TypeId { get; set; } = null!;
/// <summary>
/// Gets or sets the parentTagId of the Tag.
/// </summary>
[BsonElement("parentTagId")]
[JsonPropertyName("parentTagId")]
public string[] ParentTagId { get; set; } = null!;
/// <summary>
/// Gets or sets the slug of the Tag.
/// </summary>
[BsonElement("slug")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("slug")]
public string Slug { get; set; } = null!;
/// <summary>
/// Gets or sets the displayOrder of the Tag.
/// </summary>
[BsonElement("displayOrder")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("displayOrder")]
public int DisplayOrder { get; set; }
/// <summary>
/// Gets or sets the icon of the Tag.
/// </summary>
[BsonElement("icon")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("icon")]
public string Icon { get; set; } = null!;
}
}

View File

@@ -0,0 +1,44 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.Text.Json.Serialization;
namespace Core.Inventory.Domain.Contexts.Inventory.Request
{
/// <summary>
/// Data transfer object (DTO) for adding tagType.
/// </summary>
public class TagTypeRequest
{
/// <summary>
/// Gets or sets the tenantId of the tagType.
/// </summary>
[BsonElement("tenantId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("tenantId")]
public string TenantId { get; set; } = null!;
/// <summary>
/// Gets or sets the typeName of the tagType.
/// </summary>
[BsonElement("typeName")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("typeName")]
public string TypeName { get; set; } = null!;
/// <summary>
/// Gets or sets the level of the tagType.
/// </summary>
[BsonElement("level")]
[BsonRepresentation(BsonType.Int32)]
[JsonPropertyName("level")]
public int Level { get; set; }
/// <summary>
/// Gets or sets the parentTypeId of the tagType.
/// </summary>
[BsonElement("parentTypeId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("parentTypeId")]
public string ParentTypeId { get; set; } = null!;
}
}

View File

@@ -25,10 +25,10 @@ namespace Core.Inventory.Provider.Contracts
/// <summary>
/// Gets a furniture base entity by its ID.
/// </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>
/// <returns>The corresponding <see cref="FurnitureBase"/>.</returns>
ValueTask<FurnitureBase> GetByIdAsync(string id, CancellationToken cancellationToken);
ValueTask<FurnitureBase> GetByIdAsync(string _id, CancellationToken cancellationToken);
/// <summary>
/// Retrieves all furniture base entries.
@@ -49,10 +49,10 @@ namespace Core.Inventory.Provider.Contracts
/// <summary>
/// Changes the status of a furniture base entity.
/// </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="cancellationToken">Cancellation token.</param>
/// <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>
/// Gets a furniture variant entity by its ID.
/// </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>
/// <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>
/// Retrieves all furniture variants associated with a base model.
@@ -50,10 +58,17 @@ namespace Core.Inventory.Provider.Contracts
/// <summary>
/// Changes the status of a furniture variant entity.
/// </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="cancellationToken">Cancellation token.</param>
/// <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

@@ -0,0 +1,75 @@
using Core.Adapters.Lib;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
namespace Core.Inventory.Provider.Contracts
{
public interface ITagProvider
{
/// <summary>
/// Creates a new Tag.
/// </summary>
/// <param name="entity">The Tag to be created.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> CreateTag(TagRequest newTag, CancellationToken cancellationToken);
/// <summary>
/// Gets an Tag by identifier.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> GetTagById(string _id, CancellationToken cancellationToken);
/// <summary>
/// Gets all the Tags.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagAdapter>> GetAllTags(CancellationToken cancellationToken);
/// <summary>
/// Gets all the Tags by Tags identifier list.
/// </summary>
/// <param name="Tags">The list of Tags identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagAdapter>> GetAllTagsByList(string[] Tags, CancellationToken cancellationToken);
/// <summary>
/// Changes the status of the Tag.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <param name="newStatus">The new status of the Tag.</param>
/// <returns>The <see cref="TagAdapter"/> updated entity.</returns>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> ChangeTagStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken);
/// <summary>
/// Updates a Tag by id.
/// </summary>
/// <param name="entity">The Tag to be updated.</param>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> UpdateTag(TagAdapter entity, CancellationToken cancellationToken);
/// <summary>
/// Adds a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
ValueTask<TagAdapter> AddParentTag(string tagId, string parentTagId, CancellationToken cancellationToken);
/// <summary>
/// Removes a parentTag from the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
ValueTask<TagAdapter> RemoveParentTag(string tagId, string parentTagId, CancellationToken cancellationToken);
}
}

View File

@@ -0,0 +1,59 @@
using Core.Adapters.Lib;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
namespace Core.Inventory.Provider.Contracts
{
public interface ITagTypeProvider
{
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="entity">The TagType to be created.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> CreateTagType(TagTypeRequest newTagType, CancellationToken cancellationToken);
/// <summary>
/// Gets an TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> GetTagTypeById(string _id, CancellationToken cancellationToken);
/// <summary>
/// Gets all the tagTypes.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagTypeAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypes(CancellationToken cancellationToken);
/// <summary>
/// Gets all the tagTypes by tagTypes identifier list.
/// </summary>
/// <param name="tagTypes">The list of tagTypes identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypesByList(string[] TagTypes, CancellationToken cancellationToken);
/// <summary>
/// Changes the status of the tagType.
/// </summary>
/// <param name="id">The tagType identifier.</param>
/// <param name="newStatus">The new status of the tagType.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> ChangeTagTypeStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken);
/// <summary>
/// Updates a TagType by id.
/// </summary>
/// <param name="entity">The TagType to be updated.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> UpdateTagType(TagTypeAdapter entity, CancellationToken cancellationToken);
}
}

View File

@@ -7,9 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Adapters.Lib" Version="1.0.3" />
<PackageReference Include="Adapters.Lib" Version="1.0.10" />
<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" />
</ItemGroup>

View File

@@ -22,6 +22,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
private readonly CollectionRepository<FurnitureBase> repository;
private readonly IRedisCacheProvider cacheProvider;
private readonly CacheSettings cacheSettings;
private const string getAllCache = "GetAllFurnitureBases";
public FurnitureBaseProvider(
CollectionRepository<FurnitureBase> repository,
@@ -38,15 +39,16 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary>
/// Changes the status of a FurnitureBase entity.
/// </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="cancellationToken">Cancellation token.</param>
/// <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;
await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity;
}
@@ -60,6 +62,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
{
var furnitureCollection = newFurniture.Adapt<FurnitureBase>();
await repository.InsertOneAsync(furnitureCollection);
await ResetCollectionCache();
return furnitureCollection;
}
@@ -70,7 +73,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <returns>A list of <see cref="FurnitureBase"/>.</returns>
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) ?? [];
if (cachedData.Any()) return cachedData;
@@ -83,17 +86,17 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary>
/// Gets a FurnitureBase entity by its ID.
/// </summary>
/// <param name="id">The furniture base identifier.</param>
/// <param name="mongoId">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)
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);
if (cached is not null) return cached;
var result = await repository.FindByIdAsync(id);
var result = await repository.FindByIdAsync(mongoId);
await cacheProvider.SetAsync(cacheKey, result);
return result;
}
@@ -108,7 +111,20 @@ namespace Core.Inventory.Provider.Providers.Inventory
public async ValueTask<FurnitureBase> UpdateAsync(string id, FurnitureBase entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
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 Mapster;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace Core.Inventory.Provider.Providers.Inventory
{
@@ -22,6 +23,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
private readonly CollectionRepository<FurnitureVariant> repository;
private readonly IRedisCacheProvider cacheProvider;
private readonly CacheSettings cacheSettings;
private const string getAllVariantsCache = "GetAllFurnitureVariants";
public FurnitureVariantProvider(
CollectionRepository<FurnitureVariant> repository,
@@ -38,15 +40,16 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <summary>
/// Changes the status of a FurnitureVariant entity.
/// </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="cancellationToken">Cancellation token.</param>
/// <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;
await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
return entity;
}
@@ -60,6 +63,7 @@ namespace Core.Inventory.Provider.Providers.Inventory
{
var variantCollection = newVariant.Adapt<FurnitureVariant>();
await repository.InsertOneAsync(variantCollection);
await ResetCollectionCache();
return variantCollection;
}
@@ -70,34 +74,62 @@ namespace Core.Inventory.Provider.Providers.Inventory
/// <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 cacheKey = CacheKeyHelper.GenerateCacheKey(this, nameof(GetAllByModelIdAsync), modelId);
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();
await cacheProvider.SetAsync(cacheKey, data);
return data;
if (variants is not null && variants.Any())
await cacheProvider.SetAsync(cacheKey, variants);
return variants ?? [];
}
/// <summary>
/// Gets a FurnitureVariant entity by its ID.
/// </summary>
/// <param name="id">The furniture variant identifier.</param>
/// <param name="mongoId">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)
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);
if (cached is not null) return cached;
var result = await repository.FindByIdAsync(id);
var result = await repository.FindByIdAsync(mongoId);
await cacheProvider.SetAsync(cacheKey, 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>
/// Updates a FurnitureVariant entity by ID.
/// </summary>
@@ -108,7 +140,37 @@ namespace Core.Inventory.Provider.Providers.Inventory
public async ValueTask<FurnitureVariant> UpdateAsync(string id, FurnitureVariant entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
await ResetCollectionCache();
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);
}
}
}

View File

@@ -0,0 +1,192 @@
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;
using MongoDB.Driver;
namespace Core.Inventory.Provider.Providers.Inventory
{
/// <summary>
/// Handles all services and business rules related to <see cref="TagAdapter"/>.
/// </summary>
public class TagProvider : ITagProvider
{
private readonly CollectionRepository<TagAdapter> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public TagProvider(CollectionRepository<TagAdapter> repository,
IRedisCacheProvider cacheProvider,
IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
/// <summary>
/// Creates a new Tag.
/// </summary>
/// <param name="entity">The Tag to be created.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> CreateTag(TagRequest newTag, CancellationToken cancellationToken)
{
var tagCollection = newTag.Adapt<TagAdapter>();
await repository.InsertOneAsync(tagCollection);
return tagCollection;
}
/// <summary>
/// Gets an Tag by identifier.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>0
public async ValueTask<TagAdapter> GetTagById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagById", _id);
var cachedData = await cacheProvider.GetAsync<TagAdapter>(cacheKey);
if (cachedData is not null) { return cachedData; }
var tag = await repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, tag);
return tag;
}
/// <summary>
/// Gets all the Tags.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagAdapter>> GetAllTags(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTags");
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var tags = await repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, tags);
return tags;
}
/// <summary>
/// Gets all the Tags by Tags identifier list.
/// </summary>
/// <param name="Tags">The list of Tags identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagAdapter>> GetAllTagsByList(string[] tags, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllTagsByList", tags);
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var builder = Builders<TagAdapter>.Filter;
var filters = new List<FilterDefinition<TagAdapter>>();
if (tags != null || !tags.Any())
{
filters.Add(builder.In(x => x._Id, tags));
}
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
var TagsList = await repository.FilterByMongoFilterAsync(finalFilter);
await cacheProvider.SetAsync(cacheKey, TagsList);
return TagsList;
}
/// <summary>
/// Changes the status of the Tag.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <param name="newStatus">The new status of the Tag.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> ChangeTagStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
{
var entity = await repository.FindByIdAsync(id);
entity.Status = newStatus;
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Updates a Tag by id.
/// </summary>
/// <param name="entity">The Tag to be updated.</param>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> UpdateTag(TagAdapter entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Adds a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
public async ValueTask<TagAdapter> AddParentTag(string tagId, string parentTagId, CancellationToken cancellationToken)
{
var tag = await repository.FindOneAsync(
u => u._Id == tagId &&
u.Status == StatusEnum.Active);
var updatedParentTags = tag.ParentTagId.Append(parentTagId).Distinct().ToArray();
tag.ParentTagId = updatedParentTags;
await repository.ReplaceOneAsync(tag);
return tag;
}
/// <summary>
/// Remove a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be removed.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
public async ValueTask<TagAdapter> RemoveParentTag(string tagId, string parentTagId, CancellationToken cancellationToken)
{
var tag = await repository.FindOneAsync(
u => u._Id == tagId &&
u.Status == StatusEnum.Active);
var updatedParentTags = tag.ParentTagId
?.Where(c => c != parentTagId)
.ToArray();
tag.ParentTagId = updatedParentTags;
await repository.ReplaceOneAsync(tag);
return tag;
}
}
}

View File

@@ -0,0 +1,149 @@
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;
using MongoDB.Driver;
namespace Core.Inventory.Provider.Providers.Inventory
{
/// <summary>
/// Handles all services and business rules related to <see cref="TagTypeAdapter"/>.
/// </summary>
public class TagTypeProvider : ITagTypeProvider
{
private readonly CollectionRepository<TagTypeAdapter> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public TagTypeProvider(CollectionRepository<TagTypeAdapter> repository,
IRedisCacheProvider cacheProvider,
IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="entity">The TagType to be created.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> CreateTagType(TagTypeRequest newTagType, CancellationToken cancellationToken)
{
var tagTypeCollection = newTagType.Adapt<TagTypeAdapter>();
await repository.InsertOneAsync(tagTypeCollection);
return tagTypeCollection;
}
/// <summary>
/// Gets an TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>0
public async ValueTask<TagTypeAdapter> GetTagTypeById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagTypeById", _id);
var cachedData = await cacheProvider.GetAsync<TagTypeAdapter>(cacheKey);
if (cachedData is not null) { return cachedData; }
var tagType = await repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, tagType);
return tagType;
}
/// <summary>
/// Gets all the TagTypes.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagTypeAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypes(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagTypes");
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagTypeAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var tagTypes = await repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, tagTypes);
return tagTypes;
}
/// <summary>
/// Gets all the TagTypes by TagTypes identifier list.
/// </summary>
/// <param name="TagTypes">The list of TagTypes identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypesByList(string[] tagTypes, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllTagTypesByList", tagTypes);
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagTypeAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var builder = Builders<TagTypeAdapter>.Filter;
var filters = new List<FilterDefinition<TagTypeAdapter>>();
if (tagTypes != null || !tagTypes.Any())
{
filters.Add(builder.In(x => x._Id, tagTypes));
}
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
var tagTypesList = await repository.FilterByMongoFilterAsync(finalFilter);
await cacheProvider.SetAsync(cacheKey, tagTypesList);
return tagTypesList;
}
/// <summary>
/// Changes the status of the TagType.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <param name="newStatus">The new status of the TagType.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> ChangeTagTypeStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
{
var entity = await repository.FindByIdAsync(id);
entity.Status = newStatus;
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Updates a TagType by id.
/// </summary>
/// <param name="entity">The TagType to be updated.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> UpdateTagType(TagTypeAdapter entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
return entity;
}
}
}

View File

@@ -17,6 +17,12 @@ namespace Core.Inventory.Provider
services.AddScoped<IFurnitureVariantProvider, FurnitureVariantProvider>();
services.AddScoped<CollectionRepository<FurnitureVariant>>();
services.AddScoped<ITagTypeProvider, TagTypeProvider>();
services.AddScoped<CollectionRepository<TagTypeAdapter>>();
services.AddScoped<ITagProvider, TagProvider>();
services.AddScoped<CollectionRepository<TagAdapter>>();
return services;
}