public IHttpActionResult Create(Song song)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            db.Songs.Add(song);
            db.SaveChanges();
            return this.Ok();
        }
 public IHttpActionResult AddSong(int albumId, Song song)
 {
     if (!ModelState.IsValid)
     {
         return this.BadRequest(ModelState);
     }
     var album = db.Albums.FirstOrDefault(x => x.Id == albumId);
     if (album == null)
     {
         return this.BadRequest("No such album exists");
     }
     album.Songs.Add(song);
     db.SaveChanges();
     return this.Ok();
 }
 public static SongModel FromSong(Song song)
 {
     return new SongModel() { Title = song.Title, Genre = song.Genre };
 }
예제 #4
0
        // POST: api/Song
        public IHttpActionResult Post(SongRequestModel song)
        {
            var songToAdd = new Song
            {
                Title = song.Title,
                Year = song.Year
            };

            var genre = this.genreData.All()
                .FirstOrDefault(g => g.Name == song.Genre);
            if (genre == null)
            {
                return this.BadRequest("Invalid song genre!");
            }
            songToAdd.Genre = genre;

            var artist = this.artistData.All()
                .FirstOrDefault(a => a.Name == song.Artist);
            if (artist == null)
            {
                return this.BadRequest("Invalid song artist!");
            }
            songToAdd.Artist = artist;

            var album = this.albumData.All()
                .FirstOrDefault(a => a.Title == song.Album);
            if (album == null)
            {
                return this.BadRequest("Invalid album artist!");
            }
            songToAdd.Album = album;

            this.songData.Add(songToAdd);
            this.songData.SaveChanges();
            return this.Ok();
        }