admindiscount
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// src/Webshop.Api/Controllers/Admin/AdminDiscountsController.cs
|
// src/Webshop.Api/Controllers/Admin/AdminDiscountsController.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;
|
||||||
@@ -29,10 +30,11 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
/// Ruft eine Liste aller konfigurierten Rabatte ab.
|
/// Ruft eine Liste aller konfigurierten Rabatte ab.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<DiscountDto>>> GetAllDiscounts()
|
[ProducesResponseType(typeof(IEnumerable<DiscountDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetAllDiscounts()
|
||||||
{
|
{
|
||||||
var discounts = await _adminDiscountService.GetAllDiscountsAsync();
|
var result = await _adminDiscountService.GetAllDiscountsAsync();
|
||||||
return Ok(discounts);
|
return Ok(result.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -40,11 +42,18 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">Die ID des Rabatts.</param>
|
/// <param name="id">Die ID des Rabatts.</param>
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<DiscountDto>> GetDiscountById(Guid id)
|
[ProducesResponseType(typeof(DiscountDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetDiscountById(Guid id)
|
||||||
{
|
{
|
||||||
var discount = await _adminDiscountService.GetDiscountByIdAsync(id);
|
var result = await _adminDiscountService.GetDiscountByIdAsync(id);
|
||||||
if (discount == null) return NotFound();
|
|
||||||
return Ok(discount);
|
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." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -60,19 +69,24 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
/// <param name="discountDto">Das Datenobjekt des zu erstellenden Rabatts.</param>
|
/// <param name="discountDto">Das Datenobjekt des zu erstellenden Rabatts.</param>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ProducesResponseType(typeof(DiscountDto), StatusCodes.Status201Created)]
|
[ProducesResponseType(typeof(DiscountDto), StatusCodes.Status201Created)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
public async Task<ActionResult<DiscountDto>> CreateDiscount([FromBody] DiscountDto discountDto)
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||||||
|
public async Task<IActionResult> CreateDiscount([FromBody] DiscountDto discountDto)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
var result = await _adminDiscountService.CreateDiscountAsync(discountDto);
|
var result = await _adminDiscountService.CreateDiscountAsync(discountDto);
|
||||||
|
|
||||||
if (result.Type == ServiceResultType.Success)
|
return result.Type switch
|
||||||
{
|
{
|
||||||
return CreatedAtAction(nameof(GetDiscountById), new { id = result.Value!.Id }, result.Value);
|
ServiceResultType.Success => CreatedAtAction(nameof(GetDiscountById), new { id = result.Value!.Id }, result.Value),
|
||||||
}
|
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||||
return BadRequest(new { Message = result.ErrorMessage });
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -82,12 +96,19 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
/// <param name="discountDto">Die neuen Daten f<>r den Rabatt.</param>
|
/// <param name="discountDto">Die neuen Daten f<>r den Rabatt.</param>
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||||||
public async Task<IActionResult> UpdateDiscount(Guid id, [FromBody] DiscountDto discountDto)
|
public async Task<IActionResult> UpdateDiscount(Guid id, [FromBody] DiscountDto discountDto)
|
||||||
{
|
{
|
||||||
if (id != discountDto.Id) return BadRequest("ID in URL und Body stimmen nicht <20>berein.");
|
if (id != discountDto.Id)
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
{
|
||||||
|
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht <20>berein." });
|
||||||
|
}
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
var result = await _adminDiscountService.UpdateDiscountAsync(discountDto);
|
var result = await _adminDiscountService.UpdateDiscountAsync(discountDto);
|
||||||
|
|
||||||
@@ -95,8 +116,9 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
{
|
{
|
||||||
ServiceResultType.Success => NoContent(),
|
ServiceResultType.Success => NoContent(),
|
||||||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||||||
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||||
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +128,7 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
/// <param name="id">Die ID des zu l<>schenden Rabatts.</param>
|
/// <param name="id">Die ID des zu l<>schenden Rabatts.</param>
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
public async Task<IActionResult> DeleteDiscount(Guid id)
|
public async Task<IActionResult> DeleteDiscount(Guid id)
|
||||||
{
|
{
|
||||||
var result = await _adminDiscountService.DeleteDiscountAsync(id);
|
var result = await _adminDiscountService.DeleteDiscountAsync(id);
|
||||||
@@ -115,7 +137,7 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
{
|
{
|
||||||
ServiceResultType.Success => NoContent(),
|
ServiceResultType.Success => NoContent(),
|
||||||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,47 +4,49 @@ 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.Discounts;
|
using Webshop.Application.DTOs.Discounts;
|
||||||
using Webshop.Application.Services.Admin.Interfaces;
|
using Webshop.Application.Services.Admin.Interfaces;
|
||||||
using Webshop.Domain.Entities;
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Enums;
|
using Webshop.Domain.Enums;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
using Webshop.Infrastructure.Data;
|
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin
|
namespace Webshop.Application.Services.Admin
|
||||||
{
|
{
|
||||||
public class AdminDiscountService : IAdminDiscountService
|
public class AdminDiscountService : IAdminDiscountService
|
||||||
{
|
{
|
||||||
private readonly IDiscountRepository _discountRepository;
|
private readonly IDiscountRepository _discountRepository;
|
||||||
private readonly ApplicationDbContext _context; // F<>r Unique-Pr<50>fungen
|
|
||||||
|
|
||||||
public AdminDiscountService(IDiscountRepository discountRepository, ApplicationDbContext context)
|
public AdminDiscountService(IDiscountRepository discountRepository)
|
||||||
{
|
{
|
||||||
_discountRepository = discountRepository;
|
_discountRepository = discountRepository;
|
||||||
_context = context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync()
|
public async Task<ServiceResult<IEnumerable<DiscountDto>>> GetAllDiscountsAsync()
|
||||||
{
|
{
|
||||||
var discounts = await _discountRepository.GetAllAsync();
|
var discounts = await _discountRepository.GetAllAsync();
|
||||||
return discounts.Select(MapToDto).ToList();
|
var dtos = discounts.Select(MapToDto).ToList();
|
||||||
|
return ServiceResult.Ok<IEnumerable<DiscountDto>>(dtos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<DiscountDto?> GetDiscountByIdAsync(Guid id)
|
public async Task<ServiceResult<DiscountDto>> GetDiscountByIdAsync(Guid id)
|
||||||
{
|
{
|
||||||
var discount = await _discountRepository.GetByIdAsync(id);
|
var discount = await _discountRepository.GetByIdAsync(id);
|
||||||
return discount != null ? MapToDto(discount) : null;
|
if (discount == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<DiscountDto>(ServiceResultType.NotFound, $"Rabatt mit ID '{id}' nicht gefunden.");
|
||||||
|
}
|
||||||
|
return ServiceResult.Ok(MapToDto(discount));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto)
|
public async Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto)
|
||||||
{
|
{
|
||||||
// Validierung: Gutscheincode muss eindeutig sein, wenn er ben<65>tigt wird
|
|
||||||
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
||||||
{
|
{
|
||||||
var existing = await _context.Discounts.FirstOrDefaultAsync(d => d.CouponCode == discountDto.CouponCode);
|
var existing = await _discountRepository.GetByCouponCodeAsync(discountDto.CouponCode);
|
||||||
if (existing != null)
|
if (existing != null)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail<DiscountDto>(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' existiert bereits.");
|
return ServiceResult.Fail<DiscountDto>(ServiceResultType.Conflict, $"Der Gutscheincode '{discountDto.CouponCode}' existiert bereits.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,15 +62,15 @@ namespace Webshop.Application.Services.Admin
|
|||||||
var existingDiscount = await _discountRepository.GetByIdAsync(discountDto.Id);
|
var existingDiscount = await _discountRepository.GetByIdAsync(discountDto.Id);
|
||||||
if (existingDiscount == null)
|
if (existingDiscount == null)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Rabatt mit ID '{discountDto.Id}' nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
if (discountDto.RequiresCouponCode && !string.IsNullOrEmpty(discountDto.CouponCode))
|
||||||
{
|
{
|
||||||
var existing = await _context.Discounts.FirstOrDefaultAsync(d => d.CouponCode == discountDto.CouponCode && d.Id != discountDto.Id);
|
var existing = await _discountRepository.GetByCouponCodeAsync(discountDto.CouponCode);
|
||||||
if (existing != null)
|
if (existing != null && existing.Id != discountDto.Id)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Der Gutscheincode '{discountDto.CouponCode}' wird bereits von einem anderen Rabatt verwendet.");
|
return ServiceResult.Fail(ServiceResultType.Conflict, $"Der Gutscheincode '{discountDto.CouponCode}' wird bereits von einem anderen Rabatt verwendet.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,25 +83,29 @@ namespace Webshop.Application.Services.Admin
|
|||||||
existingDiscount.EndDate = discountDto.EndDate;
|
existingDiscount.EndDate = discountDto.EndDate;
|
||||||
existingDiscount.IsActive = discountDto.IsActive;
|
existingDiscount.IsActive = discountDto.IsActive;
|
||||||
existingDiscount.RequiresCouponCode = discountDto.RequiresCouponCode;
|
existingDiscount.RequiresCouponCode = discountDto.RequiresCouponCode;
|
||||||
existingDiscount.CouponCode = discountDto.CouponCode;
|
existingDiscount.CouponCode = discountDto.RequiresCouponCode ? discountDto.CouponCode : null;
|
||||||
existingDiscount.MinimumOrderAmount = discountDto.MinimumOrderAmount;
|
existingDiscount.MinimumOrderAmount = discountDto.MinimumOrderAmount;
|
||||||
existingDiscount.MaximumUsageCount = discountDto.MaximumUsageCount;
|
existingDiscount.MaximumUsageCount = discountDto.MaximumUsageCount;
|
||||||
|
|
||||||
// << HIER IST DER WICHTIGE, WIEDERHERGESTELLTE TEIL >>
|
|
||||||
// Sync assigned products
|
// Sync assigned products
|
||||||
existingDiscount.ProductDiscounts.Clear();
|
existingDiscount.ProductDiscounts.Clear();
|
||||||
foreach (var productId in discountDto.AssignedProductIds)
|
if (discountDto.AssignedProductIds != null)
|
||||||
{
|
{
|
||||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { DiscountId = existingDiscount.Id, ProductId = productId });
|
foreach (var productId in discountDto.AssignedProductIds)
|
||||||
|
{
|
||||||
|
existingDiscount.ProductDiscounts.Add(new ProductDiscount { DiscountId = existingDiscount.Id, ProductId = productId });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync assigned categories
|
// Sync assigned categories
|
||||||
existingDiscount.categorieDiscounts.Clear();
|
existingDiscount.categorieDiscounts.Clear();
|
||||||
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
if (discountDto.AssignedCategoryIds != null)
|
||||||
{
|
{
|
||||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { DiscountId = existingDiscount.Id, categorieId = categoryId });
|
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
||||||
|
{
|
||||||
|
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { DiscountId = existingDiscount.Id, categorieId = categoryId });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// << ENDE DES WICHTIGEN TEILS >>
|
|
||||||
|
|
||||||
await _discountRepository.UpdateAsync(existingDiscount);
|
await _discountRepository.UpdateAsync(existingDiscount);
|
||||||
return ServiceResult.Ok();
|
return ServiceResult.Ok();
|
||||||
@@ -110,7 +116,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
var discount = await _discountRepository.GetByIdAsync(id);
|
var discount = await _discountRepository.GetByIdAsync(id);
|
||||||
if (discount == null)
|
if (discount == null)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail(ServiceResultType.NotFound, "Rabatt nicht gefunden.");
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Rabatt mit ID '{id}' nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
await _discountRepository.DeleteAsync(id);
|
await _discountRepository.DeleteAsync(id);
|
||||||
@@ -154,7 +160,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
EndDate = dto.EndDate,
|
EndDate = dto.EndDate,
|
||||||
IsActive = dto.IsActive,
|
IsActive = dto.IsActive,
|
||||||
RequiresCouponCode = dto.RequiresCouponCode,
|
RequiresCouponCode = dto.RequiresCouponCode,
|
||||||
CouponCode = dto.CouponCode,
|
CouponCode = dto.RequiresCouponCode ? dto.CouponCode : null,
|
||||||
MinimumOrderAmount = dto.MinimumOrderAmount,
|
MinimumOrderAmount = dto.MinimumOrderAmount,
|
||||||
MaximumUsageCount = dto.MaximumUsageCount,
|
MaximumUsageCount = dto.MaximumUsageCount,
|
||||||
CurrentUsageCount = dto.CurrentUsageCount,
|
CurrentUsageCount = dto.CurrentUsageCount,
|
||||||
|
|||||||
@@ -2,15 +2,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Webshop.Application; // << NEU: F<>r ServiceResult >>
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Discounts;
|
using Webshop.Application.DTOs.Discounts;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin.Interfaces
|
namespace Webshop.Application.Services.Admin.Interfaces
|
||||||
{
|
{
|
||||||
public interface IAdminDiscountService
|
public interface IAdminDiscountService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync();
|
Task<ServiceResult<IEnumerable<DiscountDto>>> GetAllDiscountsAsync();
|
||||||
Task<DiscountDto?> GetDiscountByIdAsync(Guid id);
|
Task<ServiceResult<DiscountDto>> GetDiscountByIdAsync(Guid id);
|
||||||
Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto);
|
Task<ServiceResult<DiscountDto>> CreateDiscountAsync(DiscountDto discountDto);
|
||||||
Task<ServiceResult> UpdateDiscountAsync(DiscountDto discountDto);
|
Task<ServiceResult> UpdateDiscountAsync(DiscountDto discountDto);
|
||||||
Task<ServiceResult> DeleteDiscountAsync(Guid id);
|
Task<ServiceResult> DeleteDiscountAsync(Guid id);
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace Webshop.Domain.Interfaces
|
|||||||
{
|
{
|
||||||
Task<IEnumerable<Discount>> GetAllAsync();
|
Task<IEnumerable<Discount>> GetAllAsync();
|
||||||
Task<Discount?> GetByIdAsync(Guid id);
|
Task<Discount?> GetByIdAsync(Guid id);
|
||||||
|
Task<Discount?> GetByCouponCodeAsync(string couponCode); // << NEUE METHODE HINZUF<55>GEN
|
||||||
Task AddAsync(Discount discount);
|
Task AddAsync(Discount discount);
|
||||||
Task UpdateAsync(Discount discount);
|
Task UpdateAsync(Discount discount);
|
||||||
Task DeleteAsync(Guid id);
|
Task DeleteAsync(Guid id);
|
||||||
|
|||||||
@@ -35,6 +35,17 @@ namespace Webshop.Infrastructure.Repositories
|
|||||||
.FirstOrDefaultAsync(d => d.Id == id);
|
.FirstOrDefaultAsync(d => d.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// << NEUE METHODE IMPLEMENTIEREN >>
|
||||||
|
public async Task<Discount?> GetByCouponCodeAsync(string couponCode)
|
||||||
|
{
|
||||||
|
// Case-insensitive Suche f<>r den Gutscheincode
|
||||||
|
return await _context.Discounts
|
||||||
|
.Include(d => d.ProductDiscounts)
|
||||||
|
.Include(d => d.categorieDiscounts)
|
||||||
|
.FirstOrDefaultAsync(d => d.CouponCode != null && d.CouponCode.ToUpper() == couponCode.ToUpper());
|
||||||
|
}
|
||||||
|
// << ENDE DER NEUEN METHODE >>
|
||||||
|
|
||||||
public async Task AddAsync(Discount discount)
|
public async Task AddAsync(Discount discount)
|
||||||
{
|
{
|
||||||
await _context.Discounts.AddAsync(discount);
|
await _context.Discounts.AddAsync(discount);
|
||||||
@@ -43,8 +54,31 @@ namespace Webshop.Infrastructure.Repositories
|
|||||||
|
|
||||||
public async Task UpdateAsync(Discount discount)
|
public async Task UpdateAsync(Discount discount)
|
||||||
{
|
{
|
||||||
_context.Discounts.Update(discount);
|
// Sicherstellen, dass die verkn<6B>pften Entit<69>ten nicht neu hinzugef<65>gt werden
|
||||||
await _context.SaveChangesAsync();
|
var existingDiscount = await _context.Discounts
|
||||||
|
.Include(d => d.ProductDiscounts)
|
||||||
|
.Include(d => d.categorieDiscounts)
|
||||||
|
.FirstOrDefaultAsync(d => d.Id == discount.Id);
|
||||||
|
|
||||||
|
if (existingDiscount != null)
|
||||||
|
{
|
||||||
|
_context.Entry(existingDiscount).CurrentValues.SetValues(discount);
|
||||||
|
|
||||||
|
// Manuelles Synchronisieren der Many-to-Many-Beziehungen
|
||||||
|
existingDiscount.ProductDiscounts.Clear();
|
||||||
|
foreach (var pd in discount.ProductDiscounts)
|
||||||
|
{
|
||||||
|
existingDiscount.ProductDiscounts.Add(pd);
|
||||||
|
}
|
||||||
|
|
||||||
|
existingDiscount.categorieDiscounts.Clear();
|
||||||
|
foreach (var cd in discount.categorieDiscounts)
|
||||||
|
{
|
||||||
|
existingDiscount.categorieDiscounts.Add(cd);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(Guid id)
|
public async Task DeleteAsync(Guid id)
|
||||||
|
|||||||
Reference in New Issue
Block a user