public HttpResponseMessage AddAlbum([FromBody]Album album)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, "Album is not valid entity.");
                }

                var albumToAdd = new Album() { Title = album.Title, Producer = album.Producer, Year = album.Year };
                this.data.Albums.Add(albumToAdd);

                foreach (var artist in album.Artists)
                {
                    var currentArtist = new Artist() { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth };
                    this.data.Artists.Add(currentArtist);
                    albumToAdd.Artists.Add(currentArtist);

                    foreach (var song in artist.Songs)
                    {
                        var currentSong = new Song() { Title = song.Title, Year = song.Year, Genre = song.Genre, AlbumId = albumToAdd.Id, ArtistId = currentArtist.Id };
                        this.data.Songs.Add(currentSong);
                        currentArtist.Songs.Add(currentSong);
                    }
                }

                this.data.SaveChanges();

                return Request.CreateResponse(HttpStatusCode.OK, "Album added.");
            }
            catch (Exception e)
            {
                return Request.CreateResponse(HttpStatusCode.ServiceUnavailable, "Error occured while saving album.");
            }
        }
Beispiel #2
0
        public static void Main()
        {
            var httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(serviceRoot);

            var album = new Album() { Title = "Reign of Hell", Producer = "Telerik Academy", Year = "2014" };

            var songs = new List<Song>();
            songs.Add(new Song() { Title = "DSA is coming!", Genre = "demonical", Year = "2014" });
            songs.Add(new Song() { Title = "We are not prepared!", Genre = "demonical", Year = "2014" });
            songs.Add(new Song() { Title = "Gonna sell my soul!", Genre = "demonical", Year = "2014" });

            var artists = new List<Artist>();
            artists.Add(new Artist() { Name = "Daniel", Country = "Bulgaria", DateOfBirth = DateTime.Parse("1986-11-06") });
            artists[0].Songs.Add(songs[0]);
            artists[0].Songs.Add(songs[1]);
            artists.Add(new Artist() { Name = "Ivan", Country = "Bulgaria", DateOfBirth = DateTime.Parse("1989-07-18") });
            artists[1].Songs.Add(songs[2]);

            album.Artists = artists;

            AddAlbum(httpClient, album);

            GetAlbums(httpClient);

            UpdateAlbumName(httpClient, 1, "Hope");

            GetAlbums(httpClient);

            DeleteAlbum(httpClient, 1);
        }
Beispiel #3
0
        static void AddAlbum(HttpClient httpClient, Album theAlbum)
        {
            HttpContent postContent = new StringContent(JsonConvert.SerializeObject(theAlbum));
            postContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var response = httpClient.PostAsync("albums", postContent).Result;

            try
            {
                response.EnsureSuccessStatusCode();
                Console.WriteLine("Album added!");
            }
            catch (Exception)
            {
                Console.WriteLine("Error adding album.");
            }
        }