示例#1
0
        public async Task<IActionResult> AddBook([FromForm]BookForCreationDto bookForCreationDto)
        {
            //bookForCreationDto.Title = bookForCreationDto.Title.ToLower();

            if (await _repo.BookExists(bookForCreationDto.Title))
                return BadRequest("Book Name already Exist");

            var file = bookForCreationDto.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).Crop("fill").Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            bookForCreationDto.PhotoUrl = uploadResult.Uri.ToString();
            bookForCreationDto.PublicId = uploadResult.PublicId;
            //var userToCreate = new User { Username = userForRegisterDto.Username };
            var bookToCreate = _mapper.Map<Book>(bookForCreationDto);

            var createdbook = await _repo.AddBook(bookToCreate);

            return CreatedAtRoute("GetBook", new{Controller = "book", id =createdbook.Id}, createdbook);
        }