public async Task <IActionResult> GetUsers([FromQuery] UserParams userParams)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            userParams.UserId = currentUserId;

            var usersFromRepo = await _repo.GetUser(currentUserId);

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

            var users = await _repo.GetUsers(userParams);

            //var usersToReturn= await _repo.GetUsers(userParams);

            var usersDTO = _mapper.Map <IEnumerable <UserListDTO> > (users);

            Response.AddPagination(
                users.CurrentPage,
                users.PageSize,
                users.TotalCount,
                users.TotalPages);
            return(Ok(usersDTO));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO MessageForCreationDto)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            MessageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(MessageForCreationDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("could not find user"));
            }

            var message = _mapper.Map <Message>(MessageForCreationDto);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDTO>(message);
                return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetUser(int id)
        {
            var user = await _repo.GetUser(id);

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

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

            var userForRepo = 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.Url      = uploadResult.Url.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

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

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

            userForRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDTO>(photo);
                return(CreatedAtRoute("GetPhoto", new  { userId = userId, id = photo.Id }, photoToReturn));
            }

            return(BadRequest("could not add the photo"));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> UploadPhotos(int userId, [FromForm] PhotoMediaDTO _photoMediaDTO)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            var file         = _photoMediaDTO.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);
                }
            }
            _photoMediaDTO.url      = uploadResult.Uri.ToString();
            _photoMediaDTO.publicId = uploadResult.PublicId;

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

            if (!userFromRepo.Photos.Any(ph => ph.isMain))
            {
                photo.isMain = true;
            }
            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoForReturn = _mapper.Map <PhotoForReturnDTO>(photo);
                return(CreatedAtRoute("GetImage", new { id = photo.Id }, photoForReturn));
                //return Ok("Image Uploaded");
            }
            else
            {
                return(BadRequest("Issues in Image Uploading"));
            }
        }
        public async Task <IActionResult> CreateMessage(int userId, MessageDTO messageDTO)
        {
            var sender = await _repo.GetUser(userId);

            if (sender == null)
            {
                return(BadRequest("Can not find the user"));
            }

            if (sender.id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageDTO.senderId = userId;

            var recipient = await _repo.GetUser(messageDTO.recipientId);

            if (recipient == null)
            {
                return(BadRequest("Can not find the user"));
            }

            var _message = _mapper.Map <Message>(messageDTO);

            _repo.Add(_message);

            if (await _repo.SaveAll())
            {
                var _messageToReturn = _mapper.Map <MessageDetailDTO>(_message);
                return(CreatedAtRoute("GetMessage", new
                {
                    id = _message.id
                },
                                      _messageToReturn
                                      ));
            }

            return(BadRequest("Unable to create message"));
        }