Esempio n. 1
0
        protected void btnAlbumSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtAlbumTitle.Text))
            {
                var album = new Album();
                album.Title = txtAlbumTitle.Text;

                int year = 0;
                if (int.TryParse(txtAlbumYear.Text, out year))
                {
                    album.Year = year;
                }

                hfArtists.Value.Split(',').ToList().ForEach(item =>
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        var values = item.Split(';');
                        if (values.Count() > 1)
                        {
                            var artist = new Artist()
                            {
                                Id = int.Parse(values[0]),
                                Name = values[1]
                            };
                            album.Artists.Add(artist);
                        }
                    }
                });

                album.Producer = txtAlbumProducer.Text;

                var response = new HttpResponseMessage();
                if (!string.IsNullOrEmpty(btnAlbumSave.CommandArgument))
                {
                    album.Id = int.Parse(btnAlbumSave.CommandArgument);
                    response = SessionState.Client.PutAsJsonAsync("api/album/" + album.Id, album).Result;
                }
                else
                {
                    response = SessionState.Client.PostAsJsonAsync("api/album", album).Result;
                }

                if (response.IsSuccessStatusCode)
                {
                    var a = response.Content.ReadAsAsync<HttpResponseMessage>().Result;
                    grdAlbumsFill();
                }
            }

            ClearForm();
        }
Esempio n. 2
0
        protected void btnArtistSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtArtistName.Text))
            {
                var artist = new Artist
                {
                    Name = txtArtistName.Text,
                    Country = txtArtistCountry.Text,
                    DateOfBirth = DateTime.Now
                };

                hfAlbums.Value.Split(',').ToList().ForEach(item =>
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        var values = item.Split(';');
                        if (values.Count() > 1)
                        {
                            var album = new Album()
                            {
                                Id = int.Parse(values[0]),
                                Title = values[1]
                            };
                            artist.Albums.Add(album);
                        }
                    }
                });

                var response = new HttpResponseMessage();
                if (!string.IsNullOrEmpty(btnArtistSave.CommandArgument))
                {
                    artist.Id = int.Parse(btnArtistSave.CommandArgument);
                    response = SessionState.Client.PutAsJsonAsync("api/artist/" + artist.Id, artist).Result;
                }
                else
                {
                    response = SessionState.Client.PostAsJsonAsync("api/artist", artist).Result;
                }

                if (response.IsSuccessStatusCode)
                {
                    var a = response.Content.ReadAsAsync<HttpResponseMessage>().Result;
                    grdArtistsFill();
                }

                ClearForm();
            }
        }
Esempio n. 3
0
        // POST api/Album
        public HttpResponseMessage PostAlbum(Album album)
        {
            if (ModelState.IsValid)
            {
                CheckArtistsInAlbums(album);

                db.Albums.Add(album);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album);
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Esempio n. 4
0
        // PUT api/Album/5
        public HttpResponseMessage PutAlbum(int id, Album album)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != album.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            //var oldAlbum = db.Albums.FirstOrDefault(a => a.Id == album.Id);
            //if (oldAlbum != null)
            //{
            //    oldAlbum.Title = album.Title;
            //    oldAlbum.Year = album.Year;
            //    oldAlbum.Producer = album.Producer;
            //    CheckArtistsInAlbums(album);
            //    oldAlbum.Artists = album.Artists;
            //}

            CheckArtistsInAlbums(album);
            db.Entry(album).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, album);
        }
Esempio n. 5
0
 private void CheckArtistsInAlbums(Album album)
 {
     var artists = new List<Artist>();
     album.Artists.ForEach(artist =>
     {
         var a = db.Artists.FirstOrDefault(x => x.Id == artist.Id);
         if (a == null)
         {
             a = new Artist();
             a.DateOfBirth = DateTime.Now;
         }
         artists.Add(a);
     });
     album.Artists = artists;
 }
Esempio n. 6
0
 private void CheckAlbumsInArtist(Artist artist)
 {
     var artists = new List<Album>();
     artist.Albums.ForEach(album =>
     {
         var a = db.Albums.FirstOrDefault(x => x.Id == album.Id);
         if (a == null)
         {
             a = new Album();
         }
         artists.Add(a);
     });
     artist.Albums = artists;
 }