diff --git a/Webshop.Api/Controllers/Customers/CustomerController.cs b/Webshop.Api/Controllers/Customers/CustomerController.cs index 8eb3676..7f17df3 100644 --- a/Webshop.Api/Controllers/Customers/CustomerController.cs +++ b/Webshop.Api/Controllers/Customers/CustomerController.cs @@ -10,6 +10,7 @@ using Webshop.Application.DTOs.Auth; using Webshop.Application.DTOs.Customers; using Webshop.Application.DTOs.Email; using Webshop.Application.Services.Customers; +using Webshop.Application.Services.Customers.Interfaces; namespace Webshop.Api.Controllers.Customer { diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 43415bd..7ea125f 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -27,6 +27,7 @@ using Webshop.Infrastructure.Data; using Webshop.Infrastructure.Repositories; using Webshop.Infrastructure.Services; + var options = new WebApplicationOptions { Args = args, diff --git a/Webshop.Domain/Entities/Cart.cs b/Webshop.Domain/Entities/Cart.cs new file mode 100644 index 0000000..ec0420d --- /dev/null +++ b/Webshop.Domain/Entities/Cart.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Webshop.Domain.Entities +{ + public class Cart + { + [Key] + public Guid Id { get; set; } + + // Verknüpfung zum User (String, da Identity User Id ein String ist) + public string? UserId { get; set; } + + // Falls du auch Gast-Warenkörbe ohne Login unterstützt: + public string? SessionId { get; set; } + + public DateTime LastModified { get; set; } = DateTime.UtcNow; + + public virtual ICollection Items { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/Webshop.Domain/Entities/CartItem.cs b/Webshop.Domain/Entities/CartItem.cs new file mode 100644 index 0000000..1d17785 --- /dev/null +++ b/Webshop.Domain/Entities/CartItem.cs @@ -0,0 +1,26 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Webshop.Domain.Entities +{ + public class CartItem + { + [Key] + public Guid Id { get; set; } + + public Guid CartId { get; set; } + + [ForeignKey("CartId")] + public virtual Cart Cart { get; set; } = default!; + + public Guid ProductId { get; set; } + + [ForeignKey("ProductId")] + public virtual Product Product { get; set; } = default!; + + public Guid? ProductVariantId { get; set; } // Optional, falls du Varianten nutzt + + public int Quantity { get; set; } + } +} \ No newline at end of file diff --git a/Webshop.Infrastructure/Data/ApplicationDbContext.cs b/Webshop.Infrastructure/Data/ApplicationDbContext.cs index dd74cac..2561ea3 100644 --- a/Webshop.Infrastructure/Data/ApplicationDbContext.cs +++ b/Webshop.Infrastructure/Data/ApplicationDbContext.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Webshop.Domain.Entities; using Webshop.Domain.Identity; @@ -26,12 +25,16 @@ namespace Webshop.Infrastructure.Data public DbSet PaymentMethods { get; set; } = default!; public DbSet Settings { get; set; } = default!; + // Verknüpfungstabellen und Details public DbSet Productcategories { get; set; } = default!; public DbSet ProductDiscounts { get; set; } = default!; public DbSet categorieDiscounts { get; set; } = default!; public DbSet ProductImages { get; set; } = default!; public DbSet ShopInfos { get; set; } = default!; + // +++ NEU: Warenkorb Tabellen +++ + public DbSet Carts { get; set; } = default!; + public DbSet CartItems { get; set; } = default!; protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -48,6 +51,17 @@ namespace Webshop.Infrastructure.Data } } + // +++ NEU: Warenkorb Konfiguration +++ + modelBuilder.Entity(entity => + { + // Wenn ein Warenkorb gelöscht wird, lösche alle Items darin automatisch + entity.HasMany(c => c.Items) + .WithOne(i => i.Cart) + .HasForeignKey(i => i.CartId) + .OnDelete(DeleteBehavior.Cascade); + }); + + // Bestehende Konfigurationen (bleiben unverändert) modelBuilder.Entity().HasKey(pc => new { pc.ProductId, pc.categorieId }); modelBuilder.Entity().HasKey(pd => new { pd.ProductId, pd.DiscountId }); modelBuilder.Entity().HasKey(cd => new { cd.categorieId, cd.DiscountId }); @@ -143,8 +157,8 @@ namespace Webshop.Infrastructure.Data .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() - .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 + .HasOne(a => a.Customer) + .WithOne(c => c.User) .HasForeignKey(c => c.AspNetUserId); modelBuilder.Entity(entity => @@ -158,8 +172,6 @@ namespace Webshop.Infrastructure.Data .HasForeignKey(pd => pd.DiscountId); }); - - // << NEU: Beziehungskonfiguration für CategorieDiscount >> modelBuilder.Entity(entity => { entity.HasOne(cd => cd.categorie) @@ -173,20 +185,16 @@ namespace Webshop.Infrastructure.Data modelBuilder.Entity(entity => { - // Beziehung zu Product entity.HasOne(r => r.Product) .WithMany(p => p.Reviews) .HasForeignKey(r => r.ProductId) - .OnDelete(DeleteBehavior.Cascade); // Lösche Bewertungen, wenn das Produkt gelöscht wird + .OnDelete(DeleteBehavior.Cascade); - // Beziehung zu Customer entity.HasOne(r => r.Customer) .WithMany(c => c.Reviews) .HasForeignKey(r => r.CustomerId) - .OnDelete(DeleteBehavior.SetNull); // Setze CustomerId auf NULL, wenn der Kunde gelöscht wird + .OnDelete(DeleteBehavior.SetNull); }); - - } } } \ No newline at end of file