Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Controllers/OrderController.cs @ 11813

Last change on this file since 11813 was 9654, checked in by spimming, 12 years ago

#1888:

  • Added model and view for contact information
  • Insert contact information on SaveOrder page
  • Set correct entity state
File size: 6.5 KB
RevLine 
[9576]1using System.Collections.Generic;
[9556]2using System.Linq;
3using System.Web.Mvc;
[9576]4using System.Web.Security;
5using HeuristicLab.Services.Optimization.Billing.Business;
6using HeuristicLab.Services.Optimization.Billing.Interfaces;
7using HeuristicLab.Services.Optimization.Billing.Model;
[9654]8using HeuristicLab.Services.Optimization.Web.Helpers;
[9556]9using HeuristicLab.Services.Optimization.Web.Models;
10
[9576]11namespace HeuristicLab.Services.Optimization.Web.Controllers {
12  [Authorize(Roles = "Web User")]
[9586]13  [BillingFilterActionAttribute]
[9582]14  public class OrderController : BaseController {
[9576]15    private IOptimizationBilling billing = BillingServiceProvider.Instance;
[9556]16
[9576]17    //
18    // GET: /Order/
[9556]19
[9654]20    public ActionResult Index() {
21      if (RedirectToLoginIfNecessary("Order", false))
22        return View();
23      return CreateOrder(new OrderModel()); //View();
24    }
[9556]25
[9654]26    public ActionResult Overview() {
27      if (RedirectToLoginIfNecessary("Order", false))
28        return View();
29      MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
30      var orders = billing.GetOrders(currentUser.UserName);
31      return View(new OverviewModel() { Orders = orders });
32    }
[9556]33
34
[9654]35    [HttpPost]
36    public ActionResult CreateOrder(OrderModel model) {
37      if (RedirectToLoginIfNecessary("Order", false))
38        return View();
39      Session["order"] = model;
40      var currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
41      if (billing.GetUser(currentUser.UserName) == null) {
42        billing.SaveUser(new User() { Name = currentUser.UserName, PaymentInformation = new List<PaymentInformation>() });
43      }
44      model.User = billing.GetUser(currentUser.UserName);
45      return AddProduct();
46    }
[9582]47
[9576]48    public ActionResult AddProduct() {
49      return View("AddProduct", new ProductsModel() { Products = billing.GetProducts() });
50    }
[9556]51
[9576]52    [HttpPost]
53    public ActionResult AddProduct(ProductsModel product) {
54      var order = Session["order"] as OrderModel;
55      var prod = (from b in billing.GetProducts()
56                  where b.ProductId == product.ProductId
57                  select b).FirstOrDefault();
58      order.ProductQuantities[prod.ProductId] = product.Quantity;
59      return AddProduct();
60    }
[9556]61
[9576]62    public ActionResult SaveOrder() {
63      var orderModel = Session["order"] as OrderModel;
64      foreach (var key in orderModel.ProductQuantities.Keys) {
65        var product = (from p in billing.GetProducts() where p.ProductId == key select p).FirstOrDefault();
66        orderModel.Products[key] = product;
67      }
68      return View(orderModel);
69    }
[9556]70
[9576]71    [HttpPost]
72    [ActionName("SaveOrder")]
[9582]73    public ActionResult SaveOrderPost(OrderModel orderModel) {
[9654]74      var user = billing.GetUser(UserName);
[9582]75      var orderProductModel = Session["order"] as OrderModel;
[9576]76      //TODO: Save order via back-end
77      var order = new Order() {
78        State = OrderState.Created,
[9654]79        User = user,
[9576]80        BillingType = orderModel.BillingType,
81        BillingPeriod = orderModel.BillingPeriod
82      };
[9556]83
[9576]84      var orderLines = new List<OrderLine>();
85      var products = billing.GetProducts();
[9582]86      foreach (var key in orderProductModel.ProductQuantities.Keys) {
[9576]87        var product = (from p in products where p.ProductId == key select p).FirstOrDefault();
88        orderLines.Add(new OrderLine() {
89          Order = order,
90          Product = product,
91          ProductPrice = product.Price,
[9582]92          Quantity = orderProductModel.ProductQuantities[key]
[9576]93        });
94      }
95      order.OrderLines = orderLines;
96      billing.SaveOrder(order);
97      return RedirectToAction("OrderSaved");
[9556]98    }
[9576]99
[9582]100    public ActionResult Payment() {
101      var paymentModel = new PaymentModel();
102      return View(paymentModel);
103    }
104
105    [HttpPost]
106    public ActionResult SavePayment(PaymentModel model) {
107      var user = billing.GetUser(UserName);
108      var oldInfo = (from pi in user.PaymentInformation where pi.CardNumber == model.CardNumber select pi).FirstOrDefault();
109      if (oldInfo != null) {
110        oldInfo.AdditionalInformation = model.AdditionalInformation;
111        oldInfo.PaymentMethod = model.Method;
[9654]112        oldInfo.EntityState = State.Modified;
113      } else {
[9582]114        user.PaymentInformation.Add(new PaymentInformation() { CardNumber = model.CardNumber, AdditionalInformation = model.AdditionalInformation, PaymentMethod = model.Method });
[9654]115        user.EntityState = State.Modified;
[9582]116      }
117
[9654]118      billing.SaveUser(user);
[9582]119      return RedirectToAction("SaveOrder");
120    }
121
[9576]122    public ActionResult OrderSaved() {
123      return View();
124    }
[9654]125
126    public ActionResult ContactInformation() {
127      var user = billing.GetUser(UserName);
128      var userCi = user.ContactInformation;
129      ContactInformationModel contactInformationModel = null;
130
131      if (userCi == null) {
132        contactInformationModel = new ContactInformationModel();
133      } else {
134        contactInformationModel = new ContactInformationModel() {
135          ContactInformationId = userCi.ContactInformationId,
136          FirstName = userCi.FirstName,
137          LastName = userCi.LastName,
138          Email = userCi.Email,
139          OrganizationName = userCi.OrganizationName,
140          Street = userCi.Street,
141          PostalCode = userCi.PostalCode,
142          City = userCi.City,
143          State = userCi.State
144        };
145      }
146      return View(contactInformationModel);
147    }
148
149    [HttpPost]
150    public ActionResult SaveContactInformation(ContactInformationModel model) {
151      var user = billing.GetUser(UserName);
152      var userCi = user.ContactInformation;
153      if (userCi != null) {
154        userCi.City = model.City;
155        userCi.Email = model.Email;
156        userCi.FirstName = model.FirstName;
157        userCi.LastName = model.LastName;
158        userCi.OrganizationName = model.OrganizationName;
159        userCi.PostalCode = model.PostalCode;
160        userCi.State = model.State;
161        userCi.Street = model.Street;
162        userCi.EntityState = State.Modified;
163      } else {
164        user.ContactInformation = new ContactInformation() {
165          City = model.City,
166          Email = model.Email,
167          FirstName = model.FirstName,
168          LastName = model.LastName,
169          OrganizationName = model.OrganizationName,
170          PostalCode = model.PostalCode,
171          State = model.State,
172          Street = model.Street
173        };
174        user.EntityState = State.Modified;
175      }
176
177      billing.SaveUser(user);
178
179      return RedirectToAction("SaveOrder");
180    }
[9576]181  }
[9556]182}
Note: See TracBrowser for help on using the repository browser.