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

            var userToReturn = mapper.Map <UserForDetailsDto>(user);

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

            var file         = photoforCreateDto.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);
                }
            }
            photoforCreateDto.Url      = uploadResult.Uri.ToString();
            photoforCreateDto.PublicId = uploadResult.PublicId;
            var photo = mapper.Map <Photo>(photoforCreateDto);

            photo.PuplicId = uploadResult.PublicId;
            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.Id }, photoToreturn));
            }
            return(BadRequest("Could not add the photo"));
        }