public void EditShouldWorkCorrectlyWithValidModelState() { var caloriesBurned = 1500; var autoMapperConfig = new AutoMapperConfig(); autoMapperConfig.Execute(typeof(TrainingsController).Assembly); var trainingsServiceMock = new Mock <ITrainingsService>(); var training = new TrainingEditViewModel() { Calories = caloriesBurned, }; var predictionsServiceMock = new Mock <ITrainingPrediction>(); //predictionsServiceMock.Setup() var mock = new Mock <ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon"); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object); controller.ControllerContext = mock.Object; controller.WithCallTo(x => x.Edit(training)) .ShouldRedirectToRoute(""); }
public async Task <IActionResult> Edit(Guid id, TrainingEditViewModel model) { if (id == null) { return(NotFound()); } var training = await _context.Trainings .SingleOrDefaultAsync(m => m.Id == id); if (training == null) { return(NotFound()); } if (ModelState.IsValid) { training.Name = model.Name; training.NumOfLevelOneGame = model.NumOfLevelOneGame; training.NumOfLevelTwoGame = model.NumOfLevelTwoGame; training.NumOfLevelThreeGame = model.NumOfLevelThreeGame; await _context.SaveChangesAsync(); return(RedirectToAction("Index", "PersonalArea")); } return(View(model)); }
public ActionResult Edit(TrainingEditViewModel editTraining) { var training = db.Trainings.Find(editTraining.ID); if (training == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //チェックされたタグを取得 List <Tag> checkedTags = new List <Tag>(); var checkedTagValues = Request.Params["SearchTags"].Split(',').ToList(); checkedTagValues.ForEach(ct => checkedTags.Add(db.Tags.Find(int.Parse(ct)))); if (ModelState.IsValid) { training.Purpose = editTraining.Purpose; training.Title = editTraining.Title; training.YoutubeURL = editTraining.YoutubeURL; training.Tags = checkedTags; training.UpdateDateTime = DateTime.Now; db.Entry(training).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(training)); }
// GET: Trainings/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Training training = db.Trainings.Find(id); //ユーザー権限チェック var loginUserId = User.Identity.GetUserId(); if (training.ApplicationUserId != loginUserId) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //View用モデル生成 var trainingEditViewModel = new TrainingEditViewModel() { ID = training.ID, AddDateTime = training.AddDateTime.ToShortDateString(), Purpose = training.Purpose, Title = training.Title, UpdateDateTime = training.UpdateDateTime, YoutubeURL = training.YoutubeURL }; //ユーザー名を取得 string userName = ""; var userId = training.ApplicationUserId; var user = db.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { userName = user.UserName; } trainingEditViewModel.UserName = userName; //タグから表示用タグモデルを生成 List <ViewTagModel> ViewTagModels = new List <ViewTagModel>(); db.Tags.ToList().ForEach(t => ViewTagModels.Add(new ViewTagModel { ID = t.ID, Name = t.Name, Checked = false })); foreach (var tag in training.Tags) { ViewTagModels.FirstOrDefault(t => t.ID == tag.ID).Checked = true; } trainingEditViewModel.Tags = ViewTagModels; if (training == null) { return(HttpNotFound()); } return(View(trainingEditViewModel)); }
public async Task <ActionResult> Edit(TrainingEditViewModel model) { if (!ModelState.IsValid) { return(Json(ModelState.ToDictionary())); } var createdTraining = await _trainingServices.EditAsync(model, User.Identity.GetUserId()); return(Json(createdTraining)); }
public async Task <TrainingListViewModel> EditAsync(TrainingEditViewModel model, string adminId) { if (string.IsNullOrEmpty(adminId)) { throw new ArgumentException("Не е намерен администартора, който редактира Обучение!"); } if (!await _userManager.IsInRoleAsync(adminId, Enum.GetName(typeof(Role), Role.Administrator))) { throw new NotAuthorizedUserException("Потребителят няма право на това действие! Само админи имат право да редактират обучения !"); } var trainingToEdit = await _dbContext.Trainings .FirstOrDefaultAsync(t => t.Id == model.Id) ?? throw new ContentNotFoundException("Не е намерено обучението, което искате да редактирате!"); if (model.TrainingDate == null) { throw new ArgumentException("Не е въведена дата на обучението!"); } var oldThemeName = trainingToEdit.TrainingTheme; var oldThemeDate = trainingToEdit.TrainingDate; _dbContext.Trainings.Attach(trainingToEdit); trainingToEdit.TrainingDate = (DateTime)model.TrainingDate; trainingToEdit.AdditionalDescription = model.AdditionalDescription; trainingToEdit.TrainingTheme = model.TrainingTheme; trainingToEdit.TrainingMaterialsFolderLink = model.TrainingMaterialsFolderLink; await _dbContext.SaveChangesAsync(); #region notification var notificationToCreate = new NotificationCreateViewModel { NotificationTypeId = (int)NotificationType.Learning, NotificationLink = "/trainings/index?trainingId=" + model.Id, NotificationText = "Обучението на тема: " + oldThemeName + " от " + oldThemeDate.ToString("dddd, dd.MM.yyyyг. hh:mmч.") + "е преместено в " + ((DateTime)(model.TrainingDate)).ToString("dddd, dd.MM.yyyyг. hh:mmч.") + " с тема: " + model.TrainingTheme }; await _notificationCreator.CreateGlobalNotification(notificationToCreate, adminId); #endregion return(await Get(trainingToEdit.Id)); }
public ActionResult Edit(TrainingEditViewModel model) { if (!this.ModelState.IsValid) { this.ViewBag.Error = "Invalid model data"; return(this.View(model)); } Training training = this.trainings.GetById(model.Id); this.Mapper.Map(model, training); this.trainings.Save(); this.TempData["Success"] = "Successful edit."; return(this.RedirectToAction("Index")); }
// GET: Trainings/Edit/5 public async Task <IActionResult> Edit(Guid?id) { if (id == null) { return(NotFound()); } var training = await _context.Trainings .SingleOrDefaultAsync(m => m.Id == id); if (training == null) { return(NotFound()); } var model = new TrainingEditViewModel() { Name = training.Name, NumOfLevelOneGame = training.NumOfLevelOneGame, NumOfLevelTwoGame = training.NumOfLevelTwoGame, NumOfLevelThreeGame = training.NumOfLevelThreeGame }; return(View(model)); }
public void EditShouldWorkCorrectlyWithInvalidModelState() { var caloriesBurned = 1500; var autoMapperConfig = new AutoMapperConfig(); autoMapperConfig.Execute(typeof(TrainingsController).Assembly); var trainingsServiceMock = new Mock <ITrainingsService>(); var training = new TrainingEditViewModel() { Calories = caloriesBurned, }; var predictionsServiceMock = new Mock <ITrainingPrediction>(); //predictionsServiceMock.Setup() var mock = new Mock <ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon"); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object); controller.ControllerContext = mock.Object; controller.ModelState.AddModelError("FirstName", "First Name is Required"); controller.WithCallTo(x => x.Edit(training)) .ShouldRenderView("Edit") .WithModel <TrainingEditViewModel>( viewModel => { Assert.AreEqual(caloriesBurned, viewModel.Calories); }); }
public void EditShouldWorkCorrectlyWithValidModelState() { var caloriesBurned = 1500; var autoMapperConfig = new AutoMapperConfig(); autoMapperConfig.Execute(typeof(TrainingsController).Assembly); var trainingsServiceMock = new Mock<ITrainingsService>(); var training = new TrainingEditViewModel() { Calories = caloriesBurned, }; var predictionsServiceMock = new Mock<ITrainingPrediction>(); //predictionsServiceMock.Setup() var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon"); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object); controller.ControllerContext = mock.Object; controller.WithCallTo(x => x.Edit(training)) .ShouldRedirectToRoute(""); }
public void EditShouldWorkCorrectlyWithInvalidModelState() { var caloriesBurned = 1500; var autoMapperConfig = new AutoMapperConfig(); autoMapperConfig.Execute(typeof(TrainingsController).Assembly); var trainingsServiceMock = new Mock<ITrainingsService>(); var training = new TrainingEditViewModel() { Calories = caloriesBurned, }; var predictionsServiceMock = new Mock<ITrainingPrediction>(); //predictionsServiceMock.Setup() var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("anon"); mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var controller = new TrainingsController(trainingsServiceMock.Object, predictionsServiceMock.Object); controller.ControllerContext = mock.Object; controller.ModelState.AddModelError("FirstName", "First Name is Required"); controller.WithCallTo(x => x.Edit(training)) .ShouldRenderView("Edit") .WithModel<TrainingEditViewModel>( viewModel => { Assert.AreEqual(caloriesBurned, viewModel.Calories); }); }
public ActionResult Edit(TrainingEditViewModel model) { if (!this.ModelState.IsValid) { this.ViewBag.Error = "Invalid model data"; return this.View(model); } Training training = this.trainings.GetById(model.Id); this.Mapper.Map(model, training); this.trainings.Save(); this.TempData["Success"] = "Successful edit."; return this.RedirectToAction("Index"); }