예제 #1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTo messageForCreationDTo)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

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

            var sender = await _repo.GetUser(userId);

            messageForCreationDTo.UserId = userId;

            if (await _repo.GetUser(messageForCreationDTo.RecipientId) == null)
            {
                return(BadRequest("Couldn't Find User!"));
            }

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

            _repo.Add <Message>(message);


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

            return(BadRequest("Failure in message sending!"));
        }
예제 #2
0
        public async Task <IActionResult> photoUpload(int userId, [FromForm] PhotoFRomUserDto photoFRomUserDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(userId);

            var file   = photoFRomUserDto.File;
            var result = new ImageUploadResult();


            if (file.Length > 0)
            {
                using (var fileStream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, fileStream),
                        Transformation = new Transformation().Width(100).Height(100).Crop("fill").Gravity("face")
                    };


                    result = _cloudinary.Upload(uploadParams);
                }
            }


            photoFRomUserDto.Url      = result.Uri.ToString();
            photoFRomUserDto.PublicId = result.PublicId;

            var photo = _mapper.Map <Photos>(photoFRomUserDto);

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

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var returedPhoto = _mapper.Map <ReturnedPhotoDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.ID }, returedPhoto));
            }
            return(BadRequest("Couldn't Upload the photo"));
        }
예제 #3
0
        public async Task <IActionResult> getUsers([FromQuery] UserPrams prams)
        {
            prams.Id = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(prams.Id);

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

            var users = await _repo.GetUsers(prams);

            var usersForReturn = _mapper.Map <IEnumerable <UsersForListDto> >(users);

            Response.AddPagination(users.TotalCount, users.TotalPages, users.PageSize, users.CurrentPage);
            return(Ok(usersForReturn));
        }