public void Update(Music model) { if (model == null || model.Artist == null || model.Genre == null) { throw new ElementUpdateException <Music>(model, "Element incomplete."); } EntityModel.Music oldMusic = _context.Musics.Find(model.ID); EntityModel.Artist artist = _mapper.Map <Artist, EntityModel.Artist>(model.Artist); EntityModel.Genre genre = _mapper.Map <Genre, EntityModel.Genre>(model.Genre); if (oldMusic == null) { throw new ElementUpdateException <Music>(model, "Element to update doesn't exist."); } oldMusic.Name = model.Name; //oldMusic.Artist = artist; oldMusic.ArtistID = (int)artist.ID; //oldMusic.Genre = genre; oldMusic.GenreID = (int)genre.ID; try { _context.Musics.Update(oldMusic); _context.SaveChanges(); } catch (DbUpdateException ex) { throw new ElementUpdateException <Music>(model, "Element to update doesn't exist.", ex); } }
public void Update(Artist model) { EntityModel.Artist artist = _context.Artists.Find(model.ID); if (artist == null) { throw new ElementUpdateException <Artist>(model, "Element to update doesn't exist"); } artist.Name = model.Name; try { _context.Artists.Update(artist); _context.SaveChanges(); } catch (DbUpdateException ex) { throw new ElementUpdateException <Artist>(model, "Unable to update", ex); } }
public void DeleteById(int id) { EntityModel.Artist artist = _context.Artists.Include(a => a.Musics).Where(a => a.ID.Equals(id)).FirstOrDefault(); if (artist == null) { throw new ElementDeleteException <Artist>(id, "Element to delete doesn't exist."); } if (artist.Musics.Count > 0) { throw new ElementDeleteException <Artist>(id, "Element to delete can't be deleted because it has at least one music associated with it."); } try { _context.Artists.Remove(artist); _context.SaveChanges(); } catch (DbUpdateException ex) { throw new ElementDeleteException <Artist>(id, "Unable to delete", ex); } }