Files
ShopSolution-backend/Webshop.Infrastructure/Repositories/AddressRepository.cs
2025-08-01 13:42:11 +02:00

56 lines
1.6 KiB
C#

// src/Webshop.Infrastructure/Repositories/AddressRepository.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 AddressRepository : IAddressRepository
{
private readonly ApplicationDbContext _context;
public AddressRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Address>> GetAllForCustomerAsync(Guid customerId)
{
return await _context.Addresses
.Where(a => a.CustomerId == customerId)
.ToListAsync();
}
public async Task<Address?> GetByIdAsync(Guid id)
{
return await _context.Addresses.FindAsync(id);
}
public async Task AddAsync(Address address)
{
_context.Addresses.Add(address);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Address address)
{
_context.Addresses.Update(address);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(Guid id)
{
var address = await _context.Addresses.FindAsync(id);
if (address != null)
{
_context.Addresses.Remove(address);
await _context.SaveChangesAsync();
}
}
}
}