// POST api/Artists public IHttpActionResult Post([FromBody]ArtistServiceModel value) { var artist = new Artist { Name = value.Name, Country = value.Country, DateOfBirth = value.DateOfBirth }; ArtistsRepository.Add(artist); Data.SaveChanges(); return this.StatusCode(HttpStatusCode.Created); }
// POST api/Albums public IHttpActionResult Post([FromBody]AlbumServiceModel value) { var artists = new GenericRepository<Artist>(Data); var songs = new GenericRepository<Song>(Data); if (value.Artists != null) { foreach (var artist in value.Artists) { if (!artists.All().Any(x => x.Name == artist)) { var newArtist = new Artist { Name = artist }; artists.Add(newArtist); } Data.SaveChanges(); } } if (value.Songs != null) { foreach (var song in value.Songs) { if (!songs.All().Any(x => x.Title == song)) { var newSong = new Song { Title = song }; songs.Add(newSong); } Data.SaveChanges(); } } var album = new Album { Title = value.Title, Genre = value.Genre, ReleasedOn = value.ReleasedOn, Artists = artists.All().Where(x => value.Artists.Contains(x.Name)).ToList(), Songs = songs.All().Where(x => value.Songs.Contains(x.Title)).ToList() }; AlbumsRepository.Add(album); Data.SaveChanges(); return this.StatusCode(HttpStatusCode.Created); }
// POST api/Songs public IHttpActionResult Post([FromBody]SongServiceModel value) { var artists = new GenericRepository<Artist>(Data); if (!artists.All().Any(x => x.Name == value.Artist)) { var newArtist = new Artist { Name = value.Artist }; artists.Add(newArtist); Data.SaveChanges(); } var song = new Song { Title = value.Title, Genre = value.Genre, ReleasedOn = value.ReleasedOn, Artist = artists.All().FirstOrDefault(x => x.Name == value.Artist), ArtistId = artists.All().Where(x => x.Name == value.Artist).Select(x => x.ArtistId).FirstOrDefault() }; SongsRepository.Add(song); Data.SaveChanges(); return this.StatusCode(HttpStatusCode.Created); }
// PUT api/Songs/5 public IHttpActionResult Put(int id, [FromBody]SongServiceModel value) { var songToUpdate = SongsRepository.All().FirstOrDefault(x => x.SongId == id); if (songToUpdate == null) { return this.BadRequest("No such song"); } if (value.Title != null) { songToUpdate.Title = value.Title; } if (value.Genre != null) { songToUpdate.Genre = value.Genre; } if (value.ReleasedOn != null) { songToUpdate.ReleasedOn = value.ReleasedOn; } if (value.Artist != null) { var artists = new GenericRepository<Artist>(Data); if (!artists.All().Any(x => x.Name == value.Artist)) { var newArtist = new Artist { Name = value.Artist }; artists.Add(newArtist); Data.SaveChanges(); } songToUpdate.Artist = artists.All().FirstOrDefault(x => x.Name == value.Artist); songToUpdate.ArtistId = artists.All().Where(x => x.Name == value.Artist).Select(x => x.ArtistId).FirstOrDefault(); } Data.SaveChanges(); return this.ResponseMessage(new HttpResponseMessage(HttpStatusCode.Accepted)); }
// PUT api/Albums/5 public IHttpActionResult Put(int id, [FromBody]AlbumServiceModel value) { var albumToUpdate = AlbumsRepository.All().FirstOrDefault(x => x.AlbumId == id); if (albumToUpdate == null) { return this.BadRequest("No such album"); } var artists = new GenericRepository<Artist>(Data); var songs = new GenericRepository<Song>(Data); if (value.Artists != null) { foreach (var artist in value.Artists) { if (!artists.All().Any(x => x.Name == artist)) { var newArtist = new Artist { Name = artist }; artists.Add(newArtist); } Data.SaveChanges(); } albumToUpdate.Artists = artists.All().Where(x => value.Artists.Contains(x.Name)).ToList(); } if (value.Songs != null) { foreach (var song in value.Songs) { if (!songs.All().Any(x => x.Title == song)) { var newSong = new Song { Title = song }; songs.Add(newSong); } Data.SaveChanges(); } albumToUpdate.Songs = songs.All().Where(x => value.Songs.Contains(x.Title)).ToList(); } if (value.Title != null) { albumToUpdate.Title = value.Title; } if (value.Genre != null) { albumToUpdate.Genre = value.Genre; } if (value.ReleasedOn != null) { albumToUpdate.ReleasedOn = value.ReleasedOn; } Data.SaveChanges(); return this.ResponseMessage(new HttpResponseMessage(HttpStatusCode.Accepted)); }