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> LikeUser(int id, int recipientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _repo.GetLike(id, recipientId);

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

            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }

            like = new Like {
                LikerId = id,
                LikeeId = recipientId
            };

            _repo.Add <Like>(like);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to like user"));
        }