Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingDao.cs @ 14604

Last change on this file since 14604 was 9653, checked in by spimming, 11 years ago

#1888:

  • Set flag to indicate a modified model object
  • Add dao query method to specify includes as parameter
  • cleaned up the using of namespaces to qualify types
File size: 6.2 KB
RevLine 
[9577]1
2using System;
3using System.Collections.Generic;
[9619]4using System.Data.Entity;
[9577]5using System.Linq;
6using System.Linq.Expressions;
7using HeuristicLab.Services.Optimization.Billing.Model;
[9645]8using HeuristicLab.Services.Optimization.Billing.Utils;
[9577]9namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
10  public class BillingDao {
11
[9653]12    #region Context Creation
[9577]13
14    private BillingContext CreateContext() {
15      BillingContext context = new BillingContext("name=BillingContext");
16      return context;
17    }
18
19    #endregion
20
21    #region Product Methods
22
23    public Product FindProductById(long id) {
[9645]24      using (var context = CreateContext()) {
[9577]25        return context.Products.FirstOrDefault(prod => prod.ProductId == id);
[9645]26      }
[9577]27    }
28
29    public IEnumerable<Product> FindProdcutBy(Expression<Func<Product, bool>> predicate) {
30      using (var context = CreateContext()) {
31        var products = context.Products.Where(predicate).ToList();
32        return products;
33      }
34    }
35
[9653]36    public IEnumerable<Product> FindProductBy(Expression<Func<Product, bool>> query, params Expression<Func<Product, object>>[] includes) {
37      using (var context = CreateContext()) {
38        IQueryable<Product> products = context.Products.Where(query);
39
40        if (includes != null) {
41          products = includes.Aggregate(products, (current, include) => current.Include(include));
42        }
43
44        return products.ToList();
45      }
46    }
47
[9577]48    public IEnumerable<Product> FindAllProducts() {
49      using (var context = CreateContext()) {
50        var products = context.Products.ToList();
51        return products;
52      }
53    }
54
[9645]55    public void InsertOrUpdateProduct(Product entity) {
56      using (var context = CreateContext()) {
57        throw new NotImplementedException();
58      }
[9577]59    }
60
61    void DeleteProduct(Product entity) {
[9645]62      using (var context = CreateContext()) {
63        throw new NotImplementedException();
64      }
[9577]65    }
66
67    #endregion
68
69    #region Order Methods
70
[9645]71    public void InsertOrUpdateOrder(Order entity) {
72      using (var context = CreateContext()) {
73        context.Orders.Add(entity);
74        context.ApplyStateChanges();
[9577]75        context.SaveChanges();
[9645]76      }
[9577]77    }
78
79    public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
80      using (var context = CreateContext()) {
[9619]81        var orders = context.Orders
82          .Include(o => o.OrderLines)
83          .Include(o => o.User)
[9641]84          .Include(o => o.User.ContactInformation)
[9619]85          .Include(o => o.Invoices)
86          .Include(o => o.OrderLines.Select(ol => ol.Product))
87          .Where(predicate).ToList();
[9577]88        return orders;
89      }
90    }
91
92    #endregion
[9602]93
94    #region OrderLine Methods
95
96    public OrderLine FindOrderLineById(long id) {
[9645]97      using (var context = CreateContext()) {
[9602]98        return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
[9645]99      }
[9602]100    }
101
102    public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) {
103      using (var context = CreateContext()) {
104        var orderLines = context.OrderLines.Where(predicate).ToList();
105        return orderLines;
106      }
107    }
108
109    public IEnumerable<OrderLine> FindAllOrderLines() {
110      using (var context = CreateContext()) {
111        var orderLines = context.OrderLines.ToList();
112        return orderLines;
113      }
114    }
115
[9645]116    public void InsertOrUpdateOrderLine(OrderLine entity) {
117      using (var context = CreateContext()) {
118        throw new NotImplementedException();
119      }
[9602]120    }
121
122    void DeleteOrderLine(OrderLine entity) {
[9645]123      using (var context = CreateContext()) {
124        throw new NotImplementedException();
125      }
[9602]126    }
127
128    #endregion
129
130    #region User Methods
131
132    public User FindUserById(long id) {
[9645]133      using (var context = CreateContext()) {
[9602]134        return context.Users.FirstOrDefault(user => user.UserId == id);
[9645]135      }
[9602]136    }
137
138    public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) {
139      using (var context = CreateContext()) {
140        var users = context.Users.Where(predicate).ToList();
141        return users;
142      }
143    }
144
145    public IEnumerable<User> FindAllUsers() {
146      using (var context = CreateContext()) {
147        var users = context.Users.ToList();
148        return users;
149      }
150    }
151
[9645]152    public void InsertOrUpdateUser(User entity) {
153      using (var context = CreateContext()) {
154        throw new NotImplementedException();
155      }
[9602]156    }
157
158    void DeleteUser(User entity) {
[9645]159      using (var context = CreateContext()) {
160        throw new NotImplementedException();
161      }
[9602]162    }
163
164    #endregion
165
166    #region UsageRecords Methods
167
168    public UsageRecord FindUsageRecordById(long id) {
[9645]169      using (var context = CreateContext()) {
[9602]170        return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
[9645]171      }
[9602]172    }
173
174    public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) {
175      using (var context = CreateContext()) {
176        var records = context.UsageRecords.Where(predicate).ToList();
177        return records;
178      }
179    }
180
181    public IEnumerable<UsageRecord> FindAllUsageRecords() {
182      using (var context = CreateContext()) {
183        var records = context.UsageRecords.ToList();
184        return records;
185      }
186    }
187
[9645]188    public void InsertOrUpdateUsageRecord(UsageRecord entity) {
189      using (var context = CreateContext()) {
190        throw new NotImplementedException();
191      }
[9602]192    }
193
194    void DeleteUsageRecord(UsageRecord entity) {
[9645]195      using (var context = CreateContext()) {
196        throw new NotImplementedException();
197      }
[9602]198    }
199
200    #endregion
201
202    #region Invoice Methods
203
[9645]204    public void InsertOrUpdateInvoice(Invoice entity) {
205      using (var context = CreateContext()) {
[9602]206        context.Invoices.Add(entity);
[9645]207        context.ApplyStateChanges();
[9602]208        context.SaveChanges();
[9645]209      }
[9602]210    }
211
212    public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) {
213      using (var context = CreateContext()) {
214        var invoices = context.Invoices.Where(predicate).ToList();
215        return invoices;
216      }
217    }
218
219    #endregion
[9577]220  }
221}
Note: See TracBrowser for help on using the repository browser.