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

            var existingSong = this.data.Songs.All().FirstOrDefault(a => a.Id == id);
            if (existingSong == null)
            {
                return BadRequest("Such aircraft does not exists!");
            }

            existingSong.Title = song.Title;
            existingSong.Year = song.Year;
            existingSong.Genre = song.Genre;
            existingSong.Length = song.Length;
            existingSong.Producer = song.Producer;
            this.data.Songs.SaveChanges();

            return Ok(existingSong);
        }
コード例 #2
0
        public IHttpActionResult Create(SongModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            else
            {
                var newSong = new Song
                {
                    Title = song.Title,
                    Year = song.Year,
                    Genre = song.Genre,
                    Length = song.Length,
                    Producer = song.Producer
                };

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

                song.Id = newSong.Id;
                return Ok(newSong);
            }
        }