test #2
@@ -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
|
||||
{
|
||||
|
||||
@@ -27,6 +27,7 @@ using Webshop.Infrastructure.Data;
|
||||
using Webshop.Infrastructure.Repositories;
|
||||
using Webshop.Infrastructure.Services;
|
||||
|
||||
|
||||
var options = new WebApplicationOptions
|
||||
{
|
||||
Args = args,
|
||||
|
||||
22
Webshop.Domain/Entities/Cart.cs
Normal file
22
Webshop.Domain/Entities/Cart.cs
Normal file
@@ -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<CartItem> Items { get; set; } = new List<CartItem>();
|
||||
}
|
||||
}
|
||||
26
Webshop.Domain/Entities/CartItem.cs
Normal file
26
Webshop.Domain/Entities/CartItem.cs
Normal file
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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<PaymentMethod> PaymentMethods { get; set; } = default!;
|
||||
public DbSet<Setting> Settings { get; set; } = default!;
|
||||
|
||||
// Verknüpfungstabellen und Details
|
||||
public DbSet<Productcategorie> Productcategories { get; set; } = default!;
|
||||
public DbSet<ProductDiscount> ProductDiscounts { get; set; } = default!;
|
||||
public DbSet<CategorieDiscount> categorieDiscounts { get; set; } = default!;
|
||||
public DbSet<ProductImage> ProductImages { get; set; } = default!;
|
||||
public DbSet<ShopInfo> ShopInfos { get; set; } = default!;
|
||||
|
||||
// +++ NEU: Warenkorb Tabellen +++
|
||||
public DbSet<Cart> Carts { get; set; } = default!;
|
||||
public DbSet<CartItem> CartItems { get; set; } = default!;
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
@@ -48,6 +51,17 @@ namespace Webshop.Infrastructure.Data
|
||||
}
|
||||
}
|
||||
|
||||
// +++ NEU: Warenkorb Konfiguration +++
|
||||
modelBuilder.Entity<Cart>(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<Productcategorie>().HasKey(pc => new { pc.ProductId, pc.categorieId });
|
||||
modelBuilder.Entity<ProductDiscount>().HasKey(pd => new { pd.ProductId, pd.DiscountId });
|
||||
modelBuilder.Entity<CategorieDiscount>().HasKey(cd => new { cd.categorieId, cd.DiscountId });
|
||||
@@ -143,8 +157,8 @@ namespace Webshop.Infrastructure.Data
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<ApplicationUser>()
|
||||
.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<Customer>(c => c.AspNetUserId);
|
||||
|
||||
modelBuilder.Entity<ProductDiscount>(entity =>
|
||||
@@ -158,8 +172,6 @@ namespace Webshop.Infrastructure.Data
|
||||
.HasForeignKey(pd => pd.DiscountId);
|
||||
});
|
||||
|
||||
|
||||
// << NEU: Beziehungskonfiguration für CategorieDiscount >>
|
||||
modelBuilder.Entity<CategorieDiscount>(entity =>
|
||||
{
|
||||
entity.HasOne(cd => cd.categorie)
|
||||
@@ -173,20 +185,16 @@ namespace Webshop.Infrastructure.Data
|
||||
|
||||
modelBuilder.Entity<Review>(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);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user