コード例 #1
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

            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).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }
            photoForCreationDto.PhotoUrl = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <Photo>(photoForCreationDto);

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }
            userFromRepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.PhotoId }, photoToReturn));
            }
            return(BadRequest("Could no Add The Photo"));
        }
コード例 #2
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoDto)
        {
            if (photoDto.File == null || photoDto.File.Length == 0)
            {
                return(BadRequest());
            }

            var imgFromCloud = Task.Run(() => this.UploadImageToCloud(photoDto.File));
            var userFromRepo = await this._repo.GetUser(userId);

            var img = await imgFromCloud;

            if (img.PublicId == null)
            {
                return(BadRequest("Could not add the photo"));
            }

            photoDto.Url      = img.Uri.ToString();
            photoDto.PublicId = img.PublicId;

            var photo = _mapper.Map <Photo>(photoDto);

            if (!userFromRepo.Photos.Any(p => p.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                // photo ref get if from db entity when _repo.SaveAll successfully executes
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                //created 201 response with route to source in location header
                return(CreatedAtRoute(nameof(this.GetPhoto), new { id = photo.Id }, photoToReturn));
            }
            ;
            return(BadRequest("Could not add the photo"));
        }
コード例 #3
0
        public async Task <IActionResult> AddPhotoForProduct(int userid, int productid, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var productFromRepo = await _repo.GetProduct(productid);

            var file = photoForCreationDto.File;

            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uplaodParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.FileName, stream),
                        Transformation = new Transformation().Width(300).Height(300).Crop("fill").Gravity("face")
                    };

                    uploadResult = _Cloudinary.Upload(uplaodParams);
                }
            }

            photoForCreationDto.Url      = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <Photo>(photoForCreationDto);

            if (!productFromRepo.Photos.Any(x => x.IsMain))
            {
                photo.IsMain = true;
            }

            _repo.Add <Photo>(photo);

            productFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo);
                return(Ok(photoForReturnDto));
                //return CreatedAtRoute("GetPhoto", new { userid = userid, id = photo.Id }, photoForReturnDto);
            }

            return(BadRequest("Could not add the photo"));
        }