public async Task <IActionResult> DeleteBook(int id) { var book = await _repo.GetBook(id); if (book == null) { return(Unauthorized()); } foreach (var photo in book.Photos) { if (photo.PublicId != null) { var deleteParams = new DeletionParams(photo.PublicId); _cloudinaryConfig.Cloudinary.Destroy(deleteParams); } } _repo.Delete(book); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the book")); }
public IActionResult Get(int publisherId, int id) { var book = _rep.GetBook(publisherId, id); if (book == null) { return(NotFound()); } return(Ok(book)); }
public async Task <IActionResult> AddPhotoForBook(int bookId, [FromForm] PhotoForCreationDto photoForCreationDto) { var bookFromRepo = await _repo.GetBook(bookId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(500).Height(500) }; uploadResult = _cloudinaryConfig.Cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult?.Uri?.ToString(); photoForCreationDto.PublicId = uploadResult?.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!bookFromRepo.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } bookFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { bookId = bookId, id = photo?.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }