public ActionResult AddService(BusinessServicesViewModel model) { var userId = HttpContext.User.Identity.GetUserId(); BusinessServicesViewModel modelToReturn; if (!ModelState.IsValid) { modelToReturn = GetBusinessServicesViewModel(userId); modelToReturn.NewService = model.NewService; return(View("Services", modelToReturn)); } var user = _contex.Users.Include(u => u.Business.Services).Single(u => u.Id == userId); user.Business.Services.Add(new Service { Name = model.NewService.Name, Duration = model.NewService.Duration, Active = true }); if (_contex.SaveChanges() > 0) { modelToReturn = GetBusinessServicesViewModel(userId); ViewBag.SuccessMessage = "Pomyślnie dodano nową usługę."; return(View("Services", modelToReturn)); } else { modelToReturn = GetBusinessServicesViewModel(userId); ViewBag.SuccessMessage = "Coś poszło nie tak, usługa nie została dodana."; return(View("Services", modelToReturn)); } }
private BusinessServicesViewModel GetBusinessServicesViewModel(string userId) { var services = _contex.Users.Include(u => u.Business.Services).SingleOrDefault(u => u.Id == userId).Business.Services.ToList(); var model = new BusinessServicesViewModel(); model.Services = new List <ServiceViewModel>(); foreach (var service in services) { var singleService = new ServiceViewModel { Id = service.Id, Duration = service.Duration, Name = service.Name }; if (service.Active == true) { model.Services.Add(singleService); } } return(model); }