Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9602 for branches/OaaS


Ignore:
Timestamp:
06/10/13 16:32:19 (11 years ago)
Author:
spimming
Message:

#1888:

  • Workaround for enums in model classes
  • Implemented dao methods for model
  • Added test data in db seed method
  • changed EF configuration
  • Implemented some BL methods
Location:
branches/OaaS
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing.Test/HeuristicLab.Services.Optimization.Billing.Test.csproj

    r9579 r9602  
    6262    <None Include="packages.config" />
    6363  </ItemGroup>
     64  <ItemGroup>
     65    <Content Include="DDLScript.sql" />
     66  </ItemGroup>
    6467  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    6568  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing.Test/Program.cs

    r9579 r9602  
    11using System;
     2using System.Data.Entity.Infrastructure;
     3using System.IO;
    24using HeuristicLab.Services.Optimization.Billing.DataAccess;
    35using HeuristicLab.Services.Optimization.Billing.Model;
     
    68  class Program {
    79    static void Main(string[] args) {
     10      string ddlScript;
    811      using (var context = new BillingContext()) {
    912        System.Data.Entity.Database.SetInitializer(new HeuristicLab.Services.Optimization.Billing.DataAccess.BillingContext.BillingContextInitiliazer());
     13        ddlScript = (((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript());
     14      }
     15
     16      using (StreamWriter writer = new StreamWriter(@"..\..\DDLScript.sql")) {
     17        writer.Write(ddlScript);
    1018      }
    1119
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/BillingService.cs

    r9586 r9602  
    4545    }
    4646
     47    public IList<Order> GetOrders(string userName) {
     48      //TODO: Authentication
     49      //TODO: Enable Transaction
     50      User user = GetUser(userName);
     51      return BillingDao.FindOrderBy(order => order.User == user).ToList();
     52    }
     53
    4754    #endregion
    4855
    49 
    50     public IList<Order> GetOrders(string userName) {
    51       throw new System.NotImplementedException();
    52     }
    53 
     56    #region User Methods
    5457
    5558    public User GetUser(string userName) {
    56       throw new System.NotImplementedException();
     59      //TODO: Authentication
     60      //TODO: Enable Transaction
     61      return BillingDao.FindUserBy(u => u.Name == userName).First();
    5762    }
    5863
    5964    public void SaveUser(User user) {
    60       throw new System.NotImplementedException();
     65      //TODO: Authentication
     66      //TODO: Enable Transaction
     67      BillingDao.AddUser(user);
    6168    }
    6269
     70    #endregion
     71
     72    #region UsageRecords Methods
    6373
    6474    public IList<UsageRecord> GetUsageRecords(string userName) {
    65       throw new System.NotImplementedException();
     75      //TODO: Authentication
     76      //TODO: Enable Transaction
     77      User user = GetUser(userName);
     78      return BillingDao.FindUsageRecordsBy(ur => ur.User == user).ToList();
    6679    }
    6780
     81    #endregion
     82
     83    #region Invoice Methods
    6884
    6985    public IList<Invoice> GetInvoices(string userName) {
    70       throw new System.NotImplementedException();
     86      //TODO: Authentication
     87      //TODO: Enable Transaction
     88      User user = GetUser(userName);
     89      return BillingDao.FindInvoiceBy(inv => inv.User == user).ToList();
    7190    }
     91
     92    #endregion
    7293  }
    7394}
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/MockupBillingService.cs

    r9586 r9602  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Linq;
    34using HeuristicLab.Services.Optimization.Billing.Interfaces;
    4 using System;
    55
    66namespace HeuristicLab.Services.Optimization.Billing.Business {
     
    6969        orders.Add(order);
    7070        allOrders.Add(order);
    71       }
    72       else {
     71      } else {
    7372        oldOrder.Invoices = order.Invoices;
    7473        oldOrder.OrderLines = order.OrderLines;
    75         oldOrder.OrderStateId = order.OrderStateId;
    7674        oldOrder.State = order.State;
    7775        oldOrder.ActiveSince = order.ActiveSince;
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs

    r9579 r9602  
    1 using System.Data.Entity;
     1using System;
     2using System.Collections.Generic;
     3using System.Data.Entity;
    24using System.Data.Entity.ModelConfiguration;
    35using System.Data.Entity.ModelConfiguration.Conventions;
     
    68namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
    79  public class BillingContext : DbContext {
    8     public DbSet<ContactInformation> ContactInformations { get; set; }
    910    public DbSet<Product> Products { get; set; }
    1011    public DbSet<User> Users { get; set; }
     
    1415    public DbSet<InvoiceLine> InvoiceLines { get; set; }
    1516    public DbSet<UsageRecord> UsageRecords { get; set; }
     17    public DbSet<ContactInformation> ContactInformations { get; set; }
     18    public DbSet<PaymentInformation> PaymentInformations { get; set; }
    1619
    1720    // enum PaymentMethod
     
    1922
    2023    public BillingContext()
    21       : base("name=BillingContext") {
     24      : this("name=BillingContext") {
    2225
    2326    }
     
    3437      modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
    3538      modelBuilder.Configurations.Add(new UsageRecordConfiguration());
     39      modelBuilder.Configurations.Add(new UserConfiguration());
    3640
    3741      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
     
    7781    }
    7882
     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
    7989    #endregion
    8090
    8191    #region DB Seed Methods
    8292
    83     public class BillingContextInitiliazer : DropCreateDatabaseIfModelChanges<BillingContext> {
     93    // - DropCreateDatabaseAlways<BillingContext>
     94    // - DropCreateDatabaseIfModelChanges<BillingContext>
     95    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
    8496      protected override void Seed(BillingContext context) {
    85         context.Products.Add(new Model.Product() {
     97        Product p1 = new Product() {
    8698          Name = "Optimization-as-a-Service - Experiment execution",
    8799          Description = "Create and run your experiments within HeuristicLab Hive",
     
    89101          Price = 10.0,
    90102          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;
    92150        context.SaveChanges();
    93151      }
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingDao.cs

    r9577 r9602  
    111111
    112112    #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
    113266  }
    114267}
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/HeuristicLab.Services.Optimization.Billing.csproj

    r9576 r9602  
    3737    <Reference Include="System" />
    3838    <Reference Include="System.ComponentModel.DataAnnotations" />
     39    <Reference Include="System.Configuration" />
    3940    <Reference Include="System.Core" />
    4041    <Reference Include="System.Data.Entity" />
  • branches/OaaS/HeuristicLab.Services.Optimization.Billing/Model/Model.cs

    r9586 r9602  
    1 using System;
     1
     2using System;
    23using System.Collections.Generic;
     4namespace HeuristicLab.Services.Optimization.Billing.Model {
    35
    4 namespace HeuristicLab.Services.Optimization.Billing.Model {
     6  /*
     7   * Note: enum support
     8   * Although we use EF5 enums are not supported as we target .NET 4.0.
     9   * Therefor we use the following workaround:
     10   * Just create an int property on your class to represent the int value of the enum.
     11   * That's the property that EF should map, then have a "mini wrapper" property to allow you to use the enum
     12   * For example see class PaymentInformation
     13   * See also: http://stackoverflow.com/questions/6344032/enums-with-ef-code-first-standard-method-to-seeding-db-and-then-using
     14   */
     15
    516  public class ContactInformation {
    617    public long ContactInformationId { get; set; }
     
    1728  public class PaymentInformation {
    1829    public long PaymentInformationId { get; set; }
     30    public long UserId { get; set; }
    1931    public string CardNumber { get; set; }
    20     public PaymentMethod PaymentMethod { get; set; }
     32    public int PaymentMethodValue { get; set; }
    2133    public string AdditionalInformation { get; set; }
     34
     35    public virtual User User { get; set; }
     36
     37    public PaymentMethod PaymentMethod {
     38      get { return (PaymentMethod)PaymentMethodValue; }
     39      set { PaymentMethodValue = (int)value; }
     40    }
    2241  }
    2342
     
    5271    public long UserId { get; set; }
    5372    public string Name { get; set; }
     73
    5474    public virtual IList<PaymentInformation> PaymentInformation { get; set; }
     75
    5576  }
    5677
     
    6687    public long OrderId { get; set; }
    6788    public long UserId { get; set; }
    68     public BillingType BillingType { get; set; }
    69     public long OrderStateId { get; set; }
    70     public BillingPeriod BillingPeriod { get; set; }
     89    public int StateValue { get; set; }
     90    public int BillingTypeValue { get; set; }
     91    public int BillingPeriodValue { get; set; }
    7192    public DateTime? ActiveSince { get; set; }
    7293    public DateTime? ActiveUntil { get; set; }
    7394
    74 
    7595    public virtual User User { get; set; }
    76     public virtual OrderState State { get; set; }
    7796    public virtual IList<Invoice> Invoices { get; set; }
    7897    public virtual IList<OrderLine> OrderLines { get; set; }
     98
     99    public OrderState State {
     100      get { return (OrderState)StateValue; }
     101      set { StateValue = (int)value; }
     102    }
     103    public BillingType BillingType {
     104      get { return (BillingType)BillingTypeValue; }
     105      set { BillingTypeValue = (int)value; }
     106    }
     107    public BillingPeriod BillingPeriod {
     108      get { return (BillingPeriod)BillingPeriodValue; }
     109      set { BillingPeriodValue = (int)value; }
     110    }
    79111  }
    80112
     
    99131    public long OrderId { get; set; }
    100132    public DateTime Due { get; set; }
    101     public InvoiceStatus Status { get; set; }
     133    public int StatusValue { get; set; }
    102134    public string InvoiceDocument { get; set; }
    103135
     
    105137    public virtual Order Order { get; set; }
    106138    public virtual IList<InvoiceLine> InvoiceLines { get; set; }
     139
     140    public InvoiceStatus Status {
     141      get { return (InvoiceStatus)StatusValue; }
     142      set { StatusValue = (int)value; }
     143    }
    107144  }
    108145
Note: See TracChangeset for help on using the changeset viewer.