Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1888:

  • Users now contain ContactInformation
  • Fixed formatting in invoice template
  • Fixed calculation for next invoice date
  • Worked on indentation for invoice fields
File size: 7.7 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;
8namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
9  public class BillingDao {
10
11    #region Context Creation and Execution
12
13    private BillingContext CreateContext() {
14      BillingContext context = new BillingContext("name=BillingContext");
15      return context;
16    }
17
18    private delegate T ExecuteWithContextDelegate<T>(BillingContext context);
19    private delegate void ExecuteWithContextDelegate(BillingContext context);
20
21    private void ExecuteWithContext(ExecuteWithContextDelegate call) {
22      BillingContext context = null;
23      try {
24        using (context = CreateContext()) {
25          call(context);
26        }
27      }
28      finally {
29        if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
30          context.Database.Connection.Close();
31        }
32      }
33    }
34
35    private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {
36      BillingContext context = null;
37      try {
38        using (context = CreateContext()) {
39          T result = call(context);
40          return result;
41        }
42      }
43      finally {
44        if (context.Database.Connection.State == System.Data.ConnectionState.Open) {
45          context.Database.Connection.Close();
46        }
47      }
48    }
49
50    #endregion
51
52    #region Product Methods
53
54    public Product FindProductById(long id) {
55      return ExecuteWithContext<Product>((context) => {
56        return context.Products.FirstOrDefault(prod => prod.ProductId == id);
57      });
58    }
59
60    public IEnumerable<Product> FindProdcutBy(Expression<Func<Product, bool>> predicate) {
61      using (var context = CreateContext()) {
62        var products = context.Products.Where(predicate).ToList();
63        return products;
64      }
65    }
66
67    public IEnumerable<Product> FindAllProducts() {
68      using (var context = CreateContext()) {
69        var products = context.Products.ToList();
70        return products;
71      }
72    }
73
74    public void AddProduct(Product entity) {
75      ExecuteWithContext((context) => {
76        context.Products.Add(entity);
77        context.SaveChanges();
78      });
79    }
80
81    public void UpdateProduct(Product entity) {
82      ExecuteWithContext((context) => {
83        context.Products.Attach(entity);
84        context.Entry(entity).State = System.Data.EntityState.Modified;
85        context.SaveChanges();
86      });
87    }
88
89    void DeleteProduct(Product entity) {
90      ExecuteWithContext((context) => {
91        context.Products.Remove(entity);
92      });
93    }
94
95    #endregion
96
97    #region Order Methods
98
99    public void AddOrder(Order order) {
100      ExecuteWithContext((context) => {
101        context.Orders.Add(order);
102        context.SaveChanges();
103      });
104    }
105
106    public IEnumerable<Order> FindOrderBy(Expression<Func<Order, bool>> predicate) {
107      using (var context = CreateContext()) {
108        var orders = context.Orders
109          .Include(o => o.OrderLines)
110          .Include(o => o.User)
111          .Include(o => o.User.ContactInformation)
112          .Include(o => o.Invoices)
113          .Include(o => o.OrderLines.Select(ol => ol.Product))
114          .Where(predicate).ToList();
115        return orders;
116      }
117    }
118
119    #endregion
120
121    #region OrderLine Methods
122
123    public OrderLine FindOrderLineById(long id) {
124      return ExecuteWithContext<OrderLine>((context) => {
125        return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id);
126      });
127    }
128
129    public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) {
130      using (var context = CreateContext()) {
131        var orderLines = context.OrderLines.Where(predicate).ToList();
132        return orderLines;
133      }
134    }
135
136    public IEnumerable<OrderLine> FindAllOrderLines() {
137      using (var context = CreateContext()) {
138        var orderLines = context.OrderLines.ToList();
139        return orderLines;
140      }
141    }
142
143    public void AddOrderLine(OrderLine entity) {
144      ExecuteWithContext((context) => {
145        context.OrderLines.Add(entity);
146        context.SaveChanges();
147      });
148    }
149
150    public void UpdateOrderLine(OrderLine entity) {
151      ExecuteWithContext((context) => {
152        context.OrderLines.Attach(entity);
153        context.Entry(entity).State = System.Data.EntityState.Modified;
154        context.SaveChanges();
155      });
156    }
157
158    void DeleteOrderLine(OrderLine entity) {
159      ExecuteWithContext((context) => {
160        context.OrderLines.Remove(entity);
161      });
162    }
163
164    #endregion
165
166    #region User Methods
167
168    public User FindUserById(long id) {
169      return ExecuteWithContext<User>((context) => {
170        return context.Users.FirstOrDefault(user => user.UserId == id);
171      });
172    }
173
174    public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) {
175      using (var context = CreateContext()) {
176        var users = context.Users.Where(predicate).ToList();
177        return users;
178      }
179    }
180
181    public IEnumerable<User> FindAllUsers() {
182      using (var context = CreateContext()) {
183        var users = context.Users.ToList();
184        return users;
185      }
186    }
187
188    public void AddUser(User entity) {
189      ExecuteWithContext((context) => {
190        context.Users.Add(entity);
191        context.SaveChanges();
192      });
193    }
194
195    public void UpdateUser(User entity) {
196      ExecuteWithContext((context) => {
197        context.Users.Attach(entity);
198        context.Entry(entity).State = System.Data.EntityState.Modified;
199        context.SaveChanges();
200      });
201    }
202
203    void DeleteUser(User entity) {
204      ExecuteWithContext((context) => {
205        context.Users.Remove(entity);
206      });
207    }
208
209    #endregion
210
211    #region UsageRecords Methods
212
213    public UsageRecord FindUsageRecordById(long id) {
214      return ExecuteWithContext<UsageRecord>((context) => {
215        return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id);
216      });
217    }
218
219    public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) {
220      using (var context = CreateContext()) {
221        var records = context.UsageRecords.Where(predicate).ToList();
222        return records;
223      }
224    }
225
226    public IEnumerable<UsageRecord> FindAllUsageRecords() {
227      using (var context = CreateContext()) {
228        var records = context.UsageRecords.ToList();
229        return records;
230      }
231    }
232
233    public void AddUsageRecord(UsageRecord entity) {
234      ExecuteWithContext((context) => {
235        context.UsageRecords.Add(entity);
236        context.SaveChanges();
237      });
238    }
239
240    public void UpdateUsageRecord(UsageRecord entity) {
241      ExecuteWithContext((context) => {
242        context.UsageRecords.Attach(entity);
243        context.Entry(entity).State = System.Data.EntityState.Modified;
244        context.SaveChanges();
245      });
246    }
247
248    void DeleteUsageRecord(UsageRecord entity) {
249      ExecuteWithContext((context) => {
250        context.UsageRecords.Remove(entity);
251      });
252    }
253
254    #endregion
255
256    #region Invoice Methods
257
258    public void AddInvoice(Invoice entity) {
259      ExecuteWithContext((context) => {
260        context.Invoices.Add(entity);
261        context.SaveChanges();
262      });
263    }
264
265    public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) {
266      using (var context = CreateContext()) {
267        var invoices = context.Invoices.Where(predicate).ToList();
268        return invoices;
269      }
270    }
271
272    #endregion
273  }
274}
Note: See TracBrowser for help on using the repository browser.