public override async Task <Model.Album> Update(int id, AlbumUpsertRequest request) { var entity = _context.Albums.Find(id); _context.Albums.Attach(entity); _context.Albums.Update(entity); foreach (var TrackID in request.Tracks) { var albumTrack = await _context.AlbumTracks .Where(i => i.TrackID == TrackID && i.AlbumID == id) .SingleOrDefaultAsync(); if (albumTrack == null) { var newAlbumTrack = new Database.AlbumTrack() { AlbumID = id, TrackID = TrackID }; await _context.Set <Database.AlbumTrack>().AddAsync(newAlbumTrack); } } foreach (var TrackID in request.TracksToDelete) { var albumTrack = await _context.AlbumTracks .Where(i => i.TrackID == TrackID && i.AlbumID == id) .SingleOrDefaultAsync(); if (albumTrack != null) { _context.Set <Database.AlbumTrack>().Remove(albumTrack); } } _mapper.Map(request, entity); await _context.SaveChangesAsync(); return(_mapper.Map <Model.Album>(entity)); }
public override async Task <Model.Album> Insert(AlbumUpsertRequest request) { var entity = _mapper.Map <Database.Album>(request); await _context.Albums.AddAsync(entity); await _context.SaveChangesAsync(); foreach (var trackID in request.Tracks) { var albumTrack = new Database.AlbumTrack() { AlbumID = entity.ID, TrackID = trackID }; _context.AlbumTracks.Add(albumTrack); } await _context.SaveChangesAsync(); return(_mapper.Map <Model.Album>(entity)); }