public IActionResult BuyBook([FromBody] BuyBookModel model) { double.TryParse(model.DeliveryCost, out double cost); var serviceType = (DeliveryType)Enum.Parse(typeof(DeliveryType), model.DeliveryService); IDelivery delivery = _deliveryServiceGenerator.ExecuteCreation(serviceType, cost); var orderInfo = delivery.GetInfo(); return(Ok(orderInfo)); }
public ActionResult BuyBook(BuyBookModel BBModel) { User user = db.Users.FirstOrDefault(u => u.Name == BBModel.BuyUserName); Book book = db.Books.FirstOrDefault(u => u.BookName == BBModel.BuyBookName); //Добавляем обьект покупки if (user.Wallet >= book.BookPrice) { //Проверка айди покупки на уникальность var checkPurchaseCodes = db.Purchases.Select(p => p.PurchaseCode).ToList(); var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[8]; Random rnd = new Random(); String finalString = ""; bool PurchaseCodeIsUnique = false; while (!PurchaseCodeIsUnique) { for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[rnd.Next(0, chars.Length)]; finalString += stringChars[i]; } if (db.Purchases.FirstOrDefault(p => p.PurchaseCode == finalString) == null) { PurchaseCodeIsUnique = true; break; } finalString = ""; } db.Purchases.Add(new Purchase() { PurchaseCode = finalString, Book = book, User = user, Time = DateTime.Now }); //Создаем коллекцию из обьектов книг, и присваем эту коллекцию к свойству Books user.Wallet = user.Wallet - book.BookPrice; book.BoughtBooks++; db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View("NotEnoughMoney")); }
public string BuyBook([FromBody] BuyBookModel model) { if (model.DeliveryService == "Motorbike") { DateTime deliveryDate = DateTime.Now.AddDays(random.Next(0, 7)); return(DeliveryInfoGenerator.Generate(new MotorbikeDeliveryInfo { Driver = RandomString(random.Next(5, 15)), Mobile = random.Next(640000000, 649999999).ToString(), DeliveryCost = DeliveryCostCalculator.GetCost(DeliveryType.Motorbike, deliveryDate), DeliveryDate = deliveryDate })); } else if (model.DeliveryService == "Train") { DateTime arrivalDate = DateTime.Now.AddDays(random.Next(1, 7)); return(DeliveryInfoGenerator.Generate(new TrainDeliveryInfo { TrainNo = random.Next(100, 999).ToString(), ArrivalDate = arrivalDate, DeliveryCost = DeliveryCostCalculator.GetCost(DeliveryType.Train, arrivalDate), StationName = RandomString(random.Next(5, 15)) })); } else if (model.DeliveryService == "Aircraft") { DateTime arrivalDate = DateTime.Now.AddDays(random.Next(0, 3)); return(DeliveryInfoGenerator.Generate(new AircraftDeliveryInfo { FlightNo = random.Next(100, 999).ToString(), ArrivalDate = arrivalDate, DeliveryCost = DeliveryCostCalculator.GetCost(DeliveryType.Aircraft, arrivalDate), GateNumber = random.Next(1, 500).ToString() })); } else { throw new Exception("Wrong delivery service"); } }
public string DeliveryInfoGenerator(BuyBookModel submitData) { List <DriverModel> drivers = new List <DriverModel>(); drivers.Add(new DriverModel { DriverName = "David", PhoneNo = "021020102" }); drivers.Add(new DriverModel { DriverName = "Peter", PhoneNo = "021029902" }); drivers.Add(new DriverModel { DriverName = "Tom", PhoneNo = "021340102" }); drivers.Add(new DriverModel { DriverName = "John", PhoneNo = "021880102" }); string [] stations = { "Newmarket", "Manukau", "Papatoetoe", "Otahuhu" }; string [] gates = { "Auckland", "Christchurch", "Queenstown", "Tauranga" }; DateTime currentDate = DateTime.Now; Random rand = new Random(); string info = ""; int randomIndex = 1; switch (submitData.DeliveryService) { case "Motobike": randomIndex = rand.Next(drivers.Count); info = String.Join(Environment.NewLine, "Driver name: " + drivers[randomIndex].DriverName + ", ", "Phone no: " + drivers[randomIndex].PhoneNo + ", ", "Delevery date: " + currentDate.AddDays(1).ToString("dd/MM/yyyy" + ", "), "Delivery cost: $" + submitData.DeliveryCost); return(info); case "Train": string trainNo = "TRN" + rand.Next(1000).ToString(); randomIndex = rand.Next(stations.Length); info = String.Join(Environment.NewLine, "Train no: " + trainNo + ", ", "Station of arrival: " + stations[randomIndex] + ", ", "Delevery date: " + currentDate.AddDays(2).ToString("dd/MM/yyyy") + ", ", "Delivery cost: $" + submitData.DeliveryCost); return(info); case "Aircraft": string flightNo = "AIR" + rand.Next(1000).ToString(); randomIndex = rand.Next(gates.Length); info = String.Join(Environment.NewLine, "Flight no: " + flightNo + ", ", "Gate of arrival: " + gates[randomIndex] + ", ", "Delevery date: " + currentDate.AddDays(3).ToString("dd/MM/yyyy") + ", ", "Delivery cost: $" + submitData.DeliveryCost); return(info); default: break; } return(info); }
public string BuyBook([FromBody] BuyBookModel submitData) { var deleveryInfo = DeliveryInfoGenerator(submitData); return(deleveryInfo); }
public JsonResult BuyBook(BuyBookModel model) { var deliveryInfo = _deliveryInfoGenerateService.GenerateInfo(model.DeliveryService, model.DeliveryCost); return(new JsonResult(deliveryInfo)); }