Beispiel #1
0
        public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var currentUser   = await _repository.GetUser(currentUserId, false);

            userParams.UserId = currentUserId;

            var dbUsers = await _repository.GetUsers(userParams);

            var users = _mapper.Map <IEnumerable <UserListDto> >(dbUsers);

            Response.AddPagination(dbUsers.PageNumber, dbUsers.PageSize, dbUsers.TotalCount, dbUsers.TotalPages);

            return(Ok(users));
        }
Beispiel #2
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoCreationDto photoCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var dbUser = await _repository.GetUser(userId, true);

            var file = photoCreationDto.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(600).Height(600).Crop("fill").Gravity("auto")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoCreationDto.Url      = uploadResult.SecureUri.ToString();
            photoCreationDto.PublicId = uploadResult.PublicId;

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

            if (!dbUser.Photos.Any(p => p.IsProfilePhoto))
            {
                photo.IsProfilePhoto = true;
            }

            dbUser.Photos.Add(photo);

            if (await _repository.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoDetailsDto>(photo);

                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn));
            }

            return(BadRequest("Error with adding the photo."));
        }