public async Task <CakeOrderViewModel> CalculateTotalPrice(CakeOrderViewModel model) { if (!string.IsNullOrEmpty(model.ShapeCode)) { var initialData = await this.GetInitialData(); List <string> toppings = new List <string>(); if (model.Toppings != null) { toppings = model.Toppings.Split(',').ToList(); } ICake cake = initialData.CakeShapes.Where(x => x.Code.Trim() == model.ShapeCode.Trim()).FirstOrDefault(); cake.Toppings = initialData.Toppings.Where(w => toppings.Any(code => code == w.Code)).ToList(); cake.Message = model.Message; cake.Size = model.Size; model.TotalPrice = cake.CalculatePrice(); } else { model.TotalPrice = 0; } return(model); }
public async Task <bool> CreateCakeOrder(CakeOrderViewModel model, CancellationToken ct = default(CancellationToken)) { Customer cus = _customerRepository.GetByIdentityId(model.IdentityId); var initialData = await this.GetInitialData(); //calculate total price again List <string> toppings = model.Toppings.Split(',').ToList(); ICake cake = initialData.CakeShapes.Where(x => x.Code.Trim() == model.ShapeCode.Trim()).FirstOrDefault(); cake.Toppings = initialData.Toppings.Where(w => toppings.Any(code => code.Trim() == w.Code.Trim())).ToList(); cake.Message = model.Message; cake.Size = model.Size; model.TotalPrice = cake.CalculatePrice(); CakeOrder order = new CakeOrder(); order.CustomerId = cus.Id; order.Message = model.Message; order.ShapeCode = model.ShapeCode; order.Size = model.Size; order.TotalPrice = model.TotalPrice; order = await _cakeOrderRepository.AddAsync(order); foreach (var topping in cake.Toppings) { Topping toppingEntity = new Topping(); toppingEntity.CakeOrderId = order.Id; toppingEntity.Code = topping.Code; await _toppingRepository.AddAsync(toppingEntity); } return(true); }