Newer
Older
CQRSRepository / src / ApplicationCore / Data / CatalogContext.cs
@Derek Comartin Derek Comartin on 27 Oct 2021 1 KB Init
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
using System.Reflection;

namespace Microsoft.eShopWeb.Infrastructure.Data
{
    public class CatalogContext : DbContext
    {
        public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
        {
        }

        public DbSet<Basket> Baskets { get; set; }
        public DbSet<CatalogItem> CatalogItems { get; set; }
        public DbSet<CatalogBrand> CatalogBrands { get; set; }
        public DbSet<CatalogType> CatalogTypes { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderItem> OrderItems { get; set; }
        public DbSet<BasketItem> BasketItems { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        }
    }

    public static class CatalogContextExtensions
    {
        public static IQueryable<Order> CustomerOrdersWithItems(this CatalogContext dbContext, string buyerId)
        {
            return dbContext.Orders.Where(o => o.BuyerId == buyerId)
                .Include(o => o.OrderItems)
                .ThenInclude(i => i.ItemOrdered)
                .AsQueryable();
        }
    }
}