Exemplo n.º 1
0
        public async Task <IActionResult> GetUser(int id)
        {
            var user = await _repo.GetUser(id);

            var userToReturn = _mapper.Map <UserForDetailedDto>(user);

            return(Ok(userToReturn));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCeationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            var file         = photoForCeationDto.File;
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParms = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500).Height(500).Crop("fill").Gravity("face")
                    };
                    uploadResult = _cloudinary.Upload(uploadParms);
                }
            }
            photoForCeationDto.Url      = uploadResult.Uri.ToString();
            photoForCeationDto.PublicId = uploadResult.PublicId;
            var photo = _mapper.Map <Photo>(photoForCeationDto);

            if (!userFromRepo.photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }
            userFromRepo.photos.Add(photo);
            var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn1 = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn1));
            }
            return(BadRequest("Could not add the photo"));
        }