Example #1
0
        // 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));
        }
        private static async void PostRequests(HttpClient client)
        {
            var sampleArtist = new ArtistServiceModel
            {
                Name        = "Pesho Testa",
                Country     = "Bulgaria",
                DateOfBirth = new DateTime(1989, 10, 10)
            };

            var sampleSong = new SongServiceModel
            {
                Title      = "Pesen za Pesho",
                ReleasedOn = new DateTime(2015, 10, 10),
                Artist     = sampleArtist.Name,
                Genre      = "Punk"
            };

            var sampleAlbum = new AlbumServiceModel
            {
                Title   = "Pesho 2",
                Genre   = "Psychedelic",
                Artists = new List <string> {
                    "Pesho Testa"
                },
                Songs = new List <string> {
                    "Pesen za Pesho"
                }
            };

            var httpResponseArtist = await client.PostAsJsonAsync("api/Artists", sampleArtist);

            var httpResponseSong = await client.PostAsJsonAsync("api/Songs", sampleSong);

            var httpResponseAlbum = await client.PostAsJsonAsync("api/Albums", sampleAlbum);
        }
Example #3
0
        // 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)));
        }