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 Index(string search, int? page) { var context = new Context(); var carriers = from p in context.Carriers select p; if (!string.IsNullOrWhiteSpace(search)) { carriers = from p in carriers where p.Name.ToLower().Contains(search.ToLower()) || p.Identification.ToLower().Contains(search.ToLower()) select p; } page = page.GetValueOrDefault(1); var pageIndex = page.Value - 1; var itemsPage = 25; carriers = carriers.OrderBy(x => x.Name); var viewModel = new CarriersListViewModel(); viewModel.Page = page.Value; viewModel.Search = search; viewModel.Carriers = (carriers.ToCarrierViewModel().Skip(pageIndex * itemsPage).Take(itemsPage)).ToList(); viewModel.TotalPages = (int)Math.Ceiling((double)carriers.Count() / itemsPage); return View(viewModel); }
public ActionResult Create(int id) { var context = new Context(); var userId = int.Parse(HttpContext.User.Identity.Name); var exist = (from r in context.CarrierRating join c in context.Carriers on r.CarrierId equals c.Id where r.CarrierId == id && r.UserId == userId select r).Any(); if (exist) { return RedirectToAction("Edit", new { id = id }); } var carrier = (from p in context.Carriers where p.Id == id select p).FirstOrDefault(); if (carrier == null) { return RedirectToAction("Index", "Carriers"); } var viewModel = new RatingViewModel { CarrierId = carrier.Id, CarrierName = carrier.Name }; return View(viewModel); }
public ActionResult Details(int id) { var context = new Context(); var carrier = (from p in context.Carriers where p.Id == id select p).ToCarrierViewModel().FirstOrDefault(); if (carrier == null) { return RedirectToAction("Index"); } return View(carrier); }
public ActionResult Index() { var context = new Context(); var users = (from p in context.Users orderby p.Name select new UserViewModel { Name = p.Name, Id = p.Id, Username = p.Username }).ToList(); return View(users); }
public ActionResult Edit(int id) { var context = new Context(); var user = context.Users.FirstOrDefault(x => x.Id == id); if (user == null) { throw new Exception($"Usuário com código {id} não encontrado."); } var viewModel = new UserViewModel { Id = user.Id, Username = user.Username, Name = user.Name, Password = user.Password }; return View(viewModel); }
public ActionResult Edit(int id) { var userId = int.Parse(HttpContext.User.Identity.Name); var context = new Context(); var viewModel = (from r in context.CarrierRating join c in context.Carriers on r.CarrierId equals c.Id where r.CarrierId == id && r.UserId == userId select new RatingViewModel { CarrierId = r.CarrierId, CarrierName = c.Name, Rating = r.Rating }).FirstOrDefault(); if (viewModel == null) { return RedirectToAction("Index", "Carriers"); } 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 Index(LoginViewModel viewModel, string returnUrl) { if (!ModelState.IsValid) { return View(viewModel); } var context = new Context(); var user = (from p in context.Users where p.Username.ToUpper() == viewModel.Username.ToUpper() && p.Password == viewModel.Password select p).FirstOrDefault(); if (user == null) { ModelState.AddModelError("", "User and/or password are incorrect."); return View(viewModel); } FormsAuthentication.SetAuthCookie(user.Id.ToString(), viewModel.Remember); if (returnUrl != null) { return Redirect(returnUrl); } return RedirectToAction("Index", "Carriers"); }
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"); }