admindiscount
This commit is contained in:
@@ -35,6 +35,17 @@ namespace Webshop.Infrastructure.Repositories
|
||||
.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)
|
||||
{
|
||||
await _context.Discounts.AddAsync(discount);
|
||||
@@ -43,8 +54,31 @@ namespace Webshop.Infrastructure.Repositories
|
||||
|
||||
public async Task UpdateAsync(Discount discount)
|
||||
{
|
||||
_context.Discounts.Update(discount);
|
||||
await _context.SaveChangesAsync();
|
||||
// Sicherstellen, dass die verkn<6B>pften Entit<69>ten nicht neu hinzugef<65>gt werden
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user