Files
Tizian.Breuch 2d93fda7e9 order
2025-07-31 16:12:48 +02:00

67 lines
2.2 KiB
C#

// 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;
namespace Webshop.Infrastructure.Repositories
{
public class OrderRepository : IOrderRepository
{
private readonly ApplicationDbContext _context;
public OrderRepository(ApplicationDbContext context)
{
_context = context;
}
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();
}
}
}
}