// GET api/songs/5 public SongModel Get(int id) { var songEntity = this.songsRepository.Get(id); var songModel = new SongModel() { SongId = songEntity.SongId, Title = songEntity.Title, Genre = songEntity.Genre, }; return songModel; }
public IHttpActionResult Create(SongModel song) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var newSong = new Song() { Title = song.Title, Genre = song.Genre, Year = song.Year }; this.Data.Songs.Add(newSong); this.Data.SaveChanges(); song.Id = newSong.Id; return Ok(song); }
public IHttpActionResult Update(int id, SongModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var song = this.Data .Songs .All() .Where(s => s.Id == id) .Select(SongModel.FromSong) .FirstOrDefault(); if (song == null) { return BadRequest("There is no song with such id!"); } song.Title = model.Title; song.Genre = model.Genre; song.Year = model.Year; this.Data.SaveChanges(); model.Id = id; return Ok(model); }