// PUT api/Album/5 public HttpResponseMessage PutAlbum(int id, Album album) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != album.AlbumId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } db.Entry(album).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// PUT api/Albums/5 public HttpResponseMessage PutAlbum(int id, Album album) { Album dbAlbum = DataMapper.CreateOrLoadAlbum(repository, album); album.Update(dbAlbum); repository.Update(id, dbAlbum); return Request.CreateResponse(HttpStatusCode.OK); }
public void Update(Album album) { if (this.Title != null) { album.Title = this.Title; } if (this.Producer != null) { album.Producer = this.Producer; } if (this.Year != null) { album.Year = this.Year; } }
// POST api/Album public HttpResponseMessage PostAlbum(Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public static Album CreateOrLoadAlbum(IRepository<Album> repository, Album entity) { Album album = repository.Get(entity.AlbumId); if (album != null) { return album; } Album newAlbum = repository.Add(new Album() { Title = entity.Title, Producer = entity.Producer, Year = entity.Year, Artists = entity.Artists, Songs = entity.Songs }); return newAlbum; }
public void Update(Album album) { if (this.Title != null) { album.Title = this.Title; } if (this.Producer != null) { album.Producer = this.Producer; } if (this.Year != null) { album.Year = this.Year; } if (this.Songs != null) { album.Songs = this.Songs; } if (this.Artists != null) { album.Artists = this.Artists; } }
public static void Edit(int id, Album album) { album.AlbumId = id; HttpResponseMessage responseMessage = client.PutAsXmlAsync("api/albums/" + id, album).Result; if (responseMessage.IsSuccessStatusCode) { Console.WriteLine("Album with id: {0} edited!", id); } else { Console.WriteLine("{0} ({1})", (int)responseMessage.StatusCode, responseMessage.ReasonPhrase); } }
public static void Add(string title, string producer, DateTime year, ICollection<Artist> artists, ICollection<Song> songs) { Album newAlbum = new Album { Title = title, Year = year, Artists = artists, Songs = songs, Producer = producer }; HttpResponseMessage responseMessage = client.PostAsJsonAsync("api/album", newAlbum).Result; if (responseMessage.IsSuccessStatusCode) { Console.WriteLine("Album added: {0}", newAlbum.Title); } else { Console.WriteLine("{0} ({1})", (int)responseMessage.StatusCode, responseMessage.ReasonPhrase); } }