- Timestamp:
- 06/10/13 16:32:19 (12 years ago)
- Location:
- branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs
r9579 r9602 1 using System.Data.Entity; 1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity; 2 4 using System.Data.Entity.ModelConfiguration; 3 5 using System.Data.Entity.ModelConfiguration.Conventions; … … 6 8 namespace HeuristicLab.Services.Optimization.Billing.DataAccess { 7 9 public class BillingContext : DbContext { 8 public DbSet<ContactInformation> ContactInformations { get; set; }9 10 public DbSet<Product> Products { get; set; } 10 11 public DbSet<User> Users { get; set; } … … 14 15 public DbSet<InvoiceLine> InvoiceLines { get; set; } 15 16 public DbSet<UsageRecord> UsageRecords { get; set; } 17 public DbSet<ContactInformation> ContactInformations { get; set; } 18 public DbSet<PaymentInformation> PaymentInformations { get; set; } 16 19 17 20 // enum PaymentMethod … … 19 22 20 23 public BillingContext() 21 : base("name=BillingContext") {24 : this("name=BillingContext") { 22 25 23 26 } … … 34 37 modelBuilder.Configurations.Add(new InvoiceLineConfiguration()); 35 38 modelBuilder.Configurations.Add(new UsageRecordConfiguration()); 39 modelBuilder.Configurations.Add(new UserConfiguration()); 36 40 37 41 modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); … … 77 81 } 78 82 83 private class UserConfiguration : EntityTypeConfiguration<User> { 84 internal UserConfiguration() { 85 this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId); 86 } 87 } 88 79 89 #endregion 80 90 81 91 #region DB Seed Methods 82 92 83 public class BillingContextInitiliazer : DropCreateDatabaseIfModelChanges<BillingContext> { 93 // - DropCreateDatabaseAlways<BillingContext> 94 // - DropCreateDatabaseIfModelChanges<BillingContext> 95 public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> { 84 96 protected override void Seed(BillingContext context) { 85 context.Products.Add(new Model.Product() {97 Product p1 = new Product() { 86 98 Name = "Optimization-as-a-Service - Experiment execution", 87 99 Description = "Create and run your experiments within HeuristicLab Hive", … … 89 101 Price = 10.0, 90 102 ProductType = "Optimization Service" 91 }); 103 }; 104 p1 = context.Products.Add(p1); 105 106 User u1 = new Model.User() { Name = "spimming" }; 107 User u2 = new Model.User() { 108 Name = "fschoeppl", 109 PaymentInformation = new List<PaymentInformation>() { 110 new PaymentInformation() { 111 PaymentMethod = PaymentMethod.Visa, 112 CardNumber = "123456789" } } 113 }; 114 115 u1 = context.Users.Add(u1); 116 u2 = context.Users.Add(u2); 117 118 ContactInformation ci1 = new ContactInformation() { 119 FirstName = "Max", 120 LastName = "Mustermann", 121 Email = "max.mustermann@fh-hagenberg.at", 122 OrganizationName = "University of Applied Sciences Upper Austria School of Informatics/Communications/Media", 123 Street = "Softwarepark 11", 124 PostalCode = "4232", 125 City = "Hagenberg" 126 }; 127 128 ci1 = context.ContactInformations.Add(ci1); 129 130 Order o1 = new Order() { 131 User = u2, 132 State = OrderState.Created, 133 BillingType = BillingType.Post, 134 BillingPeriod = BillingPeriod.Monthly, 135 }; 136 OrderLine ol1 = new OrderLine() { 137 Order = o1, 138 Product = p1, 139 ProductPrice = p1.Price, 140 Quantity = 1 141 }; 142 o1 = context.Orders.Add(o1); 143 ol1 = context.OrderLines.Add(ol1); 144 145 context.SaveChanges(); 146 147 o1.State = OrderState.Active; 148 o1.ActiveSince = DateTime.Now; 149 context.Entry(o1).State = System.Data.EntityState.Modified; 92 150 context.SaveChanges(); 93 151 } -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingDao.cs
r9577 r9602 111 111 112 112 #endregion 113 114 #region OrderLine Methods 115 116 public OrderLine FindOrderLineById(long id) { 117 return ExecuteWithContext<OrderLine>((context) => { 118 return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id); 119 }); 120 } 121 122 public IEnumerable<OrderLine> FindOrderLineBy(Expression<Func<OrderLine, bool>> predicate) { 123 using (var context = CreateContext()) { 124 var orderLines = context.OrderLines.Where(predicate).ToList(); 125 return orderLines; 126 } 127 } 128 129 public IEnumerable<OrderLine> FindAllOrderLines() { 130 using (var context = CreateContext()) { 131 var orderLines = context.OrderLines.ToList(); 132 return orderLines; 133 } 134 } 135 136 public void AddOrderLine(OrderLine entity) { 137 ExecuteWithContext((context) => { 138 context.OrderLines.Add(entity); 139 context.SaveChanges(); 140 }); 141 } 142 143 public void UpdateOrderLine(OrderLine entity) { 144 ExecuteWithContext((context) => { 145 context.OrderLines.Attach(entity); 146 context.Entry(entity).State = System.Data.EntityState.Modified; 147 context.SaveChanges(); 148 }); 149 } 150 151 void DeleteOrderLine(OrderLine entity) { 152 ExecuteWithContext((context) => { 153 context.OrderLines.Remove(entity); 154 }); 155 } 156 157 #endregion 158 159 #region User Methods 160 161 public User FindUserById(long id) { 162 return ExecuteWithContext<User>((context) => { 163 return context.Users.FirstOrDefault(user => user.UserId == id); 164 }); 165 } 166 167 public IEnumerable<User> FindUserBy(Expression<Func<User, bool>> predicate) { 168 using (var context = CreateContext()) { 169 var users = context.Users.Where(predicate).ToList(); 170 return users; 171 } 172 } 173 174 public IEnumerable<User> FindAllUsers() { 175 using (var context = CreateContext()) { 176 var users = context.Users.ToList(); 177 return users; 178 } 179 } 180 181 public void AddUser(User entity) { 182 ExecuteWithContext((context) => { 183 context.Users.Add(entity); 184 context.SaveChanges(); 185 }); 186 } 187 188 public void UpdateUser(User entity) { 189 ExecuteWithContext((context) => { 190 context.Users.Attach(entity); 191 context.Entry(entity).State = System.Data.EntityState.Modified; 192 context.SaveChanges(); 193 }); 194 } 195 196 void DeleteUser(User entity) { 197 ExecuteWithContext((context) => { 198 context.Users.Remove(entity); 199 }); 200 } 201 202 #endregion 203 204 #region UsageRecords Methods 205 206 public UsageRecord FindUsageRecordById(long id) { 207 return ExecuteWithContext<UsageRecord>((context) => { 208 return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id); 209 }); 210 } 211 212 public IEnumerable<UsageRecord> FindUsageRecordsBy(Expression<Func<UsageRecord, bool>> predicate) { 213 using (var context = CreateContext()) { 214 var records = context.UsageRecords.Where(predicate).ToList(); 215 return records; 216 } 217 } 218 219 public IEnumerable<UsageRecord> FindAllUsageRecords() { 220 using (var context = CreateContext()) { 221 var records = context.UsageRecords.ToList(); 222 return records; 223 } 224 } 225 226 public void AddUsageRecord(UsageRecord entity) { 227 ExecuteWithContext((context) => { 228 context.UsageRecords.Add(entity); 229 context.SaveChanges(); 230 }); 231 } 232 233 public void UpdateUsageRecord(UsageRecord entity) { 234 ExecuteWithContext((context) => { 235 context.UsageRecords.Attach(entity); 236 context.Entry(entity).State = System.Data.EntityState.Modified; 237 context.SaveChanges(); 238 }); 239 } 240 241 void DeleteUsageRecord(UsageRecord entity) { 242 ExecuteWithContext((context) => { 243 context.UsageRecords.Remove(entity); 244 }); 245 } 246 247 #endregion 248 249 #region Invoice Methods 250 251 public void AddInvoice(Invoice entity) { 252 ExecuteWithContext((context) => { 253 context.Invoices.Add(entity); 254 context.SaveChanges(); 255 }); 256 } 257 258 public IEnumerable<Invoice> FindInvoiceBy(Expression<Func<Invoice, bool>> predicate) { 259 using (var context = CreateContext()) { 260 var invoices = context.Invoices.Where(predicate).ToList(); 261 return invoices; 262 } 263 } 264 265 #endregion 113 266 } 114 267 }
Note: See TracChangeset
for help on using the changeset viewer.