discount und summary
This commit is contained in:
@@ -1,18 +1,98 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Api/Controllers/Admin/AdminDiscountsController.cs
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
namespace Webshop.Api.Controllers.Admin
|
||||
{
|
||||
/// <summary>
|
||||
/// API-Endpunkte zur Verwaltung von Rabatten und Gutscheincodes.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/admin/[controller]")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class AdminDiscountsController : ControllerBase
|
||||
{
|
||||
private readonly IAdminDiscountService _adminDiscountService;
|
||||
|
||||
public AdminDiscountsController(IAdminDiscountService adminDiscountService)
|
||||
{
|
||||
_adminDiscountService = adminDiscountService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ruft eine Liste aller konfigurierten Rabatte ab.
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<DiscountDto>>> GetAllDiscounts()
|
||||
{
|
||||
var discounts = await _adminDiscountService.GetAllDiscountsAsync();
|
||||
return Ok(discounts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ruft einen einzelnen Rabatt anhand seiner eindeutigen ID ab.
|
||||
/// </summary>
|
||||
/// <param name="id">Die ID des Rabatts.</param>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<DiscountDto>> GetDiscountById(Guid id)
|
||||
{
|
||||
var discount = await _adminDiscountService.GetDiscountByIdAsync(id);
|
||||
if (discount == null) return NotFound();
|
||||
return Ok(discount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Erstellt einen neuen Rabatt.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// **Funktionsweise von Rabatten:**
|
||||
/// - **DiscountType:** 'Percentage' (z.B. 10 f<>r 10%) oder 'FixedAmount' (z.B. 5 f<>r 5,00<30>).
|
||||
/// - **CouponCode:** Wenn `requiresCouponCode` auf `true` gesetzt ist, muss ein eindeutiger `couponCode` angegeben werden.
|
||||
/// - **G<>ltigkeit:** Kann durch `startDate` und `endDate` zeitlich begrenzt werden.
|
||||
/// - **Zuweisung:** Der Rabatt kann entweder bestimmten Produkten (`assignedProductIds`) oder ganzen Kategorien (`assignedCategoryIds`) zugewiesen werden. Wenn beide Listen leer sind, gilt der Rabatt f<>r den gesamten Warenkorb (sofern `minimumOrderAmount` erreicht ist).
|
||||
/// </remarks>
|
||||
/// <param name="discountDto">Das Datenobjekt des zu erstellenden Rabatts.</param>
|
||||
[HttpPost]
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aktualisiert einen bestehenden Rabatt.
|
||||
/// </summary>
|
||||
/// <param name="id">Die ID des zu aktualisierenden Rabatts.</param>
|
||||
/// <param name="discountDto">Die neuen Daten f<>r den Rabatt.</param>
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateDiscount(Guid id, [FromBody] DiscountDto discountDto)
|
||||
{
|
||||
if (id != discountDto.Id) return BadRequest();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var success = await _adminDiscountService.UpdateDiscountAsync(discountDto);
|
||||
if (!success) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// L<>scht einen Rabatt.
|
||||
/// </summary>
|
||||
/// <param name="id">Die ID des zu l<>schenden Rabatts.</param>
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteDiscount(Guid id)
|
||||
{
|
||||
var success = await _adminDiscountService.DeleteDiscountAsync(id);
|
||||
if (!success) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,9 @@ using Webshop.Application.Services.Admin;
|
||||
|
||||
namespace Webshop.Api.Controllers.Admin
|
||||
{
|
||||
/// <summary>
|
||||
/// API-Endpunkte zur Verwaltung der zentralen Shop-Stammdaten.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/admin/[controller]")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
@@ -19,6 +22,12 @@ namespace Webshop.Api.Controllers.Admin
|
||||
_shopInfoService = shopInfoService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ruft die aktuellen Stammdaten des Shops ab.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Diese Daten werden typischerweise in einem "Stammdaten"- oder "Shop-Einstellungen"-Formular im Admin-Dashboard angezeigt.
|
||||
/// </remarks>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<AdminShopInfoDto>> GetShopInfo()
|
||||
{
|
||||
@@ -26,6 +35,13 @@ namespace Webshop.Api.Controllers.Admin
|
||||
return Ok(info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aktualisiert die zentralen Stammdaten des Shops.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Sendet das gesamte, ausgefüllte DTO. Alle Felder werden überschrieben.
|
||||
/// </remarks>
|
||||
/// <param name="shopInfoDto">Das Datenobjekt mit den neuen Shop-Informationen.</param>
|
||||
[HttpPut]
|
||||
public async Task<IActionResult> UpdateShopInfo([FromBody] AdminShopInfoDto shopInfoDto)
|
||||
{
|
||||
|
||||
@@ -7,6 +7,9 @@ using Webshop.Application.Services.Public;
|
||||
|
||||
namespace Webshop.Api.Controllers.Public
|
||||
{
|
||||
/// <summary>
|
||||
/// API-Endpunkte zum Abrufen öffentlicher Shop-Informationen.
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/v1/public/[controller]")]
|
||||
[AllowAnonymous]
|
||||
@@ -19,6 +22,12 @@ namespace Webshop.Api.Controllers.Public
|
||||
_shopInfoService = shopInfoService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ruft die öffentlichen Stammdaten des Shops ab.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Diese Informationen sind für jeden Besucher sichtbar und werden typischerweise im Footer, auf der Kontaktseite oder im Impressum verwendet.
|
||||
/// </remarks>
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<PublicShopInfoDto>> GetPublicShopInfo()
|
||||
{
|
||||
|
||||
@@ -103,9 +103,12 @@ builder.Services.AddScoped<IOrderRepository, OrderRepository>();
|
||||
builder.Services.AddScoped<IShippingMethodRepository, ShippingMethodRepository>();
|
||||
builder.Services.AddScoped<IAddressRepository, AddressRepository>();
|
||||
builder.Services.AddScoped<IDiscountRepository, DiscountRepository>();
|
||||
builder.Services.AddScoped<IReviewRepository, ReviewRepository>();
|
||||
builder.Services.AddScoped<ISettingRepository, SettingRepository>();
|
||||
builder.Services.AddScoped<IShopInfoRepository, ShopInfoRepository>();
|
||||
builder.Services.AddScoped<IDiscountRepository, DiscountRepository>();
|
||||
|
||||
//fehlt noch
|
||||
builder.Services.AddScoped<IReviewRepository, ReviewRepository>();
|
||||
|
||||
// Services
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
@@ -125,11 +128,14 @@ builder.Services.AddScoped<ICustomerService, CustomerService>();
|
||||
builder.Services.AddScoped<IOrderService, OrderService>();
|
||||
builder.Services.AddScoped<IAddressService, AddressService>();
|
||||
builder.Services.AddScoped<ICheckoutService, CheckoutService>();
|
||||
builder.Services.AddScoped<IReviewService, ReviewService>();
|
||||
builder.Services.AddScoped<IAdminShopInfoService, AdminShopInfoService>();
|
||||
builder.Services.AddScoped<IShopInfoService, ShopInfoService>();
|
||||
builder.Services.AddScoped<ISettingService, SettingService>();
|
||||
builder.Services.AddScoped<IAdminSettingService, AdminSettingService>();
|
||||
builder.Services.AddScoped<IAdminDiscountService, AdminDiscountService>();
|
||||
|
||||
//fehlt noch
|
||||
builder.Services.AddScoped<IReviewService, ReviewService>();
|
||||
|
||||
|
||||
// Controller und API-Infrastruktur
|
||||
|
||||
@@ -1,24 +1,86 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/DTOs/Discounts/DiscountDto.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using Webshop.Domain.Enums;
|
||||
|
||||
namespace Webshop.Application.DTOs.Discounts
|
||||
{
|
||||
/// <summary>
|
||||
/// Repr<70>sentiert einen Rabatt mit all seinen Konfigurationen und Zuweisungen.
|
||||
/// </summary>
|
||||
public class DiscountDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Der Name des Rabatts (z.B. "Sommerschlussverkauf", "10% auf alles").
|
||||
/// </summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public DiscountType Type { get; set; }
|
||||
public decimal Value { get; set; }
|
||||
public string? CouponCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Der Typ des Rabatts. 'Percentage' f<>r prozentuale Rabatte, 'FixedAmount' f<>r feste Betr<74>ge.
|
||||
/// </summary>
|
||||
public DiscountType DiscountType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Der Wert des Rabatts. Bei 'Percentage' z.B. 10 f<>r 10%. Bei 'FixedAmount' z.B. 5 f<>r 5,00<30>.
|
||||
/// </summary>
|
||||
public decimal DiscountValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Das Datum, an dem der Rabatt aktiv wird.
|
||||
/// </summary>
|
||||
public DateTimeOffset StartDate { get; set; }
|
||||
public DateTimeOffset EndDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Optionales Enddatum. Wenn null, ist der Rabatt unbegrenzt g<>ltig.
|
||||
/// </summary>
|
||||
public DateTimeOffset? EndDate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gibt an, ob der Rabatt derzeit aktiv ist und angewendet werden kann.
|
||||
/// </summary>
|
||||
public bool IsActive { get; set; }
|
||||
public int? MaxUses { get; set; }
|
||||
public int CurrentUses { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Wenn 'true', muss ein Gutscheincode eingegeben werden, um den Rabatt zu erhalten.
|
||||
/// </summary>
|
||||
public bool RequiresCouponCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Der Gutscheincode, der eingegeben werden muss (z.B. "SUMMER2025"). Muss eindeutig sein.
|
||||
/// </summary>
|
||||
public string? CouponCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Der Mindestbestellwert, der erreicht werden muss, damit der Rabatt angewendet wird.
|
||||
/// </summary>
|
||||
public decimal? MinimumOrderAmount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die maximale Anzahl, wie oft dieser Rabatt insgesamt eingel<65>st werden kann.
|
||||
/// </summary>
|
||||
public int? MaximumUsageCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die aktuelle Anzahl der Einl<6E>sungen. Wird vom System verwaltet.
|
||||
/// </summary>
|
||||
public int CurrentUsageCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Eine interne Beschreibung f<>r den Admin.
|
||||
/// </summary>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Eine Liste von Produkt-IDs, auf die dieser Rabatt exklusiv angewendet wird.
|
||||
/// Wenn leer, gilt der Rabatt f<>r alle Produkte (sofern keine Kategorien zugewiesen sind).
|
||||
/// </summary>
|
||||
public List<Guid> AssignedProductIds { get; set; } = new List<Guid>();
|
||||
|
||||
/// <summary>
|
||||
/// Eine Liste von Kategorie-IDs. Der Rabatt wird auf alle Produkte in diesen Kategorien angewendet.
|
||||
/// </summary>
|
||||
public List<Guid> AssignedCategoryIds { get; set; } = new List<Guid>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,88 @@
|
||||
// src/Webshop.Application/DTOs/Admin/AdminShopInfoDto.cs
|
||||
// src/Webshop.Application/DTOs/Shop/AdminShopInfoDto.cs
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Application.DTOs.Shop
|
||||
{
|
||||
/// <summary>
|
||||
/// Repräsentiert alle zentralen Stammdaten des Shops für die administrative Verwaltung.
|
||||
/// </summary>
|
||||
public class AdminShopInfoDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Der offizielle Name des Webshops.
|
||||
/// </summary>
|
||||
[Required, MaxLength(255)]
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Ein kurzer Marketing-Slogan, der auf der Startseite angezeigt werden kann.
|
||||
/// </summary>
|
||||
[MaxLength(500)]
|
||||
public string? Slogan { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die primäre Kontakt-E-Mail-Adresse für Kundenanfragen.
|
||||
/// </summary>
|
||||
[Required, EmailAddress, MaxLength(255)]
|
||||
public string ContactEmail { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Die offizielle Telefonnummer des Shops.
|
||||
/// </summary>
|
||||
[Phone, MaxLength(50)]
|
||||
public string? PhoneNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Straße und Hausnummer der Firmenadresse.
|
||||
/// </summary>
|
||||
[MaxLength(255)]
|
||||
public string? Street { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stadt der Firmenadresse.
|
||||
/// </summary>
|
||||
[MaxLength(100)]
|
||||
public string? City { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Postleitzahl der Firmenadresse.
|
||||
/// </summary>
|
||||
[MaxLength(20)]
|
||||
public string? PostalCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Land der Firmenadresse.
|
||||
/// </summary>
|
||||
[MaxLength(100)]
|
||||
public string? Country { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die Umsatzsteuer-Identifikationsnummer des Unternehmens.
|
||||
/// </summary>
|
||||
[MaxLength(50)]
|
||||
public string? VatNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die Handelsregisternummer des Unternehmens.
|
||||
/// </summary>
|
||||
[MaxLength(100)]
|
||||
public string? CompanyRegistrationNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die URL zur offiziellen Facebook-Seite.
|
||||
/// </summary>
|
||||
[Url, MaxLength(255)]
|
||||
public string? FacebookUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die URL zum offiziellen Instagram-Profil.
|
||||
/// </summary>
|
||||
[Url, MaxLength(255)]
|
||||
public string? InstagramUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die URL zum offiziellen Twitter/X-Profil.
|
||||
/// </summary>
|
||||
[Url, MaxLength(255)]
|
||||
public string? TwitterUrl { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,19 +1,64 @@
|
||||
// src/Webshop.Application/DTOs/Shop/PublicShopInfoDto.cs
|
||||
namespace Webshop.Application.DTOs.Shop
|
||||
{
|
||||
// Dieses DTO enthält nur die Informationen, die ein normaler Kunde sehen soll.
|
||||
/// <summary>
|
||||
/// Enthält die öffentlichen Stammdaten des Shops, die für Kunden sichtbar sind (z.B. im Footer oder auf der Kontaktseite).
|
||||
/// </summary>
|
||||
public class PublicShopInfoDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Der offizielle Name des Webshops.
|
||||
/// </summary>
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Ein kurzer Marketing-Slogan.
|
||||
/// </summary>
|
||||
public string? Slogan { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Die primäre Kontakt-E-Mail-Adresse.
|
||||
/// </summary>
|
||||
public string ContactEmail { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Die offizielle Telefonnummer.
|
||||
/// </summary>
|
||||
public string? PhoneNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Straße und Hausnummer.
|
||||
/// </summary>
|
||||
public string? Street { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stadt.
|
||||
/// </summary>
|
||||
public string? City { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Postleitzahl.
|
||||
/// </summary>
|
||||
public string? PostalCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Land.
|
||||
/// </summary>
|
||||
public string? Country { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// URL zur Facebook-Seite.
|
||||
/// </summary>
|
||||
public string? FacebookUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// URL zum Instagram-Profil.
|
||||
/// </summary>
|
||||
public string? InstagramUrl { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// URL zum Twitter/X-Profil.
|
||||
/// </summary>
|
||||
public string? TwitterUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,134 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/Services/Admin/AdminDiscountService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
using Webshop.Application.Services.Admin.Interfaces;
|
||||
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Enums;
|
||||
using Webshop.Domain.Interfaces;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminDiscountService : IAdminDiscountService
|
||||
{
|
||||
// Fügen Sie hier Abhängigkeiten per Dependency Injection hinzu (z.B. Repositories)
|
||||
private readonly IDiscountRepository _discountRepository;
|
||||
|
||||
// public AdminDiscountService(IYourRepository repository) { }
|
||||
public AdminDiscountService(IDiscountRepository discountRepository)
|
||||
{
|
||||
_discountRepository = discountRepository;
|
||||
}
|
||||
|
||||
// Fügen Sie hier Service-Methoden hinzu
|
||||
public async Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync()
|
||||
{
|
||||
var discounts = await _discountRepository.GetAllAsync();
|
||||
return discounts.Select(MapToDto).ToList();
|
||||
}
|
||||
|
||||
public async Task<DiscountDto?> GetDiscountByIdAsync(Guid id)
|
||||
{
|
||||
var discount = await _discountRepository.GetByIdAsync(id);
|
||||
return discount != null ? MapToDto(discount) : null;
|
||||
}
|
||||
|
||||
public async Task<DiscountDto?> CreateDiscountAsync(DiscountDto discountDto)
|
||||
{
|
||||
var discount = MapToEntity(discountDto);
|
||||
discount.Id = Guid.NewGuid();
|
||||
|
||||
await _discountRepository.AddAsync(discount);
|
||||
return MapToDto(discount);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateDiscountAsync(DiscountDto discountDto)
|
||||
{
|
||||
var existingDiscount = await _discountRepository.GetByIdAsync(discountDto.Id);
|
||||
if (existingDiscount == null) return false;
|
||||
|
||||
// Update simple properties
|
||||
existingDiscount.Name = discountDto.Name;
|
||||
existingDiscount.Description = discountDto.Description;
|
||||
existingDiscount.DiscountType = discountDto.DiscountType.ToString();
|
||||
existingDiscount.DiscountValue = discountDto.DiscountValue;
|
||||
existingDiscount.StartDate = discountDto.StartDate;
|
||||
existingDiscount.EndDate = discountDto.EndDate;
|
||||
existingDiscount.IsActive = discountDto.IsActive;
|
||||
existingDiscount.RequiresCouponCode = discountDto.RequiresCouponCode;
|
||||
existingDiscount.CouponCode = discountDto.CouponCode;
|
||||
existingDiscount.MinimumOrderAmount = discountDto.MinimumOrderAmount;
|
||||
existingDiscount.MaximumUsageCount = discountDto.MaximumUsageCount;
|
||||
|
||||
// Sync assigned products
|
||||
existingDiscount.ProductDiscounts.Clear();
|
||||
foreach (var productId in discountDto.AssignedProductIds)
|
||||
{
|
||||
existingDiscount.ProductDiscounts.Add(new ProductDiscount { ProductId = productId });
|
||||
}
|
||||
|
||||
// Sync assigned categories
|
||||
existingDiscount.categorieDiscounts.Clear();
|
||||
foreach (var categoryId in discountDto.AssignedCategoryIds)
|
||||
{
|
||||
existingDiscount.categorieDiscounts.Add(new CategorieDiscount { categorieId = categoryId });
|
||||
}
|
||||
|
||||
await _discountRepository.UpdateAsync(existingDiscount);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteDiscountAsync(Guid id)
|
||||
{
|
||||
var discount = await _discountRepository.GetByIdAsync(id);
|
||||
if (discount == null) return false;
|
||||
|
||||
await _discountRepository.DeleteAsync(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper methods for mapping
|
||||
private DiscountDto MapToDto(Discount discount)
|
||||
{
|
||||
return new DiscountDto
|
||||
{
|
||||
Id = discount.Id,
|
||||
Name = discount.Name,
|
||||
Description = discount.Description,
|
||||
DiscountType = Enum.Parse<DiscountType>(discount.DiscountType),
|
||||
DiscountValue = discount.DiscountValue,
|
||||
StartDate = discount.StartDate,
|
||||
EndDate = discount.EndDate,
|
||||
IsActive = discount.IsActive,
|
||||
RequiresCouponCode = discount.RequiresCouponCode,
|
||||
CouponCode = discount.CouponCode,
|
||||
MinimumOrderAmount = discount.MinimumOrderAmount,
|
||||
MaximumUsageCount = discount.MaximumUsageCount,
|
||||
CurrentUsageCount = discount.CurrentUsageCount,
|
||||
AssignedProductIds = discount.ProductDiscounts.Select(pd => pd.ProductId).ToList(),
|
||||
AssignedCategoryIds = discount.categorieDiscounts.Select(cd => cd.categorieId).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
private Discount MapToEntity(DiscountDto dto)
|
||||
{
|
||||
return new Discount
|
||||
{
|
||||
Id = dto.Id,
|
||||
Name = dto.Name,
|
||||
Description = dto.Description,
|
||||
DiscountType = dto.DiscountType.ToString(),
|
||||
DiscountValue = dto.DiscountValue,
|
||||
StartDate = dto.StartDate,
|
||||
EndDate = dto.EndDate,
|
||||
IsActive = dto.IsActive,
|
||||
RequiresCouponCode = dto.RequiresCouponCode,
|
||||
CouponCode = dto.CouponCode,
|
||||
MinimumOrderAmount = dto.MinimumOrderAmount,
|
||||
MaximumUsageCount = dto.MaximumUsageCount,
|
||||
CurrentUsageCount = dto.CurrentUsageCount,
|
||||
ProductDiscounts = dto.AssignedProductIds.Select(pid => new ProductDiscount { ProductId = pid }).ToList(),
|
||||
categorieDiscounts = dto.AssignedCategoryIds.Select(cid => new CategorieDiscount { categorieId = cid }).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/Services/Admin/Interfaces/IAdminDiscountService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
|
||||
using Webshop.Application.DTOs.Discounts;
|
||||
|
||||
namespace Webshop.Application.Services.Admin.Interfaces
|
||||
{
|
||||
public interface IAdminDiscountService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
Task<IEnumerable<DiscountDto>> GetAllDiscountsAsync();
|
||||
Task<DiscountDto?> GetDiscountByIdAsync(Guid id);
|
||||
Task<DiscountDto?> CreateDiscountAsync(DiscountDto discountDto);
|
||||
Task<bool> UpdateDiscountAsync(DiscountDto discountDto);
|
||||
Task<bool> DeleteDiscountAsync(Guid id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Domain/Interfaces/IDiscountRepository.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Domain.Entities;
|
||||
|
||||
|
||||
namespace Webshop.Domain.Interfaces
|
||||
{
|
||||
public interface IDiscountRepository
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
Task<IEnumerable<Discount>> GetAllAsync();
|
||||
Task<Discount?> GetByIdAsync(Guid id);
|
||||
Task AddAsync(Discount discount);
|
||||
Task UpdateAsync(Discount discount);
|
||||
Task DeleteAsync(Guid id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,32 @@ namespace Webshop.Infrastructure.Data
|
||||
.HasOne(a => a.Customer) // Ein ApplicationUser hat ein optionales Customer-Profil
|
||||
.WithOne(c => c.User) // Ein Customer-Profil ist mit genau einem User verknüpft
|
||||
.HasForeignKey<Customer>(c => c.AspNetUserId);
|
||||
|
||||
modelBuilder.Entity<ProductDiscount>(entity =>
|
||||
{
|
||||
entity.HasOne(pd => pd.Product)
|
||||
.WithMany(p => p.ProductDiscounts)
|
||||
.HasForeignKey(pd => pd.ProductId);
|
||||
|
||||
entity.HasOne(pd => pd.Discount)
|
||||
.WithMany(d => d.ProductDiscounts)
|
||||
.HasForeignKey(pd => pd.DiscountId);
|
||||
});
|
||||
|
||||
|
||||
// << NEU: Beziehungskonfiguration für CategorieDiscount >>
|
||||
modelBuilder.Entity<CategorieDiscount>(entity =>
|
||||
{
|
||||
entity.HasOne(cd => cd.categorie)
|
||||
.WithMany(c => c.categorieDiscounts)
|
||||
.HasForeignKey(cd => cd.categorieId);
|
||||
|
||||
entity.HasOne(cd => cd.Discount)
|
||||
.WithMany(d => d.categorieDiscounts)
|
||||
.HasForeignKey(cd => cd.DiscountId);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Infrastructure/Repositories/DiscountRepository.cs
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
@@ -18,12 +18,43 @@ namespace Webshop.Infrastructure.Repositories
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// Fügen Sie hier Repository-Methoden hinzu
|
||||
// Beispiel:
|
||||
// public async Task<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().ToListAsync(); }
|
||||
// public async Task<T?> GetByIdAsync(Guid id) { return await _context.Set<T>().FindAsync(id); }
|
||||
// public async Task AddAsync(T entity) { _context.Set<T>().Add(entity); await _context.SaveChangesAsync(); }
|
||||
// public async Task UpdateAsync(T entity) { _context.Set<T>().Update(entity); await _context.SaveChangesAsync(); }
|
||||
// public async Task DeleteAsync(Guid id) { var entity = await _context.Set<T>().FindAsync(id); if (entity != null) { _context.Set<T>().Remove(entity); await _context.SaveChangesAsync(); } }
|
||||
public async Task<IEnumerable<Discount>> GetAllAsync()
|
||||
{
|
||||
return await _context.Discounts
|
||||
.Include(d => d.ProductDiscounts)
|
||||
.Include(d => d.categorieDiscounts)
|
||||
.OrderBy(d => d.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Discount?> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await _context.Discounts
|
||||
.Include(d => d.ProductDiscounts)
|
||||
.Include(d => d.categorieDiscounts)
|
||||
.FirstOrDefaultAsync(d => d.Id == id);
|
||||
}
|
||||
|
||||
public async Task AddAsync(Discount discount)
|
||||
{
|
||||
await _context.Discounts.AddAsync(discount);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Discount discount)
|
||||
{
|
||||
_context.Discounts.Update(discount);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id)
|
||||
{
|
||||
var discount = await _context.Discounts.FindAsync(id);
|
||||
if (discount != null)
|
||||
{
|
||||
_context.Discounts.Remove(discount);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user