public ActionResult Update(AirlineViewModel model) { var dbContext = new ApplicationDbContext(); // Check GUEST var user = dbContext.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); if (user != null && user.Email == AccountController.GuestEmail) { TempData["GuestMessage"] = AccountController.GuestMessage; return(RedirectToAction("Register", "Account")); } var airline = dbContext.AirlineDbModels.FirstOrDefault(a => a.Id == model.Id && a.UserId == user.Id); if (airline != null) { var certsAirline = dbContext.AirlineCertificatesDbModels.Include("Certificate").Where(c => c.Airline.Id == airline.Id).ToList(); if (!model.RequireCertificates) { dbContext.AirlineCertificatesDbModels.RemoveRange(certsAirline); } else { foreach (var cert in dbContext.CertificateDbModels) { if (!certsAirline.Any(x => x.Certificate.Id == cert.Id)) { var airlineCertificates = new AirlineCertificatesDbModel() { Certificate = cert, Airline = airline }; dbContext.AirlineCertificatesDbModels.Add(airlineCertificates); } } } airline.Name = model.Name; airline.Description = model.Description; airline.Country = model.Country; airline.Score = model.Score; if (model.FilesInput != null && model.FilesInput.Any()) { var file = model.FilesInput.FirstOrDefault(); if (file != null) { var filePath = Upload(file); airline.Logo = filePath; } } dbContext.SaveChanges(); TempData["Message"] = "You update the airline. Now you can invite pilots to work with you."; Session["HeaderStatistics"] = null; } else { TempData["Message"] = "You must be the owner of the airline to edit."; } return(RedirectToAction("Index")); }
public ActionResult Add(AirlineViewModel model) { var dbContext = new ApplicationDbContext(); // Check GUEST var user = dbContext.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); if (user != null && user.Email == AccountController.GuestEmail) { TempData["GuestMessage"] = AccountController.GuestMessage; return(RedirectToAction("Register", "Account")); } var statistics = dbContext.StatisticsDbModels.FirstOrDefault(s => s.User.Id == user.Id); if (statistics != null && statistics.BankBalance >= airlinePrice) { var dbModel = new AirlineDbModel() { Name = model.Name, Description = model.Description, Country = model.Country, Salary = 20, Score = model.Score, UserId = user.Id, DebtMaturityDate = DateTime.Now }; if (model.FilesInput != null && model.FilesInput.Any()) { var file = model.FilesInput.FirstOrDefault(); string filePath = @"/Content/img/logo/LogoDefault.png";//Upload(file); dbModel.Logo = filePath; } dbContext.AirlineDbModels.Add(dbModel); statistics.BankBalance = statistics.BankBalance - airlinePrice; statistics.Airline = dbModel; if (model.RequireCertificates) { foreach (var cert in dbContext.CertificateDbModels) { var airlineCertificates = new AirlineCertificatesDbModel() { Certificate = cert, Airline = dbModel }; dbContext.AirlineCertificatesDbModels.Add(airlineCertificates); } } dbContext.SaveChanges(); TempData["Message"] = "Congratulation you created a new airline. Now you can invite pilots to work with you."; Session["HeaderStatistics"] = null; } else { TempData["Message"] = "You don't have enough bank balance to buy new airline."; } return(RedirectToAction("Index")); }