Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15490 was 9653, checked in by spimming, 12 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
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Data.Entity;
5using System.Linq;
6using System.Linq.Expressions;
7using HeuristicLab.Services.Optimization.Billing.Model;
8using HeuristicLab.Services.Optimization.Billing.Utils;
9namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
10  public class BillingDao {
11
12    #region Context Creation
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) {
24      using (var context = CreateContext()) {
25        return context.Products.FirstOrDefault(prod => prod.ProductId == id);
26      }
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
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
48    public IEnumerable<Product> FindAllProducts() {
49      using (var context = CreateContext()) {
50        var products = context.Products.ToList();
51        return products;
52      }
53    }
54
55    public void InsertOrUpdateProduct(Product entity) {
56      using (var context = CreateContext()) {
57        throw new NotImplementedException();
58      }
59    }
60
61    void DeleteProduct(Product entity) {
62      using (var context = CreateContext()) {
63        throw new NotImplementedException();
64      }
65    }
66
67    #endregion
68
69    #region Order Methods
70
71    public void InsertOrUpdateOrder(Order entity) {
72      using (var context = CreateContext()) {
73        context.Orders.Add(entity);
74        context.ApplyStateChanges();
75        context.SaveChanges();
76      }
77    }
78
79    public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
80      using (var context = CreateContext()) {
81        var orders = context.Orders
82          .Include(o => o.OrderLines)
83          .Include(o => o.User)
84          .Include(o => o.User.ContactInformation)
85          .Include(o => o.Invoices)
86          .Include(o => o.OrderLines.Select(ol => ol.Product))
87          .Where(predicate).ToList();
88        return orders;
89      }
90    }
91
92    #endregion
93
94    #region OrderLine Methods
95
96    public OrderLine FindOrderLineById(long id) {
97      using (var context = CreateContext()) {
98        return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
99      }
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
116    public void InsertOrUpdateOrderLine(OrderLine entity) {
117      using (var context = CreateContext()) {
118        throw new NotImplementedException();
119      }
120    }
121
122    void DeleteOrderLine(OrderLine entity) {
123      using (var context = CreateContext()) {
124        throw new NotImplementedException();
125      }
126    }
127
128    #endregion
129
130    #region User Methods
131
132    public User FindUserById(long id) {
133      using (var context = CreateContext()) {
134        return context.Users.FirstOrDefault(user => user.UserId == id);
135      }
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
152    public void InsertOrUpdateUser(User entity) {
153      using (var context = CreateContext()) {
154        throw new NotImplementedException();
155      }
156    }
157
158    void DeleteUser(User entity) {
159      using (var context = CreateContext()) {
160        throw new NotImplementedException();
161      }
162    }
163
164    #endregion
165
166    #region UsageRecords Methods
167
168    public UsageRecord FindUsageRecordById(long id) {
169      using (var context = CreateContext()) {
170        return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
171      }
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
188    public void InsertOrUpdateUsageRecord(UsageRecord entity) {
189      using (var context = CreateContext()) {
190        throw new NotImplementedException();
191      }
192    }
193
194    void DeleteUsageRecord(UsageRecord entity) {
195      using (var context = CreateContext()) {
196        throw new NotImplementedException();
197      }
198    }
199
200    #endregion
201
202    #region Invoice Methods
203
204    public void InsertOrUpdateInvoice(Invoice entity) {
205      using (var context = CreateContext()) {
206        context.Invoices.Add(entity);
207        context.ApplyStateChanges();
208        context.SaveChanges();
209      }
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
220  }
221}
Note: See TracBrowser for help on using the repository browser.