Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/MockupBillingService.cs @ 12464

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

#1888:

  • Model classes implement new interface to track current state of entity
  • Mark EntityState property as 'not mapped'
  • Set hook to initialize entity as unchanged
  • Extension method for DbContext to apply changes only on modified entity
File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Services.Optimization.Billing.Interfaces;
5
6namespace HeuristicLab.Services.Optimization.Billing.Business {
7  public class MockupBillingService : IOptimizationBilling {
8
9    private static IList<Model.Product> products;
10    private static IDictionary<string, IList<Model.Order>> userToOrders;
11    private static IList<Model.Order> allOrders;
12    private static IDictionary<string, Model.User> users;
13    private static IDictionary<string, IList<Model.UsageRecord>> records;
14    private static IDictionary<string, IList<Model.Invoice>> invoices;
15    private static long maxId = 1;
16
17    static MockupBillingService() {
18      Model.Product oaas;
19      products = new List<Model.Product>();
20
21      products.Add(oaas = new Model.Product() {
22        Name = "Optimization-as-a-Service - Experiment execution",
23        Description = "Create and run your experiments within HeuristicLab Hive",
24        ProductId = 1,
25        Price = 10.0,
26        ProductType = "Optimization Service"
27      });
28      products.Add(new Model.Product() {
29        Name = "Whatever-o-matic - Experiment execution",
30        Description = "Another fine product",
31        ProductId = 2,
32        Price = 100.0,
33        ProductType = "Product"
34      });
35
36      userToOrders = new Dictionary<string, IList<Model.Order>>();
37      allOrders = new List<Model.Order>();
38      users = new Dictionary<string, Model.User>();
39      records = new Dictionary<string, IList<Model.UsageRecord>>();
40      records["fschoeppl"] = new List<Model.UsageRecord>() {
41        new Model.UsageRecord() { ProductId = 1, Product = oaas, UsageRecordId = 1, User = new Model.User() { Name = "fschoeppl", UserId = 1 }, Begin = DateTime.Now, End = DateTime.Now.AddHours(2), UserId = 1, ResourceIdentifier = 0, ServiceIdentifier = 1 }
42      };
43      invoices = new Dictionary<string, IList<Model.Invoice>>();
44      invoices["fschoeppl"] = new List<Model.Invoice>() {
45        new Model.Invoice() {
46          Due = DateTime.Now.AddDays(-1),
47          Status = Model.InvoiceStatus.Overdue, 
48          InvoiceId = 1, OrderId = 1,
49          InvoiceLines = new List<Model.InvoiceLine>() {
50            new Model.InvoiceLine() { Product = oaas, ProductId = oaas.ProductId, ProductPrice = oaas.Price, Quantity = 2, InvoiceLineId = 1, InvoiceId = 1 }
51          },
52          UserId = 1, User = new Model.User() { UserId = 1, Name = "fschoeppl" }
53        }
54      };
55    }
56
57    public IList<Model.Product> GetProducts() {
58      return products;
59    }
60
61    public void SaveOrder(Model.Order order) {
62      IList<Model.Order> orders;
63      if (!userToOrders.TryGetValue(order.User.Name, out orders)) {
64        userToOrders[order.User.Name] = orders = new List<Model.Order>();
65      }
66      var oldOrder = (from o in orders where o.OrderId == order.OrderId select o).FirstOrDefault();
67      if (oldOrder == null) {
68        order.OrderId = maxId++;
69        orders.Add(order);
70        allOrders.Add(order);
71      } else {
72        oldOrder.Invoices = order.Invoices;
73        oldOrder.OrderLines = order.OrderLines;
74        oldOrder.State = order.State;
75        oldOrder.ActiveSince = order.ActiveSince;
76        oldOrder.ActiveUntil = order.ActiveUntil;
77        oldOrder.BillingPeriod = order.BillingPeriod;
78        oldOrder.BillingType = order.BillingType;
79      }
80    }
81
82    public IList<Model.Order> GetOrdersByState(Model.OrderState state) {
83      return (from o in allOrders where o.State == state select o).ToList();
84    }
85
86
87    public IList<Model.Order> GetOrders(string userName) {
88      return (from o in allOrders where o.User.Name == userName select o).ToList();
89    }
90
91
92    public Model.User GetUser(string userName) {
93      Model.User user;
94      if (!users.TryGetValue(userName, out user))
95        return null;
96      return user;
97    }
98
99    public void SaveUser(Model.User user) {
100      users[user.Name] = user;
101    }
102
103
104    public IList<Model.UsageRecord> GetUsageRecords(string userName) {
105      IList<Model.UsageRecord> foundRecords;
106      if (records.TryGetValue(userName, out foundRecords)) {
107        return foundRecords;
108      }
109      return null;
110    }
111
112
113    public IList<Model.Invoice> GetInvoices(string userName) {
114      IList<Model.Invoice> foundInvoices;
115      if (invoices.TryGetValue(userName, out foundInvoices)) {
116        return foundInvoices;
117      }
118      return null;
119    }
120
121    #region IOptimizationBilling Members
122
123
124    public IList<Model.Product> GetProductByName(string productName) {
125      throw new NotImplementedException();
126    }
127
128    public void UpdateOrder(Model.Order order) {
129      throw new NotImplementedException();
130    }
131
132    public IList<Model.UsageRecord> GetUsageRecords(string username, DateTime from, DateTime to) {
133      throw new NotImplementedException();
134    }
135
136    public void SaveInvoice(Model.Invoice invoice) {
137      throw new NotImplementedException();
138    }
139
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.