public async Task Update_Song_With_Genres_Should_Persiste() { using (var factory = new MusicStoreContextFactory()) { var genresIds = new List <int>(); var songId = 0; using (var context = factory.CreateMusicStoreContext()) { var genres = new List <GenreEntity> { new GenreEntity(), new GenreEntity(), new GenreEntity() }; context.Genres.AddRange(genres); context.SaveChanges(); genresIds = genres.Select(s => s.Id).ToList(); var song = new SongEntity { Name = "First Song", GenreSong = new List <GenreSongEntity> { new GenreSongEntity { GenreId = genresIds[0] }, new GenreSongEntity { GenreId = genresIds[1] } } }; context.Songs.Add(song); context.SaveChanges(); songId = song.Id; } using (var context = factory.CreateMusicStoreContext()) { var unitOfWork = new UnitOfWork(context, _mapper); var songDto = new SongForUpdatingDto { Id = songId, GenresIds = new List <int> { genresIds[2] } }; await unitOfWork.Songs.UpdateAsync(songDto); await unitOfWork.SaveAsync(); } using (var context = factory.CreateMusicStoreContext()) { var song = context.Songs.Include(c => c.GenreSong).First(); Assert.Single(song.GenreSong); } } }
public async Task UpdateAsync(SongForUpdatingDto dto) { var songEntity = await context.Songs .Include(s => s.GenreSong) .FirstOrDefaultAsync(s => s.Id == dto.Id); mapper.Map(dto, songEntity); }
public async Task <IActionResult> UpdateeSong( int id, [FromBody] SongForUpdatingDto dto) { if (!ModelState.IsValid) { return(BadRequest()); } try { var song = await unitOfWork.Songs.GetAsync(id); if (song == null) { return(NotFound()); } if (song.Id != id) { return(BadRequest()); } // Validate the album if exist if (dto.AlbumId != null && !await unitOfWork.Albums.Exist(dto.AlbumId)) { dto.AlbumId = null; } var isAuthorized = await authorizationService .AuthorizeAsync(User, song.OwenerId, AutherazationOperations.OwenResourse); if (!isAuthorized.Succeeded) { return(Unauthorized()); } await unitOfWork.Songs.UpdateAsync(dto); await unitOfWork.SaveAsync(); song = await unitOfWork.Songs.GetAsync(id); return(Ok(song)); } catch (Exception ex) { logger.LogError(ex, "An error occurred while updating song.", dto); return(StatusCode(500)); } }
public async Task Update_Song_Should_Persiste() { using (var factory = new MusicStoreContextFactory()) { int songId = 0; using (var context = factory.CreateMusicStoreContext()) { var song = new SongEntity { Name = "First Song", Album = new AlbumEntity() }; context.Songs.Add(song); context.SaveChanges(); songId = song.Id; } using (var context = factory.CreateMusicStoreContext()) { var songForUpdateDto = new SongForUpdatingDto { Id = songId, Name = "Updated" }; var unitOfWork = new UnitOfWork(context, _mapper); await unitOfWork.Songs.UpdateAsync(songForUpdateDto); await unitOfWork.SaveAsync(); } using (var context = factory.CreateMusicStoreContext()) { var song = context.Songs.First(); Assert.Equal("Updated", song.Name); Assert.Null(song.AlbumId); } } }