public IHttpActionResult ById(int id) { var artist = this.GetArtistById(id); var model = ArtistServiceModel.ConvertFromArtist(artist); return(Ok(model)); }
// PUT api/Artists/5 public IHttpActionResult Put(int id, [FromBody] ArtistServiceModel value) { var artistToUpdate = ArtistsRepository.All().FirstOrDefault(x => x.ArtistId == id); if (artistToUpdate == null) { return(this.BadRequest("No such artist")); } if (value.Name != null) { artistToUpdate.Name = value.Name; } if (value.Country != null) { artistToUpdate.Country = value.Country; } if (value.DateOfBirth != null) { artistToUpdate.DateOfBirth = value.DateOfBirth; } Data.SaveChanges(); return(this.ResponseMessage(new HttpResponseMessage(HttpStatusCode.Accepted))); }
private static async void PostRequests(HttpClient client) { var sampleArtist = new ArtistServiceModel { Name = "Pesho Testa", Country = "Bulgaria", DateOfBirth = new DateTime(1989, 10, 10) }; var sampleSong = new SongServiceModel { Title = "Pesen za Pesho", ReleasedOn = new DateTime(2015, 10, 10), Artist = sampleArtist.Name, Genre = "Punk" }; var sampleAlbum = new AlbumServiceModel { Title = "Pesho 2", Genre = "Psychedelic", Artists = new List <string> { "Pesho Testa" }, Songs = new List <string> { "Pesen za Pesho" } }; var httpResponseArtist = await client.PostAsJsonAsync("api/Artists", sampleArtist); var httpResponseSong = await client.PostAsJsonAsync("api/Songs", sampleSong); var httpResponseAlbum = await client.PostAsJsonAsync("api/Albums", sampleAlbum); }
public HttpResponseMessage Update(int id, ArtistServiceModel artistModel) { if (!this.ModelState.IsValid) { return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)); } var artist = this.GetArtistById(id); artist.Name = artistModel.Name; artist.DateOfBirth = artistModel.DateOfBirth; var country = this.data.Countries.All().FirstOrDefault(c => c.Name == artistModel.Country); if (country == null) { country = new Country { Name = artistModel.Country }; } artist.Country = country; if (artistModel.Albums.Count > 0) { artist.Albums.Clear(); foreach (var albumName in artistModel.Albums) { artist.Albums.Add(new Album { Name = albumName }); } } if (artistModel.Songs.Count > 0) { artist.Songs.Clear(); foreach (var songName in artistModel.Songs) { artist.Songs.Add(new Song { Name = songName }); } } this.data.Artists.Add(artist); this.data.SaveChanges(); artistModel.Id = artist.ArtistId; var response = this.Request.CreateResponse <ArtistServiceModel>(HttpStatusCode.OK, artistModel); return(response); }
private static async void AddArtist(HttpClient client, ArtistServiceModel artist) { HttpContent body = new StringContent(JsonConvert.SerializeObject(artist)); body.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.PostAsync("Artists/Add", body); string responsMessage = await response.Content.ReadAsStringAsync(); Console.WriteLine(responsMessage); }
public HttpResponseMessage Delete(int id) { var artist = this.GetArtistById(id); this.data.Artists.Delete(artist); var model = ArtistServiceModel.ConvertFromArtist(artist); var response = this.Request.CreateResponse(HttpStatusCode.OK, model); return(response); }
// POST api/Artists public IHttpActionResult Post([FromBody] ArtistServiceModel value) { var artist = new Artist { Name = value.Name, Country = value.Country, DateOfBirth = value.DateOfBirth }; ArtistsRepository.Add(artist); Data.SaveChanges(); return(this.StatusCode(HttpStatusCode.Created)); }
public string Update(ArtistServiceModel artistService, string id) { var a = context.Artists.FirstOrDefault(x => x.Id == id); a.Name = artistService.Name; a.Image = artistService.Image; a.Description = artistService.Description; context.Artists.Update(a); context.SaveChanges(); return(a.Id); }
public string Create(ArtistServiceModel artistService) { var artist = new Artist() { Name = artistService.Name, Image = artistService.Image, Description = artistService.Description }; context.Artists.Add(artist); context.SaveChanges(); return(artist.Id); }
public async Task <IActionResult> Create(CreateArtistViewModel artistViewModel) { string pictureUrl = await this.cloudinaryService.UploadPictureAsync(artistViewModel.Image, artistViewModel.Name); ArtistServiceModel artist = new ArtistServiceModel { Name = artistViewModel.Name, Description = artistViewModel.Description, Image = pictureUrl }; artistService.Create(artist); return(RedirectToAction("All", "Artists")); }
public HttpResponseMessage Add(ArtistServiceModel artistModel) { if (!this.ModelState.IsValid) { return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState)); } Artist newArtist = artistModel.ConvertToArtist(this.data); this.data.Artists.Add(newArtist); this.data.SaveChanges(); artistModel.Id = newArtist.ArtistId; var response = this.Request.CreateResponse <ArtistServiceModel>(HttpStatusCode.Created, artistModel); return(response); }
static void Main() { using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:2002/api/"); client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); GetAllArtists(client); Console.WriteLine("Press enter to proceed"); Console.ReadLine(); ArtistServiceModel artist = new ArtistServiceModel { Name = "LongJosh", Country = "Neverland", Albums = new List <string> { "Foundation", "The Rcok" } }; AddArtist(client, artist); Console.ReadLine(); } }
public async Task <IActionResult> Update(CreateArtistViewModel serviceModel) { string pictureUrl = await this.cloudinaryService.UploadPictureAsync(serviceModel.Image, serviceModel.Name); var id = context.Artists.FirstOrDefault(x => x.Name.ToLower() == serviceModel.Name.ToLower()); if (id != null) { ArtistServiceModel artist = new ArtistServiceModel { Name = serviceModel.Name, Description = serviceModel.Description, Image = pictureUrl }; artistService.Update(artist, id.Id); return(RedirectToAction("All", "Artists")); } else { return(NotFound());//RedirectToAction("All", "Artists"); } }