Ejemplo n.º 1
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.º 2
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.º 3
0
        public async Task <IActionResult> GetLike(int id, int recipientId)
        {
            if (await _repo.GetUser(id) == null)
            {
                return(Unauthorized());
            }
            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }
            var like = await _repo.GetLike(id, recipientId);

            if (like != null)
            {
                return(BadRequest("You Already liked this user"));
            }

            like = new Like
            {
                LikeeId = recipientId,
                LikerId = id
            };
            _repo.Add <Like>(like);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to like User"));
        }
Ejemplo n.º 4
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> UpdateUser(int id, UserDataForUpdateDTO updateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(updateDTO, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user{id} failed on saved");
        }
        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"));
        }