diff --git a/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs b/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs index 52c175c..f2a367c 100644 --- a/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs +++ b/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs @@ -1,9 +1,11 @@ // src/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs.Shipping; using Webshop.Application.Services.Admin; @@ -21,43 +23,90 @@ namespace Webshop.Api.Controllers.Admin _shippingMethodService = shippingMethodService; } - [HttpPost] - public async Task> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto) + [HttpGet] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllShippingMethods() { - var createdMethod = await _shippingMethodService.CreateAsync(shippingMethodDto); - return CreatedAtAction(nameof(GetShippingMethodById), new { id = createdMethod.Id }, createdMethod); + var result = await _shippingMethodService.GetAllAsync(); + return Ok(result.Value); } [HttpGet("{id}")] - public async Task> GetShippingMethodById(Guid id) + [ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] + public async Task GetShippingMethodById(Guid id) { - var method = await _shippingMethodService.GetByIdAsync(id); - if (method == null) return NotFound(); - return Ok(method); + var result = await _shippingMethodService.GetByIdAsync(id); + + return result.Type switch + { + ServiceResultType.Success => Ok(result.Value), + ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), + _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) + }; } - [HttpGet] - public async Task>> GetAllShippingMethods() + [HttpPost] + [ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status201Created)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] + public async Task CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto) { - var methods = await _shippingMethodService.GetAllAsync(); - return Ok(methods); + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + var result = await _shippingMethodService.CreateAsync(shippingMethodDto); + + return result.Type switch + { + ServiceResultType.Success => CreatedAtAction(nameof(GetShippingMethodById), new { id = result.Value!.Id }, result.Value), + ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), + _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) + }; } [HttpPut("{id}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task UpdateShippingMethod(Guid id, [FromBody] ShippingMethodDto shippingMethodDto) { - if (id != shippingMethodDto.Id) return BadRequest(); - var success = await _shippingMethodService.UpdateAsync(shippingMethodDto); - if (!success) return NotFound(); - return NoContent(); + if (id != shippingMethodDto.Id) + { + return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." }); + } + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + var result = await _shippingMethodService.UpdateAsync(shippingMethodDto); + + return result.Type switch + { + ServiceResultType.Success => NoContent(), + ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), + ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), + _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) + }; } [HttpDelete("{id}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task DeleteShippingMethod(Guid id) { - var success = await _shippingMethodService.DeleteAsync(id); - if (!success) return NotFound(); - return NoContent(); + var result = await _shippingMethodService.DeleteAsync(id); + + return result.Type switch + { + ServiceResultType.Success => NoContent(), + ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), + _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) + }; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/AdminShippingMethodService.cs b/Webshop.Application/Services/Admin/AdminShippingMethodService.cs index 4d9ea6f..0cf8c7d 100644 --- a/Webshop.Application/Services/Admin/AdminShippingMethodService.cs +++ b/Webshop.Application/Services/Admin/AdminShippingMethodService.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs.Shipping; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; @@ -18,38 +19,31 @@ namespace Webshop.Application.Services.Admin _shippingMethodRepository = shippingMethodRepository; } - public async Task> GetAllAsync() + public async Task>> GetAllAsync() { var methods = await _shippingMethodRepository.GetAllAsync(); - return methods.Select(sm => new ShippingMethodDto - { - Id = sm.Id, - Name = sm.Name, - Description = sm.Description, - Cost = sm.BaseCost, - IsActive = sm.IsActive, - MinDeliveryDays = 0, // Annahme, diese Felder existieren in Ihrer Entität - MaxDeliveryDays = 0 // Annahme, diese Felder existieren in Ihrer Entität - }).ToList(); + var dtos = methods.Select(MapToDto).ToList(); + return ServiceResult.Ok>(dtos); } - public async Task GetByIdAsync(Guid id) + public async Task> GetByIdAsync(Guid id) { var sm = await _shippingMethodRepository.GetByIdAsync(id); - if (sm == null) return null; - - return new ShippingMethodDto + if (sm == null) { - Id = sm.Id, - Name = sm.Name, - Description = sm.Description, - Cost = sm.BaseCost, - IsActive = sm.IsActive - }; + return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden."); + } + return ServiceResult.Ok(MapToDto(sm)); } - public async Task CreateAsync(ShippingMethodDto shippingMethodDto) + public async Task> CreateAsync(ShippingMethodDto shippingMethodDto) { + var allMethods = await _shippingMethodRepository.GetAllAsync(); + if (allMethods.Any(sm => sm.Name.Equals(shippingMethodDto.Name, StringComparison.OrdinalIgnoreCase))) + { + return ServiceResult.Fail(ServiceResultType.Conflict, $"Eine Versandmethode mit dem Namen '{shippingMethodDto.Name}' existiert bereits."); + } + var newMethod = new ShippingMethod { Id = Guid.NewGuid(), @@ -60,14 +54,24 @@ namespace Webshop.Application.Services.Admin }; await _shippingMethodRepository.AddAsync(newMethod); + shippingMethodDto.Id = newMethod.Id; - return shippingMethodDto; + return ServiceResult.Ok(shippingMethodDto); } - public async Task UpdateAsync(ShippingMethodDto shippingMethodDto) + public async Task UpdateAsync(ShippingMethodDto shippingMethodDto) { var existingMethod = await _shippingMethodRepository.GetByIdAsync(shippingMethodDto.Id); - if (existingMethod == null) return false; + if (existingMethod == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{shippingMethodDto.Id}' nicht gefunden."); + } + + var allMethods = await _shippingMethodRepository.GetAllAsync(); + if (allMethods.Any(sm => sm.Name.Equals(shippingMethodDto.Name, StringComparison.OrdinalIgnoreCase) && sm.Id != shippingMethodDto.Id)) + { + return ServiceResult.Fail(ServiceResultType.Conflict, $"Eine andere Versandmethode mit dem Namen '{shippingMethodDto.Name}' existiert bereits."); + } existingMethod.Name = shippingMethodDto.Name; existingMethod.Description = shippingMethodDto.Description; @@ -75,16 +79,31 @@ namespace Webshop.Application.Services.Admin existingMethod.IsActive = shippingMethodDto.IsActive; await _shippingMethodRepository.UpdateAsync(existingMethod); - return true; + return ServiceResult.Ok(); } - public async Task DeleteAsync(Guid id) + public async Task DeleteAsync(Guid id) { var method = await _shippingMethodRepository.GetByIdAsync(id); - if (method == null) return false; + if (method == null) + { + return ServiceResult.Fail(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden."); + } await _shippingMethodRepository.DeleteAsync(id); - return true; + return ServiceResult.Ok(); + } + + private ShippingMethodDto MapToDto(ShippingMethod sm) + { + return new ShippingMethodDto + { + Id = sm.Id, + Name = sm.Name, + Description = sm.Description, + Cost = sm.BaseCost, + IsActive = sm.IsActive + }; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/Interfaces/IAdminShippingMethodService.cs b/Webshop.Application/Services/Admin/Interfaces/IAdminShippingMethodService.cs index 471e718..8a93c5d 100644 --- a/Webshop.Application/Services/Admin/Interfaces/IAdminShippingMethodService.cs +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminShippingMethodService.cs @@ -2,16 +2,17 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Application.DTOs.Shipping; // Für ShippingMethodDto +using Webshop.Application; +using Webshop.Application.DTOs.Shipping; namespace Webshop.Application.Services.Admin { public interface IAdminShippingMethodService { - Task> GetAllAsync(); - Task GetByIdAsync(Guid id); - Task CreateAsync(ShippingMethodDto shippingMethodDto); - Task UpdateAsync(ShippingMethodDto shippingMethodDto); - Task DeleteAsync(Guid id); + Task>> GetAllAsync(); + Task> GetByIdAsync(Guid id); + Task> CreateAsync(ShippingMethodDto shippingMethodDto); + Task UpdateAsync(ShippingMethodDto shippingMethodDto); + Task DeleteAsync(Guid id); } } \ No newline at end of file