First version of BFF
This commit is contained in:
		
							
								
								
									
										49
									
								
								Core.Inventory.BFF.API/Controllers/BaseController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								Core.Inventory.BFF.API/Controllers/BaseController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
|  | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Newtonsoft.Json; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Inventory.BFF.API.Controllers | ||||
| { | ||||
|     [Route("api/[controller]")] | ||||
|     [ApiController] | ||||
|     public class BaseController(ILogger<BaseController> logger) : ControllerBase | ||||
|     { | ||||
|         private readonly ILogger<BaseController> _logger = logger; | ||||
|  | ||||
|         protected Guid TrackingId => (Guid)(HttpContext.Items["TrackingId"] ?? Guid.NewGuid()); | ||||
|  | ||||
|         protected async Task<IActionResult> Handle<T>(Func<Task<ApiResponse<T>>> apiCall) where T : class | ||||
|         { | ||||
|             var response = await apiCall().ConfigureAwait(false); | ||||
|  | ||||
|             _logger.LogInformation($"{TrackingId} - {response.RequestMessage?.Method} {response.RequestMessage?.RequestUri} - Status: {response.StatusCode}"); | ||||
|  | ||||
|             return FromAPIResponse(response); | ||||
|         } | ||||
|  | ||||
|         private IActionResult FromAPIResponse<T>(ApiResponse<T> response) where T : class | ||||
|         { | ||||
|             if (response.IsSuccessful) | ||||
|                 return StatusCode((int)response.StatusCode, response.Content); | ||||
|             else | ||||
|             { | ||||
|                 dynamic errorContent = string.Empty; | ||||
|  | ||||
|                 try | ||||
|                 { | ||||
|                     errorContent = JsonConvert.DeserializeObject<string>(response.Error?.Content ?? string.Empty) ?? string.Empty; | ||||
|                 } | ||||
|                 catch (Exception) | ||||
|                 { | ||||
|                     errorContent = JsonConvert.DeserializeObject<HttpError>(response.Error?.Content); | ||||
|                     if (errorContent?.Error?.ErrorCode is null && errorContent?.Error?.Message is null && errorContent?.Error?.Target is null) | ||||
|                         errorContent = JsonConvert.DeserializeObject<GenericErrorResponse>(response.Error?.Content); | ||||
|  | ||||
|                 } | ||||
|                 return StatusCode((int)response.StatusCode, errorContent); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										142
									
								
								Core.Inventory.BFF.API/Controllers/FurnitureBaseController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								Core.Inventory.BFF.API/Controllers/FurnitureBaseController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,142 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.External.Clients.Inventory; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Base; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace Core.Inventory.BFF.API.Controllers | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all requests for furniture base operations. | ||||
|     /// </summary> | ||||
|     [ApiVersion("1.0")] | ||||
|     [Route("api/v{version:apiVersion}/[controller]")] | ||||
|     [ApiController] | ||||
|     [Consumes("application/json")] | ||||
|     [Produces("application/json")] | ||||
|     public class FurnitureBaseController(IInventoryServiceClient inventoryClient, ILogger<FurnitureBaseController> logger) : BaseController(logger) | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets all furniture base records. | ||||
|         /// </summary> | ||||
|         [HttpGet("GetAll")] | ||||
|         [ProducesResponseType(typeof(IEnumerable<FurnitureBase>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetAllAsync)} - Request received."); | ||||
|                 return await Handle(() => inventoryClient.GetAllAsync(TrackingId.ToString(), cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetAllAsync)} - An error occurred."); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a furniture base by its identifier. | ||||
|         /// </summary> | ||||
|         [HttpPost("GetById")] | ||||
|         [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetByIdAsync([FromBody] GetFurnitureBaseByIdRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetByIdAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(request.MongoId)) | ||||
|                     return BadRequest("FurnitureBase MongoId is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.GetByIdAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetByIdAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new furniture base record. | ||||
|         /// </summary> | ||||
|         [HttpPost("Create")] | ||||
|         [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status201Created)] | ||||
|         public async Task<IActionResult> CreateAsync([FromBody] CreateFurnitureBaseRequest newFurniture, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(CreateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newFurniture)}"); | ||||
|  | ||||
|                 if (newFurniture == null) return BadRequest("Invalid furniture object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Name)) return BadRequest("Invalid furniture name"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Material)) return BadRequest("Invalid furniture material"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Condition)) return BadRequest("Invalid furniture condition"); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.CreateAsync(TrackingId.ToString(), newFurniture, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(CreateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newFurniture)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a furniture base record. | ||||
|         /// </summary> | ||||
|         [HttpPut("Update")] | ||||
|         [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> UpdateAsync([FromBody] UpdateFurnitureBaseRequest newFurniture, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(UpdateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newFurniture)}"); | ||||
|  | ||||
|                 if (newFurniture == null) return BadRequest("Invalid furniture object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Name)) return BadRequest("Invalid furniture name"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Material)) return BadRequest("Invalid furniture material"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newFurniture.Condition)) return BadRequest("Invalid furniture condition"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(newFurniture.Id)) return BadRequest("Id is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.UpdateFurnitureBaseAsync(TrackingId.ToString(), newFurniture, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(UpdateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newFurniture)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a furniture base. | ||||
|         /// </summary> | ||||
|         [HttpPatch("ChangeStatus")] | ||||
|         [ProducesResponseType(typeof(FurnitureBase), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> ChangeStatusAsync([FromBody] ChangeFurnitureBaseStatusRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(ChangeStatusAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(request.MongoId)) return BadRequest("Id is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.ChangeFurnitureBaseStatusAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(ChangeStatusAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										167
									
								
								Core.Inventory.BFF.API/Controllers/FurnitureVariantController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								Core.Inventory.BFF.API/Controllers/FurnitureVariantController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,167 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.External.Clients.Inventory; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Variant; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace Core.Inventory.BFF.API.Controllers | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all requests for furniture variant operations. | ||||
|     /// </summary> | ||||
|     [ApiVersion("1.0")] | ||||
|     [Route("api/v{version:apiVersion}/[controller]")] | ||||
|     [ApiController] | ||||
|     [Consumes("application/json")] | ||||
|     [Produces("application/json")] | ||||
|     public class FurnitureVariantController(IInventoryServiceClient inventoryClient, ILogger<FurnitureVariantController> logger) : BaseController(logger) | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets furniture variants by model ID. | ||||
|         /// </summary> | ||||
|         [HttpPost("GetAllByModelId")] | ||||
|         [ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllByModelIdAsync([FromBody] GetAllFurnitureVariantsByModelIdRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetAllByModelIdAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(request.ModelId)) return BadRequest("ModelId is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.GetVariantsByModelIdAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetAllByModelIdAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a furniture variant by its MongoId. | ||||
|         /// </summary> | ||||
|         [HttpPost("GetById")] | ||||
|         [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetByIdAsync([FromBody] GetFurnitureVariantByIdRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetByIdAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(request.MongoId)) return BadRequest("MongoId is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.GetFurnitureVariantByIdAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetByIdAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets furniture variants by a list of IDs. | ||||
|         /// </summary> | ||||
|         [HttpPost("GetByIds")] | ||||
|         [ProducesResponseType(typeof(IEnumerable<FurnitureVariant>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetByIdsAsync([FromBody] GetFurnitureVariantsByIdsRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetByIdsAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (request?.Ids == null || request.Ids.Count == 0) | ||||
|                     return BadRequest("At least one Id is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.GetFurnitureVariantsByIdsAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetByIdsAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new furniture variant. | ||||
|         /// </summary> | ||||
|         [HttpPost("Create")] | ||||
|         [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status201Created)] | ||||
|         public async Task<IActionResult> CreateAsync([FromBody] CreateFurnitureVariantRequest newVariant, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(CreateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newVariant)}"); | ||||
|  | ||||
|                 if (newVariant == null) return BadRequest("Invalid furniture object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.ModelId)) return BadRequest("Invalid furniture modelId"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.Name)) return BadRequest("Invalid furniture name"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.Color)) return BadRequest("Invalid furniture color"); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.CreateFurnitureVariantAsync(TrackingId.ToString(), newVariant, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(CreateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newVariant)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a furniture variant. | ||||
|         /// </summary> | ||||
|         [HttpPut("Update")] | ||||
|         [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> UpdateAsync([FromBody] UpdateFurnitureVariantRequest newVariant, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(UpdateAsync)} - Request received. Payload: {JsonSerializer.Serialize(newVariant)}"); | ||||
|  | ||||
|                 if (newVariant == null) return BadRequest("Invalid furniture object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.ModelId)) return BadRequest("Invalid furniture modelId"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.Name)) return BadRequest("Invalid furniture name"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newVariant.Color)) return BadRequest("Invalid furniture color"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(newVariant.Id)) return BadRequest("Id is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.UpdateFurnitureVariantAsync(TrackingId.ToString(), newVariant.Id, newVariant, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(UpdateAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(newVariant)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of a furniture variant. | ||||
|         /// </summary> | ||||
|         [HttpPatch("ChangeStatus")] | ||||
|         [ProducesResponseType(typeof(FurnitureVariant), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> ChangeStatusAsync([FromBody] ChangeFurnitureVariantStatusRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(ChangeStatusAsync)} - Request received. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrWhiteSpace(request.MongoId)) return BadRequest("Id is required."); | ||||
|  | ||||
|                 return await Handle(() => inventoryClient.ChangeFurnitureVariantStatusAsync(TrackingId.ToString(), request, cancellationToken)); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(ChangeStatusAsync)} - An error occurred. Payload: {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,33 +0,0 @@ | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace Core.Inventory.BFF.API.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     [Route("[controller]")] | ||||
|     public class WeatherForecastController : ControllerBase | ||||
|     { | ||||
|         private static readonly string[] Summaries = new[] | ||||
|         { | ||||
|             "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||||
|         }; | ||||
|  | ||||
|         private readonly ILogger<WeatherForecastController> _logger; | ||||
|  | ||||
|         public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||||
|         { | ||||
|             _logger = logger; | ||||
|         } | ||||
|  | ||||
|         [HttpGet(Name = "GetWeatherForecast")] | ||||
|         public IEnumerable<WeatherForecast> Get() | ||||
|         { | ||||
|             return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||||
|             { | ||||
|                 Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||||
|                 TemperatureC = Random.Shared.Next(-20, 55), | ||||
|                 Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||||
|             }) | ||||
|             .ToArray(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user