public void Controller_AddWithSameName() { var model = new ContainerModel { FoodChoice = new UserModel { Name = "Michel" } }; var viewResult = (ViewResult)controller.Add(model); Assert.AreEqual("Le nom existe déjà !", GetErrorMessage(viewResult)); }
public void Controller_AddWithNameButNoChoice() { var model = new ContainerModel { FoodChoice = new UserModel { Name = "Simon", }, Choices = new List<ChoiceModel>() }; var viewResult = (ViewResult)controller.Add(model); Assert.AreEqual("Merci de cocher au moins une case !", GetErrorMessage(viewResult)); }
public ActionResult Add(ContainerModel model) { var usersChoices = FoodChoiceService.GetTodayUsersChoices(); var ipAddress = GetIP(Request); if (ModelState.IsValid) { var user = Converter.UserModelToUser(model); // check choices consistency var places = FoodChoiceService.GetPlaces(); ValidationService.RemoveInvalidEntry(user, places); var errorMessage = string.Empty; var allControlsAreOk = ValidationService.DoAllControls(user, usersChoices, out errorMessage); if (allControlsAreOk) { user.IpAddress = ipAddress; FoodChoiceService.AddUser(user); logger.Debug("Ajout de l'utilisateur: {0} - IP: {1}", user.Name, user.IpAddress); return RedirectToAction("Index"); } else { ModelState.AddModelError("Error", errorMessage); model.UsersChoices = Converter.UsersToUserModels(usersChoices); model.IpVisitor = ipAddress; model.Places = Converter.PlacesToPlaceModels(FoodChoiceService.GetPlaces()); model.ShowForm = true; logger.Error("Erreur lors de l'ajout: {0} - IP: {1} - erreur: {2}", model.FoodChoice.Name, model.IpVisitor, errorMessage); return View("Index", model); } } else { var allPlaces = FoodChoiceService.GetPlaces(); model.UsersChoices = Converter.UsersToUserModels(usersChoices); model.Places = Converter.PlacesToPlaceModels(allPlaces); model.ShowForm = true; logger.Error("Erreur lors de l'ajout: {0} - IP: {1}", model.FoodChoice.Name, ipAddress); return View("Index", model); } }
public ActionResult Edit(int id) { var user = FoodChoiceService.FindUserById(id); if (user != null) { var allPlaces = FoodChoiceService.GetPlaces(); var container = new ContainerModel { FoodChoice = Converter.UserToFoodChoiceModel(user), IpVisitor = GetIP(Request), Places = Converter.PlacesToPlaceModels(allPlaces), ShowForm = true }; return View("_Form", "Popup", container); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }
public void Controller_AddOneOrMoreChoicesAndSeats() { var model = new ContainerModel { FoodChoice = new UserModel { Name = "Charles", NumberOfAvailableSeats = 3 }, Choices = new List<ChoiceModel> { new ChoiceModel { PlaceId = 1 }, new ChoiceModel { PlaceId = 5 } } }; var routeResult = (RedirectToRouteResult)controller.Add(model); var method = routeResult.RouteValues.First().Value.ToString(); Assert.AreEqual("Index", method); }
public static User UserModelToUser(ContainerModel model) { // select only checked box: PlaceId > 0 var choices = model.Choices.Where(c => c.PlaceId > 0).Select(c => new Choice { UserId = c.UserId, PlaceId = c.PlaceId, OtherIdea = CheckOtherIdeaValue(c.Other) }); return new User { Id = model.FoodChoice.Id, Name = model.FoodChoice.Name, AvailableSeats = model.FoodChoice.NumberOfAvailableSeats, IpAddress = model.FoodChoice.IP, DepartureTime = model.FoodChoice.DepartureTime, Choices = choices.ToList() }; }
public void Controller_TODO() { var model = new ContainerModel { FoodChoice = new UserModel { Name = "Bob" } }; var viewResult = (ViewResult)controller.Add(model); Assert.AreEqual("Merci de cocher au moins une case !", GetErrorMessage(viewResult)); }
public ActionResult Index() { var places = FoodChoiceService.GetPlaces(); var usersChoices = FoodChoiceService.GetTodayUsersChoices(); var ipAddress = GetIP(Request); #if DEBUG var userAlreadyRegistered = false; User user = null; #else var userAlreadyRegistered = FoodChoiceService.UserAlreadyRegisteredToday(ipAddress); var user = FoodChoiceService.FindUserByIpAddress(ipAddress); #endif var model = new ContainerModel() { // empty form FoodChoice = new UserModel{ Name = user == null ? null : user.Name }, // users choices list UsersChoices = Converter.UsersToUserModels(usersChoices), // places list Places = Converter.PlacesToPlaceModels(places), IpVisitor = ipAddress, ShowForm = !userAlreadyRegistered }; return View(model); }
public ActionResult Update(ContainerModel model) { var ipAddress = GetIP(Request); if (ModelState.IsValid) { var user = Converter.UserModelToUser(model); // check choices consistency var places = FoodChoiceService.GetPlaces(); ValidationService.RemoveInvalidEntry(user, places); var errorMessage = string.Empty; if (ValidationService.ControlCheckBoxes(user, out errorMessage)) { FoodChoiceService.UpdateUser(user, ipAddress); logger.Debug("Modification de l'utilisateur: {0} - IP: {1}", user.Name, ipAddress); } return RedirectToAction("Index"); } else { // errors are not managed logger.Error("Erreur lors de la modification: {0} - IP: {1}", model.FoodChoice.Name, ipAddress); return RedirectToAction("Index"); } }