예제 #1
0
        public IHttpActionResult Update(int id, SongModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var existingSong = this.data.Songs.Find(id);

            if (existingSong == null)
            {
                return BadRequest(NoSuchSongId);
            }

            existingSong.Title = song.Title;
            existingSong.Year = song.Year;
            existingSong.Genre = song.Genre;
            existingSong.AlbumId = song.AlbumId;
            existingSong.ArtistId = song.ArtistId;
            this.data.SaveChanges();

            song.SongId = existingSong.Id;

            return Ok(song);
        }
예제 #2
0
        public IHttpActionResult Create(SongModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newSong = new Song
            {
                Title = song.Title,
                Year = song.Year,
                Genre = song.Genre,
                AlbumId = song.AlbumId,
                ArtistId = song.ArtistId
            };

            this.data.Songs.Add(newSong);
            this.data.SaveChanges();

            song.SongId = newSong.Id;

            return Ok(song);
        }