Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9641 was 9641, checked in by spimming, 10 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.0 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        this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId);
88      }
89    }
90
91    #endregion
92
93    #region DB Seed Methods
94
95    // - DropCreateDatabaseAlways<BillingContext>
96    // - DropCreateDatabaseIfModelChanges<BillingContext>
97    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
98      protected override void Seed(BillingContext context) {
99        Product p1 = new Product() {
100          Name = "OaaS Basic Service Charge",
101          Description = "Create and run your experiments within HeuristicLab Hive",
102          Price = 20.0,
103          ProductType = "Optimization Service"
104        };
105
106        Product p2 = new Product() {
107          Name = "Oaas Usage Charges",
108          Description = "Charges for OaaS based on usage data",
109          Price = 0.0,
110          ProductType = "Optimization Service"
111        };
112
113        Product p3 = new Product() {
114          Name = "Oaas Support Package",
115          Description = "Charges for OaaS based on usage data",
116          Price = 50.0,
117          ProductType = "Optimization Service"
118        };
119
120        p1 = context.Products.Add(p1);
121        p2 = context.Products.Add(p2);
122        p3 = context.Products.Add(p3);
123
124        User u1 = new Model.User() { Name = "spimming" };
125        User u2 = new Model.User() {
126          Name = "fschoeppl",
127          PaymentInformation = new List<PaymentInformation>() {
128            new PaymentInformation() {
129              PaymentMethod = PaymentMethod.Visa,
130              CardNumber = "123456789" } }
131        };
132
133        u1 = context.Users.Add(u1);
134        u2 = context.Users.Add(u2);
135
136        ContactInformation ci1 = new ContactInformation() {
137          FirstName = "Max",
138          LastName = "Mustermann",
139          Email = "max.mustermann@fh-hagenberg.at",
140          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
141          Street = "Softwarepark 11",
142          PostalCode = "4232",
143          City = "Hagenberg",
144        };
145
146        ci1 = context.ContactInformations.Add(ci1);
147        u1.ContactInformation = ci1;
148
149        ContactInformation ci2 = new ContactInformation() {
150          FirstName = "Max",
151          LastName = "Mustermann",
152          Email = "max.mustermann@fh-hagenberg.at",
153          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
154          Street = "Softwarepark 11",
155          PostalCode = "4232",
156          City = "Hagenberg",
157        };
158
159        ci2 = context.ContactInformations.Add(ci2);
160        u2.ContactInformation = ci2;
161
162        Order o1 = new Order() {
163          User = u2,
164          State = OrderState.Created,
165          BillingType = BillingType.Post,
166          BillingPeriod = BillingPeriod.Monthly,
167        };
168        OrderLine ol1 = new OrderLine() {
169          Order = o1,
170          Product = p1,
171          ProductPrice = p1.Price,
172          Quantity = 1
173        };
174        o1 = context.Orders.Add(o1);
175        ol1 = context.OrderLines.Add(ol1);
176
177        context.SaveChanges();
178
179        o1.State = OrderState.Active;
180        o1.ActiveSince = DateTime.Now;
181        context.Entry(o1).State = System.Data.EntityState.Modified;
182        context.SaveChanges();
183      }
184    }
185
186    #endregion
187  }
188}
Note: See TracBrowser for help on using the repository browser.