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(); }
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(); } }
// POST api/Artist public HttpResponseMessage PostArtist(Artist artist) { if (ModelState.IsValid) { CheckAlbumsInArtist(artist); db.Artists.Add(artist); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
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; }
// PUT api/Artist/5 public HttpResponseMessage PutArtist(int id, Artist artist) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != artist.Id) { return Request.CreateResponse(HttpStatusCode.BadRequest); } CheckAlbumsInArtist(artist); db.Entry(artist).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK, artist); }
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; }