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
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<ActionResult<ShippingMethodDto>> CreateShippingMethod([FromBody] ShippingMethodDto shippingMethodDto)
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ShippingMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> 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<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);
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<ActionResult<IEnumerable<ShippingMethodDto>>> GetAllShippingMethods()
[HttpPost]
[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();
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<IActionResult> 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<IActionResult> 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." })
};
}
}
}

View File

@@ -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<IEnumerable<ShippingMethodDto>> GetAllAsync()
public async Task<ServiceResult<IEnumerable<ShippingMethodDto>>> 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<IEnumerable<ShippingMethodDto>>(dtos);
}
public async Task<ShippingMethodDto?> GetByIdAsync(Guid id)
public async Task<ServiceResult<ShippingMethodDto>> 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<ShippingMethodDto>(ServiceResultType.NotFound, $"Versandmethode mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToDto(sm));
}
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
{
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<bool> UpdateAsync(ShippingMethodDto shippingMethodDto)
public async Task<ServiceResult> 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<bool> DeleteAsync(Guid id)
public async Task<ServiceResult> 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
};
}
}
}

View File

@@ -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<IEnumerable<ShippingMethodDto>> GetAllAsync();
Task<ShippingMethodDto?> GetByIdAsync(Guid id);
Task<ShippingMethodDto> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<bool> DeleteAsync(Guid id);
Task<ServiceResult<IEnumerable<ShippingMethodDto>>> GetAllAsync();
Task<ServiceResult<ShippingMethodDto>> GetByIdAsync(Guid id);
Task<ServiceResult<ShippingMethodDto>> CreateAsync(ShippingMethodDto shippingMethodDto);
Task<ServiceResult> UpdateAsync(ShippingMethodDto shippingMethodDto);
Task<ServiceResult> DeleteAsync(Guid id);
}
}