Exemple #1
0
        public async Task <IActionResult> AddPhotoForUser(string userId, [FromForm]  PhotoForCreationDto photoForCreationDto)
        {
            if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetPersonById(userId);

            var file = photoForCreationDto.File;

            var photoForCreation = await _photoRepo.UploadPhotoToCloud(photoForCreationDto, _clodinaryConfig);

            var photo = Helpers.HelperMapper.MapPhotoForCreationDtoToPhoto(photoForCreation);

            // check if there is no main Photo for the person
            if (userFromRepo.Photos != null)
            {
                if (!userFromRepo.Photos.Any(p => p.IsMain))
                {
                    photo.IsMain = true;
                }
                var currentPhototCount = userFromRepo.Photos.Count();
                photo.Id = userFromRepo.Photos.Last().Id + 1;
            }
            else
            {
                photo.Id     = 0;
                photo.IsMain = true;
            }

            if (userFromRepo.Photos == null)
            {
                userFromRepo.Photos = new List <Photo>();
            }

            userFromRepo.Photos.Add(photo);

            var response = await _repo.UpdatePersonsData(userId, userFromRepo);

            if (response == HttpStatusCode.OK)
            {
                var phototToReturn = Helpers.HelperMapper.MapPhotoToPhotoForReturnDto(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, phototToReturn));
            }


            return(BadRequest("Adding photo failed"));
        }
        public async Task <IActionResult> Get([FromQuery] UserParams userParams)
        {
            var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            var userFromRepo = await _cosmosManager.GetPersonById(currentUserId);

            userParams.UserId = currentUserId;

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = userFromRepo.Gender == "male" ? "female" : "male";
            }

            var users = await _cosmosManager.GetUsers(userParams);

            // var usersToReturn = _mapper.Map<IEnumerable<UserForListDto>>(users);
            var usersToReturn = Helpers.HelperMapper.MapUserToUserForListDto(users);

            // adding the pagination header information
            Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(usersToReturn));
        }