using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using HeuristicLab.Services.Optimization.Billing.Model; using HeuristicLab.Services.Optimization.Billing.Utils; namespace HeuristicLab.Services.Optimization.Billing.DataAccess { public class BillingDao { #region Context Creation private BillingContext CreateContext() { BillingContext context = new BillingContext("name=BillingContext"); return context; } #endregion #region Product Methods public Product FindProductById(long id) { using (var context = CreateContext()) { return context.Products.FirstOrDefault(prod => prod.ProductId == id); } } public IEnumerable FindProdcutBy(Expression> predicate) { using (var context = CreateContext()) { var products = context.Products.Where(predicate).ToList(); return products; } } public IEnumerable FindProductBy(Expression> query, params Expression>[] includes) { using (var context = CreateContext()) { IQueryable products = context.Products.Where(query); if (includes != null) { products = includes.Aggregate(products, (current, include) => current.Include(include)); } return products.ToList(); } } public IEnumerable FindAllProducts() { using (var context = CreateContext()) { var products = context.Products.ToList(); return products; } } public void InsertOrUpdateProduct(Product entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } void DeleteProduct(Product entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } #endregion #region Order Methods public void InsertOrUpdateOrder(Order entity) { using (var context = CreateContext()) { context.Orders.Add(entity); context.ApplyStateChanges(); context.SaveChanges(); } } public IEnumerable FindOrderBy(Expression> predicate) { using (var context = CreateContext()) { var orders = context.Orders .Include(o => o.OrderLines) .Include(o => o.User) .Include(o => o.User.ContactInformation) .Include(o => o.Invoices) .Include(o => o.OrderLines.Select(ol => ol.Product)) .Where(predicate).ToList(); return orders; } } #endregion #region OrderLine Methods public OrderLine FindOrderLineById(long id) { using (var context = CreateContext()) { return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id); } } public IEnumerable FindOrderLineBy(Expression> predicate) { using (var context = CreateContext()) { var orderLines = context.OrderLines.Where(predicate).ToList(); return orderLines; } } public IEnumerable FindAllOrderLines() { using (var context = CreateContext()) { var orderLines = context.OrderLines.ToList(); return orderLines; } } public void InsertOrUpdateOrderLine(OrderLine entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } void DeleteOrderLine(OrderLine entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } #endregion #region User Methods public User FindUserById(long id) { using (var context = CreateContext()) { return context.Users.FirstOrDefault(user => user.UserId == id); } } public IEnumerable FindUserBy(Expression> predicate) { using (var context = CreateContext()) { var users = context.Users.Where(predicate).ToList(); return users; } } public IEnumerable FindAllUsers() { using (var context = CreateContext()) { var users = context.Users.ToList(); return users; } } public void InsertOrUpdateUser(User entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } void DeleteUser(User entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } #endregion #region UsageRecords Methods public UsageRecord FindUsageRecordById(long id) { using (var context = CreateContext()) { return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id); } } public IEnumerable FindUsageRecordsBy(Expression> predicate) { using (var context = CreateContext()) { var records = context.UsageRecords.Where(predicate).ToList(); return records; } } public IEnumerable FindAllUsageRecords() { using (var context = CreateContext()) { var records = context.UsageRecords.ToList(); return records; } } public void InsertOrUpdateUsageRecord(UsageRecord entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } void DeleteUsageRecord(UsageRecord entity) { using (var context = CreateContext()) { throw new NotImplementedException(); } } #endregion #region Invoice Methods public void InsertOrUpdateInvoice(Invoice entity) { using (var context = CreateContext()) { context.Invoices.Add(entity); context.ApplyStateChanges(); context.SaveChanges(); } } public IEnumerable FindInvoiceBy(Expression> predicate) { using (var context = CreateContext()) { var invoices = context.Invoices.Where(predicate).ToList(); return invoices; } } #endregion } }