예제 #1
0
        public async Task <IActionResult> AddBook(BookForCreationDto bookForCreationDto)
        {
            var book = _mapper.Map <Book>(bookForCreationDto);

            _repo.Add(book);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetBooks", null));
            }

            return(BadRequest("Could not add the book"));
        }
예제 #2
0
        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"));
        }