Example #1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreatingDto messageForCreatingDto)
        {
            var sender = await _repo.GetUser(userId);

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

            messageForCreatingDto.SenderId = sender.Id;
            var recipent = await _repo.GetUser(messageForCreatingDto.RecipientId);

            if (recipent == null)
            {
                return(BadRequest("Could Not find User"));
            }

            var massage = _mapper.Map <Message>(messageForCreatingDto);


            _repo.Add <Message>(massage); // massage will return each  and every object in the return so we convert it again into DTO to filter the response
            // MessageForCreatingDto

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

            throw new Exception("Creating the message failed on save");
        }
Example #2
0
        public async Task <IActionResult> AddPhotoForUser(int userId,
                                                          [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = 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.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;

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

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

            //var photoToReturn  = _mapper.Map<PhotoForReturnDto>(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); // Also used in User Registraion (AuthController)
            }

            return(BadRequest("Could Not Add the Photo"));
        }
Example #3
0
        public async Task <IActionResult> GetUsers([FromQuery] UserParams userParam)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(currentUserId);

            userParam.UserId = currentUserId;

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

            var users = await _repo.GetUsers(userParam);

            var UserToReturn = _mapper.Map <IEnumerable <UserForListDto> >(users);

            Response.AddPagination(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(UserToReturn));
        }