public HttpResponseMessage PostAlbum(Album album) { album = repository.Add(album); var response = new HttpResponseMessage(HttpStatusCode.Created); string uri = Url.Route(null, new { id = album.Id }); response.Headers.Location = new Uri(Request.RequestUri, uri); return response; }
public HttpResponseMessage PutAlbum(int id, Album newAlbum) { newAlbum.Id = id; if (!repository.Update(newAlbum)) { return new HttpResponseMessage(HttpStatusCode.NotFound); } return new HttpResponseMessage(HttpStatusCode.OK); }
public Album Add(Album album) { using (var context = new StoreContext()) { //album.Id = nextId++; context.Albums.Add(album); context.SaveChanges(); return album; } }
public bool Update(Album newAlbum) { using (var context = new StoreContext()) { /* int index = context.Albums.ToList().FindIndex(a => a.Id == album.Id); if (index == -1) { return false; } context.Albums.ToList().RemoveAt(index); context.Albums.Add(album); context.SaveChanges(); return true; * */ var album = context.Albums.Where(a => a.Id == newAlbum.Id).FirstOrDefault<Album>(); if (album != null) { album.Id = newAlbum.Id; album.Name = newAlbum.Name; album.Genre = newAlbum.Genre; album.Artist = newAlbum.Artist; album.Price = newAlbum.Price; context.Entry(album).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return true; } return false; } }