Add Order/Address/OrderAddress repos, and handlers
This commit is contained in:
@@ -11,7 +11,6 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
|
||||
public DbSet<Product> Products { get; set; }
|
||||
public DbSet<ProductVariant> ProductVariants { get; set; }
|
||||
public DbSet<Order> Orders { get; set; }
|
||||
public DbSet<OrderAddress> OrderAddresses { get; set; }
|
||||
public DbSet<Address> Addresses { get; set; }
|
||||
public DbSet<OrderStatus> OrderStatuses { get; set; }
|
||||
public DbSet<ShippingStatus> ShippingStatuses { get; set; }
|
||||
@@ -19,6 +18,7 @@ public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
|
||||
public DbSet<UserRole> UserRole { get; set; }
|
||||
public DbSet<Role> Roles { get; set; }
|
||||
public DbSet<Category> Categories { get; set; }
|
||||
public DbSet<OrderAddress> OrderAddresses { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using Imprink.Domain.Entities;
|
||||
using Imprink.Domain.Repositories;
|
||||
using Imprink.Infrastructure.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Imprink.Infrastructure.Repositories;
|
||||
|
||||
public class OrderAddressRepository(ApplicationDbContext context) : IOrderAddressRepository
|
||||
{
|
||||
|
||||
public async Task<OrderAddress?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.OrderAddresses
|
||||
.Include(oa => oa.Order)
|
||||
.FirstOrDefaultAsync(oa => oa.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<OrderAddress?> GetByOrderIdAsync(Guid orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.OrderAddresses
|
||||
.Include(oa => oa.Order)
|
||||
.FirstOrDefaultAsync(oa => oa.OrderId == orderId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrderAddress>> GetByOrderIdsAsync(IEnumerable<Guid> orderIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.OrderAddresses
|
||||
.Include(oa => oa.Order)
|
||||
.Where(oa => orderIds.Contains(oa.OrderId))
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<OrderAddress> AddAsync(OrderAddress orderAddress, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entry = await context.OrderAddresses.AddAsync(orderAddress, cancellationToken);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return entry.Entity;
|
||||
}
|
||||
|
||||
public async Task<OrderAddress> UpdateAsync(OrderAddress orderAddress, CancellationToken cancellationToken = default)
|
||||
{
|
||||
context.OrderAddresses.Update(orderAddress);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return orderAddress;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var orderAddress = await context.OrderAddresses.FindAsync([id], cancellationToken);
|
||||
if (orderAddress == null)
|
||||
return false;
|
||||
|
||||
context.OrderAddresses.Remove(orderAddress);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteByOrderIdAsync(Guid orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var orderAddress = await context.OrderAddresses.FirstOrDefaultAsync(oa => oa.OrderId == orderId, cancellationToken);
|
||||
if (orderAddress == null)
|
||||
return false;
|
||||
|
||||
context.OrderAddresses.Remove(orderAddress);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.OrderAddresses.AnyAsync(oa => oa.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> ExistsByOrderIdAsync(Guid orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await context.OrderAddresses.AnyAsync(oa => oa.OrderId == orderId, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,8 @@ public class UnitOfWork(
|
||||
IUserRoleRepository userRoleRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IOrderRepository orderRepository,
|
||||
IAddressRepository addressRepository) : IUnitOfWork
|
||||
IAddressRepository addressRepository,
|
||||
IOrderAddressRepository orderAddressRepository) : IUnitOfWork
|
||||
{
|
||||
public IProductRepository ProductRepository => productRepository;
|
||||
public IProductVariantRepository ProductVariantRepository => productVariantRepository;
|
||||
@@ -23,6 +24,7 @@ public class UnitOfWork(
|
||||
public IRoleRepository RoleRepository => roleRepository;
|
||||
public IOrderRepository OrderRepository => orderRepository;
|
||||
public IAddressRepository AddressRepository => addressRepository;
|
||||
public IOrderAddressRepository OrderAddressRepository => orderAddressRepository;
|
||||
|
||||
public async Task SaveAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user