public async Task <IActionResult> Create(CreateLogViewModel model) { ModelState.Remove("Log.UserId"); ModelState.Remove("Log.User"); if (ModelState.IsValid) { if (model.MyImage != null) { var uniqueFileName = GetUniqueFileName(model.MyImage.FileName); var uploads = Path.Combine(hostingEnvironment.WebRootPath, "images"); var filePath = Path.Combine(uploads, uniqueFileName); using (var myFile = new FileStream(filePath, FileMode.Create)) { model.MyImage.CopyTo(myFile); } model.Log.FileName = uniqueFileName; //to do : Save uniqueFileName to your db table } var user = await _userManager.GetUserAsync(HttpContext.User); model.Log.UserId = user.Id; _context.Add(model.Log); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Type", model.Log.CategoryId); ViewData["SellerId"] = new SelectList(_context.Sellers, "SellerId", "Name", model.Log.SellerId); return(View(model)); }
// GET: Stats/Edit/5 public async Task<IActionResult> Create() { CreateLogViewModel viewModel = new CreateLogViewModel(); viewModel.Boulders = await _context.Boulders.ToListAsync(); viewModel.SportClimbs = await _context.Sports.ToListAsync(); var List1 = viewModel.Boulders.Select( c => new { c.Area }); var List2 = viewModel.SportClimbs.Select( c => new { c.Area }); var aList = List1.Concat(List2).Distinct().ToList(); ViewData["UserID"] = UserIDSess; ViewData["Area"] = new SelectList(aList, "Area", "Area"); return View(viewModel); }
public void TestCreate_ShouldCallLogServiceCreateTrainingLog(int logId, string name, string description, string userId) { // Arrange var log = new TrainingLog { LogId = logId }; var mockedLogService = new Mock <ILogService>(); mockedLogService.Setup(s => s.CreateTrainingLog(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(log); var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>(); mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId); var mockedFactory = new Mock <IViewModelFactory>(); var model = new CreateLogViewModel { Description = description, Name = name }; var controller = new LogsController(mockedLogService.Object, mockedAuthenticationProvider.Object, mockedFactory.Object); // Act controller.Create(model); // Assert mockedLogService.Verify(p => p.CreateTrainingLog(name, description, userId), Times.Once); }
public void TestCreate_ShouldSetDetailsViewIdCorrectly(int logId, string name, string description, string userId) { // Arrange var log = new TrainingLog { LogId = logId }; var mockedLogService = new Mock <ILogService>(); mockedLogService.Setup(s => s.CreateTrainingLog(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(log); var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>(); mockedAuthenticationProvider.Setup(p => p.CurrentUserId).Returns(userId); var mockedFactory = new Mock <IViewModelFactory>(); var model = new CreateLogViewModel { Description = description, Name = name }; var controller = new LogsController(mockedLogService.Object, mockedAuthenticationProvider.Object, mockedFactory.Object); // Act, Assert controller .WithCallTo(c => c.Create(model)) .ShouldRedirectTo((LogsController c) => c.Details(logId, It.IsAny <int>(), It.IsAny <int>())); }
public async Task <IActionResult> Edit(int id, CreateLogViewModel model) { if (id != model.Log.Id) { return(NotFound()); } ModelState.Remove("Log.UserId"); ModelState.Remove("Log.User"); if (ModelState.IsValid) { var currentFileName = model.Log.FileName; if (model.MyImage != null && model.MyImage.FileName != currentFileName) { if (currentFileName != null) { var images = Directory.GetFiles("wwwroot/images"); var fileToDelete = images.First(i => i.Contains(currentFileName)); System.IO.File.Delete(fileToDelete); } var uniqueFileName = GetUniqueFileName(model.MyImage.FileName); var imageDirectory = Path.Combine(hostingEnvironment.WebRootPath, "images"); var filePath = Path.Combine(imageDirectory, uniqueFileName); using (var myFile = new FileStream(filePath, FileMode.Create)) { model.MyImage.CopyTo(myFile); } model.Log.FileName = uniqueFileName; } try { var user = await _userManager.GetUserAsync(HttpContext.User); model.Log.UserId = user.Id; _context.Update(model.Log); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!LogExists(model.Log.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Type", model.Log.CategoryId); ViewData["SellerId"] = new SelectList(_context.Sellers, "SellerId", "Name", model.Log.SellerId); return(View(model)); }
public ActionResult Create(CreateLogViewModel model) { if (!ModelState.IsValid) { return(this.View(model)); } var userId = this.authenticationProvider.CurrentUserId; var log = this.logService.CreateTrainingLog(model.Name, model.Description, userId); return(this.RedirectToAction("Details", new { id = log.LogId })); }
public void TestCreate_ModelStateIsNotValid_ShouldReturnViewWithModel() { // Arrange var mockedLogService = new Mock <ILogService>(); var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>(); var mockedFactory = new Mock <IViewModelFactory>(); var model = new CreateLogViewModel(); var controller = new LogsController(mockedLogService.Object, mockedAuthenticationProvider.Object, mockedFactory.Object); controller.ModelState.AddModelError("key", "value"); // Act, Assert controller .WithCallTo(c => c.Create(model)) .ShouldRenderDefaultView() .WithModel <CreateLogViewModel>(m => Assert.AreSame(model, m)); }
public void TestCreate_ShouldReturnCorrectViewWithModel() { // Arrange var mockedLogService = new Mock <ILogService>(); var mockedAuthenticationProvider = new Mock <IAuthenticationProvider>(); var model = new CreateLogViewModel(); var mockedFactory = new Mock <IViewModelFactory>(); mockedFactory.Setup(f => f.CreateCreateLogViewModel()).Returns(model); var controller = new LogsController(mockedLogService.Object, mockedAuthenticationProvider.Object, mockedFactory.Object); // Act, Assert controller .WithCallTo(c => c.Create()) .ShouldRenderDefaultView() .WithModel <CreateLogViewModel>(m => { Assert.AreSame(model, m); }); }
// GET: Logs/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var log = await _context.Logs.FindAsync(id); if (log == null) { return(NotFound()); } var viewModel = new CreateLogViewModel() { Log = log }; ViewData["CategoryId"] = new SelectList(_context.Categories, "CategoryId", "Type", log.CategoryId); ViewData["SellerId"] = new SelectList(_context.Sellers, "SellerId", "Name", log.SellerId); return(View(viewModel)); }
public async Task<IActionResult> Create(CreateLogViewModel viewModel) { viewModel.Log.UserID = UserIDSess; viewModel.Boulders = await _context.Boulders.ToListAsync(); viewModel.SportClimbs = await _context.Sports.ToListAsync(); if (viewModel.Log.Type == ClimbType.Boulder) { viewModel.Log.BoulderID = viewModel.climbID; } else { viewModel.Log.SportID = viewModel.climbID; } Log log = new Log { Type = viewModel.Log.Type, UserID = UserIDSess, ModifyDate = viewModel.Log.ModifyDate }; Debug.WriteLine("checking select valuye" + viewModel.climbID); if (viewModel.Log.Type == ClimbType.Boulder) { if (viewModel.climbID == null) { ModelState.AddModelError(nameof(viewModel.climbID), "Must select a climb."); } log.BoulderID = viewModel.climbID; } else { if (viewModel.climbID == null) { ModelState.AddModelError(nameof(viewModel.climbID), "Must select a climb."); } log.SportID = viewModel.climbID; } if (ModelState.IsValid) { _context.Logs.Add(log); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } var List1 = viewModel.Boulders.Select( c => new { c.Area }); var List2 = viewModel.SportClimbs.Select( c => new { c.Area }); Debug.WriteLine(viewModel.SportClimbs); Debug.WriteLine(List1.Concat(List2).Distinct().ToList()); var aList = List1.Concat(List2).Distinct().ToList(); ViewData["Area"] = new SelectList(aList, "Area", "Area"); return View(viewModel); }