public ActionResult New() { var form = "GarageFormUser"; var viewModel = new ServicesInGarageViewModel() { Garage = new Garage(), Services = _context.Services.ToList() }; if (User.IsInRole(RoleNames.Administrator) || User.IsInRole(RoleNames.GarageOwner)) { form = "GarageForm"; return(View(form, viewModel)); } else if (User.IsInRole(RoleNames.Customer)) { form = "AlreadyInRole"; return(View(form)); } else { return(View(form, viewModel)); } }
public ActionResult ListOfServices(int id) { var viewModel = new ServicesInGarageViewModel() { Garage = _context.Garages.SingleOrDefault(c => c.ID == id), Services = (from data in _context.Garages join data2 in _context.GarageServices on data.ID equals data2.GarageID join data3 in _context.Services on data2.ServiceID equals data3.ID where data.ID == id select data3).ToList() }; return(View(viewModel)); }
public ActionResult Edit(int?Id) { var currentUserID = User.Identity.GetUserId(); var garageInDB = _context.Garages.SingleOrDefault(c => c.ApplicationUserId == currentUserID); var form = "GarageForm"; if (garageInDB == null) { return(HttpNotFound()); } var viewModel = new ServicesInGarageViewModel() { Garage = garageInDB, Services = _context.Services.ToList() }; return(View(form, viewModel)); }
public ActionResult Save(Garage garage) { if (!ModelState.IsValid) { var form = "GarageFormUser"; if (User.IsInRole(RoleNames.Administrator) || User.IsInRole(RoleNames.GarageOwner) || User.IsInRole(RoleNames.Customer)) { form = "GarageForm"; var viewModel = new ServicesInGarageViewModel() { Garage = garage, Services = _context.Services.ToList() }; return(View(form, viewModel)); } else { return(View(form)); } } var currentUserID = User.Identity.GetUserId(); var userIsCustomer = _context.Customers.SingleOrDefault(c => c.ApplicationUserId == currentUserID); var userIsGarage = _context.Garages.SingleOrDefault(c => c.ApplicationUserId == currentUserID); var objectIsCustomer = _context.Customers.SingleOrDefault(c => c.ApplicationUserId == garage.ApplicationUserId); var objectIsGarage = _context.Garages.SingleOrDefault(c => c.ApplicationUserId == garage.ApplicationUserId); var userExists = _context.Users.SingleOrDefault(u => u.Id == garage.ApplicationUserId); //******** Come here if form is valid if (garage.ID == 0) { //_context.Garages.Add(garage); if (User.IsInRole(RoleNames.GarageOwner)) { return(View("AlreadyInRoleGeneral")); } else { //checking if the user has a role, a customer cannot be a garage in the same time if ((objectIsGarage == null || objectIsCustomer == null) && userExists != null) { security.AddUserToRole(garage.ApplicationUserId, RoleNames.GarageOwner); _context.Garages.Add(garage); } else if (userIsCustomer == null || userIsGarage == null) //&& //_context.Users.SingleOrDefault(u => u.Id == User.Identity.GetUserId()) != null { security.AddUserToRole(currentUserID, RoleNames.GarageOwner); garage.ApplicationUserId = currentUserID; _context.Garages.Add(garage); } //if there is no userID on the file matching the one returned from the form else if (_context.Users.SingleOrDefault(u => u.Id == garage.ApplicationUserId) == null) { return(View("NeedToRegisterBefore")); } //there is a user with this UserID in the customers or garage role else { return(View("AlreadyInRoleGeneral")); } } } else { var garageInDB = _context.Garages.Single(g => g.ID == garage.ID); var UserId = _context.Users.SingleOrDefault(c => c.Id == garage.ApplicationUserId); //Manually update the fields I want. garageInDB.Name = garage.Name; garageInDB.Address = garage.Address; garageInDB.PhoneNumber = garage.PhoneNumber; garageInDB.ApplicationUserId = garage.ApplicationUserId; security.AddUserToRole(garage.ApplicationUserId, RoleNames.GarageOwner); } _context.SaveChanges(); if (User.IsInRole(RoleNames.Administrator) || User.IsInRole(RoleNames.GarageOwner)) { return(RedirectToAction("Index", "Garages")); } else { return(RedirectToAction("Index", "Home")); } }