public ActionResult Create(UserViewModel viewModel) { if (ModelState.IsValid) { var context = new Context(); var user = new User { Name = viewModel.Name, Username = viewModel.Username.ToUpper(), Password = viewModel.Password }; context.Users.Add(user); var exists = (from p in context.Users where p.Username.ToUpper() == viewModel.Username select p).Any(); if (exists) { throw new Exception($"There is already a registered user with the username {user.Username}."); } context.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel); }
public ActionResult Create(RatingViewModel viewModel) { if (ModelState.IsValid) { var userId = int.Parse(HttpContext.User.Identity.Name); var rating = new CarrierRating { CarrierId = viewModel.CarrierId, UserId = userId, Rating = viewModel.Rating.Value }; var context = new Context(); rating.Create(); context.CarrierRating.Add(rating); context.SaveChanges(); return RedirectToAction("Index", "Carriers"); } return View(viewModel); }
public ActionResult Edit(UserViewModel viewModel) { if (ModelState.IsValid) { var context = new Context(); var user = context.Users.FirstOrDefault(x => x.Id == viewModel.Id); if (user == null) { throw new Exception($"User with id {viewModel.Id} not found."); } user.Username = viewModel.Username; user.Name = viewModel.Name; if (!string.IsNullOrWhiteSpace(viewModel.Password)) { user.Password = viewModel.Password; } try { var exists = (from p in context.Users where p.Username.ToUpper() == user.Username && p.Id != user.Id select p).Any(); if (exists) { throw new Exception($"There is already a registered user with the username {user.Username}."); } context.SaveChanges(); return RedirectToAction("Index"); } catch (Exception exp) { ModelState.AddModelError("", exp.Message); } } return View(viewModel); }
public ActionResult Delete(int id) { var context = new Context(); var user = (from p in context.Users where p.Id == id select p).FirstOrDefault(); if (user == null) { throw new Exception($"User with id {id} not found."); } if (user.Id == 1) { throw new Exception("You can not delete the user id 1."); } context.Users.Remove(user); context.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult Create(CarrierViewModel viewModel) { if (ModelState.IsValid) { var carrier = new Carrier { Name = viewModel.Name, Adress = viewModel.Adress, City = viewModel.City, Country = viewModel.Country, Identification = viewModel.Identification, PhoneNumber = viewModel.PhoneNumber, State = viewModel.State, Url = viewModel.Url }; var context = new Context(); carrier.Create(); context.Carriers.Add(carrier); context.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel); }
public ActionResult Delete(int id) { var context = new Context(); var carrier = context.Carriers.FirstOrDefault(x => x.Id == id); if (carrier == null) { return RedirectToAction("Index"); } context.Carriers.Remove(carrier); context.SaveChanges(); return RedirectToAction("Index"); }
public ActionResult Edit(CarrierViewModel viewModel) { if (ModelState.IsValid) { var context = new Context(); var carrier = context.Carriers.First(x => x.Id == viewModel.Id); carrier.Name = viewModel.Name; carrier.Adress = viewModel.Adress; carrier.City = viewModel.City; carrier.Country = viewModel.Country; carrier.Identification = viewModel.Identification; carrier.PhoneNumber = viewModel.PhoneNumber; carrier.State = viewModel.State; carrier.Url = viewModel.Url; carrier.Update(); context.SaveChanges(); return RedirectToAction("Index"); } return View(viewModel); }
public ActionResult Edit(RatingViewModel viewModel) { if (ModelState.IsValid) { var userId = int.Parse(HttpContext.User.Identity.Name); var context = new Context(); var rating = context.CarrierRating .FirstOrDefault(x => x.CarrierId == viewModel.CarrierId && x.UserId == userId); if (rating == null) { return RedirectToAction("Index", "Carriers"); } rating.Rating = viewModel.Rating.Value; rating.Update(); context.SaveChanges(); return RedirectToAction("Index", "Carriers"); } return View(viewModel); }
public ActionResult Delete(int id) { var userId = int.Parse(HttpContext.User.Identity.Name); var context = new Context(); var rating = context.CarrierRating.FirstOrDefault(x => x.CarrierId == id && x.UserId == userId); if (rating == null) { return RedirectToAction("Index", "Carriers"); } context.CarrierRating.Remove(rating); context.SaveChanges(); return RedirectToAction("Index", "Carriers"); }