private static void UpadteGenre() { Console.WriteLine("Put Producer"); using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:60039/"); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var album = new GenreRequestModel() { Name = "Updated" }; var postContent = new StringContent(JsonConvert.SerializeObject(album)); postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = client.PutAsync("api/genre/2", postContent).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Put succsessful"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } } }
// POST: api/Genre public IHttpActionResult Post(GenreRequestModel genreInfo) { if (genreInfo == null) { return(this.BadRequest("Invalid gernre format!")); } this.genreData.Add(new Genre { Name = genreInfo.Name }); this.genreData.SaveChanges(); return(this.Ok("Genre added!")); }
public IHttpActionResult Update(GenreRequestModel targetGenre) { var genreToUpdate = this.db.Genres .FirstOrDefault(c => c.Id == targetGenre.Id); if (genreToUpdate == null) { return(this.NotFound()); } genreToUpdate.Name = targetGenre.Name; this.db.SaveChanges(); return(this.Ok(string.Format("The name of the genre with id {0}, was successfully updated to {1}", targetGenre.Id, targetGenre.Name))); }
public IHttpActionResult Post(GenreRequestModel genreToAdd) { if (!this.ModelState.IsValid) { return(this.BadRequest()); } var dbGenre = new Genre { Name = genreToAdd.Name }; this.db.Genres.Add(dbGenre); this.db.SaveChanges(); return(this.Ok(string.Format("{0} was added to Genres database!", genreToAdd.Name))); }
public IHttpActionResult Post(GenreRequestModel genreToAdd) { if (!this.ModelState.IsValid) { return this.BadRequest(); } var dbGenre = new Genre { Name = genreToAdd.Name }; this.db.Genres.Add(dbGenre); this.db.SaveChanges(); return this.Ok(string.Format("{0} was added to Genres database!", genreToAdd.Name)); }
public IHttpActionResult Post([FromBody] GenreRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var entity = new Genre { Name = model.Name }; this.context.AddEntity(entity); this.context.Save(); return(this.Created(this.Url.ToString(), entity)); }
// PUT: api/Genre/5 public IHttpActionResult Put(int id, GenreRequestModel genreInfo) { if (genreInfo == null) { return(this.BadRequest("Invalid genre format!")); } var genreToModify = this.genreData.All() .FirstOrDefault(g => g.GenreId == id); if (genreToModify == null) { return(this.BadRequest("Invalid gernre id")); } genreToModify.Name = genreInfo.Name; this.genreData.SaveChanges(); return(this.Ok("Genre modified!")); }
public IHttpActionResult Put(int id, [FromBody] GenreRequestModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var entity = this.context.GetById(id); if (entity == null) { return(this.BadRequest("Genre not found!")); } this.context.Update(entity); this.context.Save(); return(this.Ok("[" + entity.Name + "] genre has been UPDATED!")); }
public IHttpActionResult Update(GenreRequestModel targetGenre) { var genreToUpdate = this.db.Genres .FirstOrDefault(c => c.Id == targetGenre.Id); if (genreToUpdate == null) { return this.NotFound(); } genreToUpdate.Name = targetGenre.Name; this.db.SaveChanges(); return this.Ok(string.Format("The name of the genre with id {0}, was successfully updated to {1}", targetGenre.Id, targetGenre.Name)); }