From 38f097f5e62ed3c8abcab487d2acb591771139b8 Mon Sep 17 00:00:00 2001 From: Oscar Morales Date: Tue, 5 Aug 2025 12:29:28 -0600 Subject: [PATCH] Add TagType CRUD --- .../Controllers/TagOverrideController.cs | 154 ++++++++++++++++++ .../Inventory/Request/TagOverrideRequest.cs | 36 ++++ .../Contracts/ITagOverrideProvider.cs | 59 +++++++ .../Inventory/TagOverrideProvider.cs | 149 +++++++++++++++++ .../ServiceCollectionExtensions.cs | 3 + 5 files changed, 401 insertions(+) create mode 100644 Core.Inventory.DAL.API/Controllers/TagOverrideController.cs create mode 100644 Core.Inventory.Domain/Contexts/Inventory/Request/TagOverrideRequest.cs create mode 100644 Core.Inventory.Provider/Contracts/ITagOverrideProvider.cs create mode 100644 Core.Inventory.Provider/Providers/Inventory/TagOverrideProvider.cs diff --git a/Core.Inventory.DAL.API/Controllers/TagOverrideController.cs b/Core.Inventory.DAL.API/Controllers/TagOverrideController.cs new file mode 100644 index 0000000..cf9d423 --- /dev/null +++ b/Core.Inventory.DAL.API/Controllers/TagOverrideController.cs @@ -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 +{ + /// + /// Handles all requests for TagOverride authentication. + /// + [ApiVersion(MimeTypes.ApplicationVersion)] + [Route("api/v{api-version:apiVersion}/[controller]")] + [Produces(MimeTypes.ApplicationJson)] + [Consumes(MimeTypes.ApplicationJson)] + [ApiController] + [AllowAnonymous] + public class TagOverrideController(ITagOverrideProvider service) : ControllerBase + { + /// + /// Gets all the TagOverrides. + /// + /// The found entities. + /// The TagOverrides found. + /// The TagOverrides not found error. + /// The service internal error. + [HttpGet] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllTagOverridesAsync(CancellationToken cancellationToken) + { + var result = await service.GetAllTagOverrides(cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Gets all the TagOverrides by TagOverride identifiers. + /// + /// The list of TagOverride identifiers. + /// The found entities. + /// The TagOverrides found. + /// The TagOverrides not found error. + /// The service internal error. + [HttpPost] + [Route("GetTagOverrideList")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllTagOverridesByList([FromBody] string[] tagOverrides, CancellationToken cancellationToken) + { + if (tagOverrides == null || !tagOverrides.Any()) + { + return BadRequest("TagOverride identifiers are required."); + } + + var result = await service.GetAllTagOverridesByList(tagOverrides, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + + /// + /// Gets the TagOverride by identifier. + /// + /// The TagOverride identifier. + /// The found entity. + /// The TagOverride found. + /// The TagOverride not found error. + /// The service internal error. + [HttpGet] + [Route("{id}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TagOverrideAdapter), StatusCodes.Status200OK)] + public async Task GetTagOverrideByIdAsync([FromRoute] string id, CancellationToken cancellationToken) + { + var result = await service.GetTagOverrideById(id, cancellationToken).ConfigureAwait(false); + + if (result == null) + { + return NotFound("Entity not found"); + } + + return Ok(result); + } + + /// + /// Creates a new TagOverride. + /// + /// The TagOverride to be added. + /// The created entity. + /// The TagOverride created. + /// The TagOverride could not be created. + /// The service internal e|ror. + [HttpPost] + [ProducesResponseType(typeof(TagOverrideAdapter), StatusCodes.Status201Created)] + public async Task CreateTagOverrideAsync([FromBody] TagOverrideRequest newTagOverride, CancellationToken cancellationToken) + { + var result = await service.CreateTagOverride(newTagOverride, cancellationToken).ConfigureAwait(false); + return Created("CreatedWithIdAsync", result); + } + + /// + /// Updates a full TagOverride by identifier. + /// + /// The TagOverride to update. + /// The TagOverride identifier. + /// The updated entity. + /// The TagOverride updated. + /// The TagOverride not found. + /// The TagOverride could not be updated. + /// The service internal error. + [HttpPut] + [Route("{id}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TagOverrideAdapter), StatusCodes.Status200OK)] + public async Task UpdateTagOverrideAsync([FromRoute] string id, TagOverrideAdapter entity, CancellationToken cancellationToken) + { + if (id != entity.Id?.ToString()) + { + return BadRequest("TagOverride ID mismatch"); + } + + var result = await service.UpdateTagOverride(entity, cancellationToken).ConfigureAwait(false); + + return Ok(result); + } + + /// + /// Changes the status of the TagOverride. + /// + /// The TagOverride identifier. + /// The new status of the TagOverride. + /// The updated entity. + /// The TagOverride updates. + /// The TagOverride not found. + /// The TagOverride could not be deleted. + /// The service internal error. + [HttpPatch] + [Route("{id}/{newStatus}/ChangeStatus")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TagOverrideAdapter), StatusCodes.Status200OK)] + public async Task ChangeTagOverrideStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + { + var result = await service.ChangeTagOverrideStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + } +} \ No newline at end of file diff --git a/Core.Inventory.Domain/Contexts/Inventory/Request/TagOverrideRequest.cs b/Core.Inventory.Domain/Contexts/Inventory/Request/TagOverrideRequest.cs new file mode 100644 index 0000000..addbced --- /dev/null +++ b/Core.Inventory.Domain/Contexts/Inventory/Request/TagOverrideRequest.cs @@ -0,0 +1,36 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using System.Text.Json.Serialization; + +namespace Core.Inventory.Domain.Contexts.Inventory.Request +{ + /// + /// Data transfer object (DTO) for adding TagOverride. + /// + public class TagOverrideRequest + { + /// + /// Gets or sets the tenantId of the TagOverride. + /// + [BsonElement("tenantId")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("tenantId")] + public string TenantId { get; set; } = null!; + + /// + /// Gets or sets the baseTagId of the TagOverride. + /// + [BsonElement("baseTagId")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("baseTagId")] + public string BaseTagId { get; set; } = null!; + + /// + /// Gets or sets the overrideTagId of the TagOverride. + /// + [BsonElement("overrideTagId")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("overrideTagId")] + public string OverrideTagId { get; set; } = null!; + } +} diff --git a/Core.Inventory.Provider/Contracts/ITagOverrideProvider.cs b/Core.Inventory.Provider/Contracts/ITagOverrideProvider.cs new file mode 100644 index 0000000..4171cb9 --- /dev/null +++ b/Core.Inventory.Provider/Contracts/ITagOverrideProvider.cs @@ -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 ITagOverrideProvider + { + /// + /// Creates a new TagOverride. + /// + /// The TagOverride to be created. + /// A representing + /// the asynchronous execution of the service. + ValueTask CreateTagOverride(TagOverrideRequest newTagOverride, CancellationToken cancellationToken); + + /// + /// Gets an TagOverride by identifier. + /// + /// The TagOverride identifier. + /// A representing + /// the asynchronous execution of the service. + ValueTask GetTagOverrideById(string _id, CancellationToken cancellationToken); + + /// + /// Gets all the TagOverrides. + /// + /// A representing + /// the asynchronous execution of the service. + ValueTask> GetAllTagOverrides(CancellationToken cancellationToken); + + /// + /// Gets all the TagOverrides by TagOverrides identifier list. + /// + /// The list of TagOverrides identifiers. + /// A representing + /// the asynchronous execution of the service. + ValueTask> GetAllTagOverridesByList(string[] tagOverrides, CancellationToken cancellationToken); + + /// + /// Changes the status of the TagOverride. + /// + /// The TagOverride identifier. + /// The new status of the TagOverride. + /// The updated entity. + /// A representing + /// the asynchronous execution of the service. + ValueTask ChangeTagOverrideStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken); + + /// + /// Updates a TagOverride by id. + /// + /// The TagOverride to be updated. + /// The TagOverride identifier. + /// A representing + /// the asynchronous execution of the service. + ValueTask UpdateTagOverride(TagOverrideAdapter entity, CancellationToken cancellationToken); + } +} diff --git a/Core.Inventory.Provider/Providers/Inventory/TagOverrideProvider.cs b/Core.Inventory.Provider/Providers/Inventory/TagOverrideProvider.cs new file mode 100644 index 0000000..f142653 --- /dev/null +++ b/Core.Inventory.Provider/Providers/Inventory/TagOverrideProvider.cs @@ -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 +{ + /// + /// Handles all services and business rules related to . + /// + public class TagOverrideProvider : ITagOverrideProvider + { + private readonly CollectionRepository repository; + private readonly CacheSettings cacheSettings; + private readonly IRedisCacheProvider cacheProvider; + + public TagOverrideProvider(CollectionRepository repository, + IRedisCacheProvider cacheProvider, + IOptions cacheSettings) + { + this.repository = repository; + this.repository.CollectionInitialization(); + this.cacheSettings = cacheSettings.Value; + this.cacheProvider = cacheProvider; + } + + /// + /// Creates a new TagOverride. + /// + /// The TagOverride to be created. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask CreateTagOverride(TagOverrideRequest newTagOverride, CancellationToken cancellationToken) + { + var tagOverrideCollection = newTagOverride.Adapt(); + + await repository.InsertOneAsync(tagOverrideCollection); + + return tagOverrideCollection; + } + + /// + /// Gets an TagOverride by identifier. + /// + /// The TagOverride identifier. + /// A representing + /// the asynchronous execution of the service.0 + public async ValueTask GetTagOverrideById(string _id, CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagOverrideById", _id); + var cachedData = await cacheProvider.GetAsync(cacheKey); + + if (cachedData is not null) { return cachedData; } + + var TagOverride = await repository.FindByIdAsync(_id); + + await cacheProvider.SetAsync(cacheKey, TagOverride); + + return TagOverride; + } + + /// + /// Gets all the TagOverrides. + /// + /// A representing + /// the asynchronous execution of the service. + public async ValueTask> GetAllTagOverrides(CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagOverrides"); + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var TagOverrides = await repository.AsQueryable(); + + await cacheProvider.SetAsync(cacheKey, TagOverrides); + + return TagOverrides; + } + + /// + /// Gets all the TagOverrides by TagOverrides identifier list. + /// + /// The list of TagOverrides identifiers. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask> GetAllTagOverridesByList(string[] tagOverrides, CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllTagOverridesByList", tagOverrides); + + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var builder = Builders.Filter; + var filters = new List>(); + + if (tagOverrides != null || !tagOverrides.Any()) + { + filters.Add(builder.In(x => x._Id, tagOverrides)); + } + + var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; + + var TagOverridesList = await repository.FilterByMongoFilterAsync(finalFilter); + + await cacheProvider.SetAsync(cacheKey, TagOverridesList); + + return TagOverridesList; + } + + + /// + /// Changes the status of the TagOverride. + /// + /// The TagOverride identifier. + /// The new status of the TagOverride. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask ChangeTagOverrideStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken) + { + var entity = await repository.FindByIdAsync(id); + entity.Status = newStatus; + + await repository.ReplaceOneAsync(entity); + + return entity; + } + + /// + /// Updates a TagOverride by id. + /// + /// The TagOverride to be updated. + /// The TagOverride identifier. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask UpdateTagOverride(TagOverrideAdapter entity, CancellationToken cancellationToken) + { + await repository.ReplaceOneAsync(entity); + + return entity; + } + } +} \ No newline at end of file diff --git a/Core.Inventory.Provider/ServiceCollectionExtensions.cs b/Core.Inventory.Provider/ServiceCollectionExtensions.cs index 60d28c9..3b3f660 100644 --- a/Core.Inventory.Provider/ServiceCollectionExtensions.cs +++ b/Core.Inventory.Provider/ServiceCollectionExtensions.cs @@ -24,6 +24,9 @@ namespace Core.Inventory.Provider services.AddScoped(); services.AddScoped>(); + services.AddScoped(); + services.AddScoped>(); + services.AddScoped(); services.AddScoped>();