Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs @ 9619

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

#1888:

  • Added new BillingService methods
  • Disabled proxy generation and lazy loading!
  • Extended see method with additional test data
  • Added properties to order and invoice model
  • initial commit of BillingEngine module
File size: 6.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Data.Entity;
4using System.Data.Entity.ModelConfiguration;
5using System.Data.Entity.ModelConfiguration.Conventions;
6using HeuristicLab.Services.Optimization.Billing.Model;
7
8namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
9  public class BillingContext : DbContext {
10    public DbSet<Product> Products { get; set; }
11    public DbSet<User> Users { get; set; }
12    public DbSet<Order> Orders { get; set; }
13    public DbSet<OrderLine> OrderLines { get; set; }
14    public DbSet<Invoice> Invoices { get; set; }
15    public DbSet<InvoiceLine> InvoiceLines { get; set; }
16    public DbSet<UsageRecord> UsageRecords { get; set; }
17    public DbSet<ContactInformation> ContactInformations { get; set; }
18    public DbSet<PaymentInformation> PaymentInformations { get; set; }
19
20    // enum PaymentMethod
21    // enum OrderState
22
23    public BillingContext()
24      : this("name=BillingContext") {
25
26    }
27
28    public BillingContext(string connectionString)
29      : base(connectionString) {
30      this.Configuration.LazyLoadingEnabled = false;
31      this.Configuration.ProxyCreationEnabled = false;
32    }
33
34    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
35      modelBuilder.Configurations.Add(new OrderConfiguration());
36      modelBuilder.Configurations.Add(new OrderLineConfiguration());
37      modelBuilder.Configurations.Add(new InvoiceConfiguration());
38      modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
39      modelBuilder.Configurations.Add(new UsageRecordConfiguration());
40      modelBuilder.Configurations.Add(new UserConfiguration());
41
42      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
43
44      base.OnModelCreating(modelBuilder);
45    }
46
47    #region EntityTypeConfigurations
48
49    private class OrderConfiguration : EntityTypeConfiguration<Order> {
50      internal OrderConfiguration() {
51        this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId);
52        // TODO: OrderState?
53      }
54    }
55
56    private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> {
57      internal OrderLineConfiguration() {
58        this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
59        this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
60      }
61    }
62
63    private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
64      internal InvoiceConfiguration() {
65        this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
66        this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
67      }
68    }
69
70    private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
71      internal InvoiceLineConfiguration() {
72        this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
73        this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
74      }
75    }
76
77    private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
78      internal UsageRecordConfiguration() {
79        this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
80        this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
81      }
82    }
83
84    private class UserConfiguration : EntityTypeConfiguration<User> {
85      internal UserConfiguration() {
86        this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId);
87      }
88    }
89
90    #endregion
91
92    #region DB Seed Methods
93
94    // - DropCreateDatabaseAlways<BillingContext>
95    // - DropCreateDatabaseIfModelChanges<BillingContext>
96    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
97      protected override void Seed(BillingContext context) {
98        Product p1 = new Product() {
99          Name = "OaaS BAsic Service Charge",
100          Description = "Create and run your experiments within HeuristicLab Hive",
101          Price = 20.0,
102          ProductType = "Optimization Service"
103        };
104
105        Product p2 = new Product() {
106          Name = "Oaas Usage Charges",
107          Description = "Charges for OaaS based on usage data",
108          Price = 0.0,
109          ProductType = "Optimization Service"
110        };
111
112        Product p3 = new Product() {
113          Name = "Oaas Support Package",
114          Description = "Charges for OaaS based on usage data",
115          Price = 50.0,
116          ProductType = "Optimization Service"
117        };
118
119        p1 = context.Products.Add(p1);
120        p2 = context.Products.Add(p2);
121        p3 = context.Products.Add(p3);
122
123        User u1 = new Model.User() { Name = "spimming" };
124        User u2 = new Model.User() {
125          Name = "fschoeppl",
126          PaymentInformation = new List<PaymentInformation>() {
127            new PaymentInformation() {
128              PaymentMethod = PaymentMethod.Visa,
129              CardNumber = "123456789" } }
130        };
131
132        u1 = context.Users.Add(u1);
133        u2 = context.Users.Add(u2);
134
135        ContactInformation ci1 = new ContactInformation() {
136          FirstName = "Max",
137          LastName = "Mustermann",
138          Email = "max.mustermann@fh-hagenberg.at",
139          OrganizationName = "University of Applied Sciences Upper Austria School of Informatics/Communications/Media",
140          Street = "Softwarepark 11",
141          PostalCode = "4232",
142          City = "Hagenberg"
143        };
144
145        ci1 = context.ContactInformations.Add(ci1);
146
147        Order o1 = new Order() {
148          User = u2,
149          State = OrderState.Created,
150          BillingType = BillingType.Post,
151          BillingPeriod = BillingPeriod.Monthly,
152        };
153        OrderLine ol1 = new OrderLine() {
154          Order = o1,
155          Product = p1,
156          ProductPrice = p1.Price,
157          Quantity = 1
158        };
159        o1 = context.Orders.Add(o1);
160        ol1 = context.OrderLines.Add(ol1);
161
162        context.SaveChanges();
163
164        o1.State = OrderState.Active;
165        o1.ActiveSince = DateTime.Now;
166        context.Entry(o1).State = System.Data.EntityState.Modified;
167        context.SaveChanges();
168      }
169    }
170
171    #endregion
172  }
173}
Note: See TracBrowser for help on using the repository browser.