Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9558 was 9558, checked in by fschoepp, 11 years ago

#1888:

  • Added Usage statistic classes; added authorization attributes to web controllers.
File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HeuristicLab.Services.Optimization.Web.Models;
7using HeuristicLab.Services.Optimization.Billing.Model;
8using HeuristicLab.Services.Optimization.Billing.Interfaces;
9using HeuristicLab.Services.Optimization.Billing;
10using System.Web.Security;
11
12namespace HeuristicLab.Services.Optimization.Web.Controllers
13{
14    [Authorize(Roles = "Web User")]
15    public class OrderController : Controller
16    {
17        private IOptimizationBilling billing = new MockupOptimizationBilling();
18
19        //
20        // GET: /Order/
21
22        public ActionResult Index()
23        {
24          return CreateOrder(new OrderModel()); //View();
25        }
26
27
28        [HttpPost]
29        public ActionResult CreateOrder(OrderModel model) {
30          Session["order"] = model;
31          return AddProduct();
32        }
33
34        public ActionResult AddProduct() {
35          return View("AddProduct", new ProductsModel() { Products = billing.GetProducts() });
36        }
37
38        [HttpPost]
39        public ActionResult AddProduct(ProductsModel product) {
40          var order = Session["order"] as OrderModel;
41          var prod = (from b in billing.GetProducts()
42                      where b.Id == product.ProductId
43                      select b).FirstOrDefault();
44          order.ProductQuantities[prod.Id] = product.Quantity;
45          return AddProduct();
46        }
47
48        public ActionResult SaveOrder() {
49          var orderModel = Session["order"] as OrderModel;
50          foreach (var key in orderModel.ProductQuantities.Keys) {
51            var product = (from p in billing.GetProducts() where p.Id == key select p).FirstOrDefault();
52            orderModel.Products[key] = product;           
53          }
54          return View(orderModel);
55        }
56
57        [HttpPost]
58        [ActionName("SaveOrder")]
59        public ActionResult SaveOrderPost() {
60          var orderModel = Session["order"] as OrderModel;
61          //TODO: Save order via back-end
62          var order = new Order() {
63            State = OrderState.Created,
64            User = new User() { Name = Membership.GetUser().UserName },
65            BillingType = orderModel.BillingType,
66            BillingPeriod = orderModel.BillingPeriod
67          };
68         
69          var orderLines = new List<OrderLine>();
70          var products = billing.GetProducts();
71          foreach (var key in orderModel.ProductQuantities.Keys) {
72            var product = (from p in products where p.Id == key select p).FirstOrDefault();
73            orderLines.Add(new OrderLine() {
74               Order = order,
75               Product = product,
76               ProductPrice = product.Price,
77               Quantity = orderModel.ProductQuantities[key]
78            });
79          }
80          order.OrderLines = orderLines;
81          billing.SaveOrder(order);
82          return RedirectToAction("OrderSaved");
83        }
84
85        public ActionResult OrderSaved() {
86          return View();
87        }
88    }
89}
Note: See TracBrowser for help on using the repository browser.