public async Task <IActionResult> Edit(int id, MemberEditInputModel input) { var groups = await this.groupsService.GetAll <GroupDropDownViewModel>().ToListAsync(); if (!this.ModelState.IsValid) { input.Groups = groups; return(this.View(input)); } try { await this.membersService.EditAsync(id, input); this.TempData["Success"] = EditSuccessMessage; return(this.Redirect("/Groups/Details/" + input.GroupId)); } catch (Exception e) { this.TempData["Error"] = e.Message; input.Groups = groups; return(this.View(input)); } }
public async Task EditeAsyncWithDublicateNameShouldThrowArgumentException() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var cloudinary = new Mock <ICloudinaryService>(); var membersRepository = new EfDeletableEntityRepository <Member>(dbContext); var service = new MembersService(membersRepository, cloudinary.Object); var photo = new Mock <IFormFile>(); await service.CreateAsync("Lars Ulrich", photo.Object, "description", 2); var id = await service.CreateAsync("Kirk Hammett", photo.Object, "description", 2); var model = new MemberEditInputModel { FullName = "Lars Ulrich", Photo = photo.Object, Description = "description", GroupId = 2, }; await Assert.ThrowsAsync <ArgumentException>(async() => { await service.EditAsync(id, model); }); }
public async Task EditAsyncWithCorrectDataShouldReturnCorrectResult() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; var dbContext = new ApplicationDbContext(options); var cloudinary = new Mock <ICloudinaryService>(); var membersRepository = new EfDeletableEntityRepository <Member>(dbContext); var service = new MembersService(membersRepository, cloudinary.Object); var photo = new Mock <IFormFile>(); var id = await service.CreateAsync("Kirk Hammett", photo.Object, "description", 2); var model = new MemberEditInputModel { FullName = "Kirk Hammett", Photo = photo.Object, Description = "Other", GroupId = 1, }; Assert.True(await service.EditAsync(id, model)); }
public async Task <bool> EditAsync(int id, MemberEditInputModel model) { var member = await this.membersRepository .All() .FirstOrDefaultAsync(m => m.Id == id); if (member.FullName.ToUpper() != model.FullName.ToUpper()) { var memberWithSameName = await this.membersRepository .All() .FirstOrDefaultAsync(m => m.FullName == model.FullName && m.Id != id); if (memberWithSameName != null) { throw new ArgumentException(ErrorMessageMemeberExist); } } var url = model.ImgUrl; if (model.Photo != null) { url = await this.cloudinaryService.UploadPhotoAsync( model.Photo, model.FullName, GlobalConstants.CloudFolderForMembersPhotos); } member.FullName = model.FullName; member.ImgUrl = url; member.Description = model.Description; member.GroupId = model.GroupId; this.membersRepository.Update(member); var result = await this.membersRepository.SaveChangesAsync(); return(result > 0); }