public IHttpActionResult Create(ArtistModels artist) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var newArtist = new Artist { Name = artist.Name, Country = artist.Country, BirthDate = artist.BirthDate, WebSite = artist.WebSite }; this.data.Artists.Add(newArtist); this.data.SaveChanges(); artist.Id = newArtist.Id; return(this.Ok(artist)); }
private static void UpdateArtist(int id, string newName, string newCountry, string newBirthDate, string newWebSite) { Console.WriteLine("Updating artist..."); var artist = new ArtistModels { Name = newName, Country = newCountry, BirthDate = newBirthDate, WebSite = newWebSite }; HttpResponseMessage response = client.PutAsJsonAsync(Artists + Update + id, artist).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist updated!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
private static void AddNewArtist(string name, string country, string birthdate, string website) { Console.WriteLine("Creating artist..."); var artist = new ArtistModels { Name = name, Country = country, BirthDate = birthdate, WebSite = website }; HttpResponseMessage response = client.PostAsJsonAsync(Artists + Create, artist).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist added!"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
public IHttpActionResult Update(int id, ArtistModels artist) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var existingArtist = this.data.Artists.All().FirstOrDefault(a => a.Id == id); if (existingArtist == null) { return(this.BadRequest("Such artist does not exists!")); } existingArtist.Name = artist.Name; existingArtist.Country = artist.Country; existingArtist.BirthDate = artist.BirthDate; if (artist.WebSite != null) { existingArtist.WebSite = artist.WebSite; } this.data.SaveChanges(); artist.Id = id; artist.WebSite = existingArtist.WebSite; var newArtist = new { Id = artist.Id, Name = artist.Name, Country = artist.Country, BirthDate = artist.BirthDate, WebSite = artist.WebSite }; return(this.Ok(newArtist)); }