public async Task <IActionResult> Edit(int id, AlbumEditInputModel input) { var groups = await this.groupsService.GetAll <GroupDropDownViewModel>().ToListAsync(); if (!this.ModelState.IsValid) { input.Groups = groups; return(this.View(input)); } try { await this.albumsService.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 <IActionResult> Edit(int id, AlbumEditInputModel albumEditInputModel) { if (ModelState.IsValid) { var album = await _albumService.GetAlbumById(id); if (album == null) { return(new NotFoundResult()); } var authorization = await _authorizationService.AuthorizeAsync(User, album, "EditPolicy"); if (!authorization.Succeeded) { return(new ForbidResult()); } } var viewModel = Mapper.Map <AlbumEditInputModel, AlbumEditViewModel>(albumEditInputModel); return(this.View(viewModel)); }
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 albumsRepository = new EfDeletableEntityRepository <Album>(dbContext); var albumsService = new AlbumsService(albumsRepository, cloudinary.Object); await albumsRepository.AddAsync(new Album { Name = "Primo Victoria", CoverUrl = "https://res.cloudinary.com/nikolacgeorgiev/image/upload/v1587375808/albums_photos/Primo_Victoria_xhi3ny.jpg", ReleaseDate = DateTime.ParseExact("2005-03-04", "yyyy-MM-dd", CultureInfo.InvariantCulture), GroupId = 4, }); await albumsRepository.SaveChangesAsync(); var photo = new Mock <IFormFile>(); var releaseDate = DateTime.ParseExact("2012-05-22", "yyyy-MM-dd", CultureInfo.InvariantCulture); var id = await albumsService.CreateAsync("Carolus Rex", photo.Object, releaseDate, 4); var album = new AlbumEditInputModel { Name = "Primo Victoria", CoverUrl = "https://res.cloudinary.com/nikolacgeorgiev/image/upload/v1587375808/albums_photos/Primo_Victoria_xhi3ny.jpg", ReleaseDate = DateTime.ParseExact("2005-03-04", "yyyy-MM-dd", CultureInfo.InvariantCulture), GroupId = 4, }; await Assert.ThrowsAsync <ArgumentException>(async() => { await albumsService.EditAsync(id, album); }); }
public async Task <bool> EditAsync(int id, AlbumEditInputModel model) { var album = await this.albumsRepository .All() .FirstOrDefaultAsync(a => a.Id == id); var groupAllAlbums = await this.albumsRepository .All() .Where(a => a.GroupId == model.GroupId && a.Id != id) .Select(a => a.Name) .ToListAsync(); if (groupAllAlbums.Contains(model.Name)) { throw new ArgumentException(ErrorMessageAlbumExist); } var url = model.CoverUrl; if (model.Photo != null) { url = await this.cloudinaryService.UploadPhotoAsync( model.Photo, model.Name, GlobalConstants.CloudFolderForAlbumsPhotos); } album.Name = model.Name; album.CoverUrl = url; album.ReleaseDate = model.ReleaseDate; album.GroupId = model.GroupId; this.albumsRepository.Update(album); await this.albumsRepository.SaveChangesAsync(); return(true); }