discounts überarbeitet

This commit is contained in:
Tizian.Breuch
2025-08-29 13:57:39 +02:00
parent 6e6f38397b
commit a2e3a44e94
7 changed files with 257 additions and 112 deletions

View File

@@ -1,9 +1,10 @@
// src/Webshop.Api/Controllers/Admin/AdminDiscountsController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Discounts;
using Webshop.Application.Services.Admin.Interfaces;
@@ -58,11 +59,20 @@ namespace Webshop.Api.Controllers.Admin
/// </remarks>
/// <param name="discountDto">Das Datenobjekt des zu erstellenden Rabatts.</param>
[HttpPost]
[ProducesResponseType(typeof(DiscountDto), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<DiscountDto>> CreateDiscount([FromBody] DiscountDto discountDto)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var createdDiscount = await _adminDiscountService.CreateDiscountAsync(discountDto);
return CreatedAtAction(nameof(GetDiscountById), new { id = createdDiscount.Id }, createdDiscount);
var result = await _adminDiscountService.CreateDiscountAsync(discountDto);
if (result.Type == ServiceResultType.Success)
{
return CreatedAtAction(nameof(GetDiscountById), new { id = result.Value!.Id }, result.Value);
}
return BadRequest(new { Message = result.ErrorMessage });
}
/// <summary>
@@ -71,15 +81,23 @@ namespace Webshop.Api.Controllers.Admin
/// <param name="id">Die ID des zu aktualisierenden Rabatts.</param>
/// <param name="discountDto">Die neuen Daten f<>r den Rabatt.</param>
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdateDiscount(Guid id, [FromBody] DiscountDto discountDto)
{
if (id != discountDto.Id) return BadRequest();
if (id != discountDto.Id) return BadRequest("ID in URL und Body stimmen nicht <20>berein.");
if (!ModelState.IsValid) return BadRequest(ModelState);
var success = await _adminDiscountService.UpdateDiscountAsync(discountDto);
if (!success) return NotFound();
var result = await _adminDiscountService.UpdateDiscountAsync(discountDto);
return NoContent();
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
};
}
/// <summary>
@@ -87,12 +105,18 @@ namespace Webshop.Api.Controllers.Admin
/// </summary>
/// <param name="id">Die ID des zu l<>schenden Rabatts.</param>
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteDiscount(Guid id)
{
var success = await _adminDiscountService.DeleteDiscountAsync(id);
if (!success) return NotFound();
var result = await _adminDiscountService.DeleteDiscountAsync(id);
return NoContent();
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
};
}
}
}