public IHttpActionResult Create(SongModel song) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var artistOfSong = this.data.Artist.SelectAll().FirstOrDefault(a => a.Id == song.Id); if (artistOfSong == null) { return BadRequest("Can not find artist by given id!"); } this.data.Songs.Add(new Song { Title = song.Title, Genre = song.Genre, Year = song.Year, ArtistId = artistOfSong.Id }); this.data.SaveChanges(); return Ok(HttpStatusCode.Created); }
public IHttpActionResult Update(int id, SongModel song) { var existingSong = this.data.Songs.SelectAll().FirstOrDefault(a => a.Id == id); if (existingSong == null) { return BadRequest("No such song with this id!"); } existingSong.Title = (song.Title != null) ? song.Title : existingSong.Title; existingSong.Genre = (song.Genre != null) ? song.Genre : existingSong.Genre; existingSong.Year = (song.Year != 0) ? song.Year : existingSong.Year; this.data.Songs.Update(existingSong); this.data.SaveChanges(); return Ok(HttpStatusCode.OK); }
public IHttpActionResult Update(int albumId, SongModel song) { var album = this.data.Albums.SelectAll().FirstOrDefault(a => a.Id == albumId); var albumSong = album.Songs.FirstOrDefault(s => s.Id == song.Id); if (albumSong == null) { return BadRequest("The album does not have that song!"); } albumSong.Genre = song.Genre; albumSong.Title = song.Title; albumSong.Year = song.Year; this.data.Songs.Update(albumSong); this.data.SaveChanges(); return Ok(HttpStatusCode.OK); }