AdminShippingMethod

This commit is contained in:
Tizian.Breuch
2025-09-25 14:45:30 +02:00
parent f5543d35b4
commit 8f222ef4aa
3 changed files with 123 additions and 54 deletions

View File

@@ -1,9 +1,11 @@
// src/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs // src/Webshop.Api/Controllers/Admin/AdminShippingMethodsController.cs
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Shipping; using Webshop.Application.DTOs.Shipping;
using Webshop.Application.Services.Admin; using Webshop.Application.Services.Admin;
@@ -21,43 +23,90 @@ namespace Webshop.Api.Controllers.Admin
_shippingMethodService = shippingMethodService; _shippingMethodService = shippingMethodService;
} }
[HttpPost] [HttpGet]
public async Task<ActionResult<ShippingMethodDto>> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto) [ProducesResponseType(typeof(IEnumerable<ShippingMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllShippingMethods()
{ {
var createdMethod = await _shippingMethodService.CreateAsync(shippingMethodDto); var result = await _shippingMethodService.GetAllAsync();
return CreatedAtAction(nameof(GetShippingMethodById), new { id = createdMethod.Id }, createdMethod); return Ok(result.Value);
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<ActionResult<ShippingMethodDto>> GetShippingMethodById(Guid id) [ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetShippingMethodById(Guid id)
{ {
var method = await _shippingMethodService.GetByIdAsync(id); var result = await _shippingMethodService.GetByIdAsync(id);
if (method == null) return NotFound();
return Ok(method); 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] [HttpPost]
public async Task<ActionResult<IEnumerable<ShippingMethodDto>>> GetAllShippingMethods() [ProducesResponseType(typeof(ShippingMethodDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
public async Task<IActionResult> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
{ {
var methods = await _shippingMethodService.GetAllAsync(); if (!ModelState.IsValid)
return Ok(methods); {
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}")] [HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
public async Task<IActionResult> UpdateShippingMethod(Guid id, [FromBody] ShippingMethodDto shippingMethodDto) public async Task<IActionResult> UpdateShippingMethod(Guid id, [FromBody] ShippingMethodDto shippingMethodDto)
{ {
if (id != shippingMethodDto.Id) return BadRequest(); if (id != shippingMethodDto.Id)
var success = await _shippingMethodService.UpdateAsync(shippingMethodDto); {
if (!success) return NotFound(); return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." });
return NoContent(); }
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}")] [HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteShippingMethod(Guid id) public async Task<IActionResult> DeleteShippingMethod(Guid id)
{ {
var success = await _shippingMethodService.DeleteAsync(id); var result = await _shippingMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent(); 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." })
};
} }
} }
} }

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Shipping; using Webshop.Application.DTOs.Shipping;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces; using Webshop.Domain.Interfaces;
@@ -18,38 +19,31 @@ namespace Webshop.Application.Services.Admin
_shippingMethodRepository = shippingMethodRepository; _shippingMethodRepository = shippingMethodRepository;
} }
public async Task<IEnumerable<ShippingMethodDto>> GetAllAsync() public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync()
{ {
var methods = await _shippingMethodRepository.GetAllAsync(); var methods = await _shippingMethodRepository.GetAllAsync();
return methods.Select(sm => new ShippingMethodDto var dtos = methods.Select(MapToDto).ToList();
{ return ServiceResult.Ok<IEnumerable<ShippingMethodDto>>(dtos);
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();
} }
public async Task<ShippingMethodDto?> GetByIdAsync(Guid id) public async Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id)
{ {
var sm = await _shippingMethodRepository.GetByIdAsync(id); var sm = await _shippingMethodRepository.GetByIdAsync(id);
if (sm == null) return null; if (sm == null)
return new ShippingMethodDto
{ {
Id = sm.Id, return ServiceResult.Fail<ShippingMethodDto>(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
Name = sm.Name, }
Description = sm.Description, return ServiceResult.Ok(MapToDto(sm));
Cost = sm.BaseCost,
IsActive = sm.IsActive
};
} }
public async Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto) public async Task<ServiceResult<ShippingMethodDto>> CreateAsync(ShippingMethodDto shippingMethodDto)
{ {
var allMethods = await _shippingMethodRepository.GetAllAsync();
if (allMethods.Any(sm => sm.Name.Equals(shippingMethodDto.Name, StringComparison.OrdinalIgnoreCase)))
{
return ServiceResult.Fail<ShippingMethodDto>(ServiceResultType.Conflict, $"Eine Versandmethode mit dem Namen '{shippingMethodDto.Name}' existiert bereits.");
}
var newMethod = new ShippingMethod var newMethod = new ShippingMethod
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
@@ -60,14 +54,24 @@ namespace Webshop.Application.Services.Admin
}; };
await _shippingMethodRepository.AddAsync(newMethod); await _shippingMethodRepository.AddAsync(newMethod);
shippingMethodDto.Id = newMethod.Id; shippingMethodDto.Id = newMethod.Id;
return shippingMethodDto; return ServiceResult.Ok(shippingMethodDto);
} }
public async Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto) public async Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto)
{ {
var existingMethod = await _shippingMethodRepository.GetByIdAsync(shippingMethodDto.Id); 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.Name = shippingMethodDto.Name;
existingMethod.Description = shippingMethodDto.Description; existingMethod.Description = shippingMethodDto.Description;
@@ -75,16 +79,31 @@ namespace Webshop.Application.Services.Admin
existingMethod.IsActive = shippingMethodDto.IsActive; existingMethod.IsActive = shippingMethodDto.IsActive;
await _shippingMethodRepository.UpdateAsync(existingMethod); await _shippingMethodRepository.UpdateAsync(existingMethod);
return true; return ServiceResult.Ok();
} }
public async Task<bool> DeleteAsync(Guid id) public async Task<ServiceResult> DeleteAsync(Guid id)
{ {
var method = await _shippingMethodRepository.GetByIdAsync(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); 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
};
} }
} }
} }

View File

@@ -2,16 +2,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; 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 namespace Webshop.Application.Services.Admin
{ {
public interface IAdminShippingMethodService public interface IAdminShippingMethodService
{ {
Task<IEnumerable<ShippingMethodDto>> GetAllAsync(); Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync();
Task<ShippingMethodDto?> GetByIdAsync(Guid id); Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id);
Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto); Task<ServiceResult<ShippingMethodDto>> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto); Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> DeleteAsync(Guid id); Task<ServiceResult> DeleteAsync(Guid id);
} }
} }