public async Task <IActionResult> PostAsync(string userId, [FromForm] SaveArtworkResource saveArtworkResource) { if (!ModelState.IsValid) { return(BadRequest("invalid post request data was sent")); } string webRootPath = env.WebRootPath; string imagesFolderPath = Path.Combine(webRootPath, "images"); string filePath = await CreateImageFile(saveArtworkResource.ImageFile, imagesFolderPath); if (filePath == null) { return(BadRequest("Image file could not be saved")); } Artwork artwork = mapper.Map <SaveArtworkResource, Artwork>(saveArtworkResource); artwork.ApplicationUserId = userId; artwork.ImageUrl = filePath; ArtworkResponse artworkResponse = await artworkService.SaveArtworkAsync(artwork); if (!artworkResponse.Success) { return(BadRequest(artworkResponse.Message)); } ArtworkResource artworkResource = mapper.Map <Artwork, ArtworkResource>(artworkResponse.Resource); return(Ok(artworkResource)); }
public async Task <IActionResult> PutAsync(int artworkId, [FromForm] UpdateArtworkResource updateArtworkResource) { Artwork existingArtwork = await artworkService.FindArtworkByIdAsync(artworkId); if (existingArtwork == null) { BadRequest("Artwork does not exist"); } updateArtworkResource.Name ??= existingArtwork.Name; updateArtworkResource.Description ??= existingArtwork.Description; if (updateArtworkResource.ImageFile == null) { updateArtworkResource.ImageUrl = existingArtwork.ImageUrl; } else { try { string webRootPath = env.WebRootPath; string imagesFolderPath = Path.Combine(webRootPath, "images"); string filePath = await CreateImageFile(updateArtworkResource.ImageFile, imagesFolderPath); if (filePath == null) { return(BadRequest("Image file could not be saved")); } updateArtworkResource.ImageUrl = filePath; string oldImageFilePath = Path.Combine(webRootPath, existingArtwork.ImageUrl); System.IO.File.Delete(oldImageFilePath); } catch (Exception ex) { BadRequest(ex.Message); } } Artwork artwork = mapper.Map <UpdateArtworkResource, Artwork>(updateArtworkResource); ArtworkResponse artworkResponse = await artworkService.UpdateArtworkAsync(artworkId, artwork); if (!artworkResponse.Success) { return(BadRequest(artworkResponse.Message)); } ArtworkResource artworkResource = mapper.Map <Artwork, ArtworkResource>(artworkResponse.Resource); return(Ok(artworkResource)); }
public async Task <IActionResult> DeleteAsync(int artworkId) { ArtworkResponse artworkResponse = await artworkService.DeleteArtworkAsync(artworkId); if (!artworkResponse.Success) { return(BadRequest(artworkResponse.Message)); } ArtworkResource artworkResource = mapper.Map <Artwork, ArtworkResource>(artworkResponse.Resource); return(Ok(artworkResource)); }