This commit is contained in:
Tizian.Breuch
2025-07-31 16:12:48 +02:00
parent 4a24f4559d
commit 2d93fda7e9
10 changed files with 264 additions and 104 deletions

View File

@@ -1,11 +1,12 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Infrastructure/Repositories/OrderRepository.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Webshop.Infrastructure.Repositories
{
@@ -18,12 +19,49 @@ 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<Order>> GetAllAsync()
{
// Lade Bestellungen und zugehörige Kunden, um den Kundennamen anzeigen zu können
return await _context.Orders
.Include(o => o.Customer)
.OrderByDescending(o => o.OrderDate)
.ToListAsync();
}
public async Task<Order?> GetByIdAsync(Guid id)
{
// Lade eine einzelne Bestellung mit allen relevanten Details
return await _context.Orders
.Include(o => o.Customer)
.Include(o => o.BillingAddress)
.Include(o => o.ShippingAddress)
.Include(o => o.PaymentMethodInfo) // << KORREKTUR: Name der Navigation Property >>
.Include(o => o.ShippingMethodInfo) // << KORREKTUR: Name der Navigation Property >>
.Include(o => o.OrderItems)
.ThenInclude(oi => oi.Product)
.FirstOrDefaultAsync(o => o.Id == id);
}
public async Task AddAsync(Order order)
{
_context.Orders.Add(order);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Order order)
{
_context.Orders.Update(order);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(Guid id)
{
var order = await _context.Orders.FindAsync(id);
if (order != null)
{
_context.Orders.Remove(order);
await _context.SaveChangesAsync();
}
}
}
}
}