예제 #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 recepient = await _repo.GetUser(messageForCreationDto.RecepientId);

            if (recepient == null)
            {
                return(BadRequest("Could not find the user"));
            }

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

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);

                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Message failed on save");
        }
예제 #2
0
        public async Task <IActionResult> FollowUser(int id, int recepientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var follow = await _repo.GetFollow(id, recepientId);

            if (follow != null)
            {
                return(BadRequest("you have already followed this user"));
            }

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

            follow = new Follow {
                FollowerId = id,
                FolloweeId = recepientId
            };

            _repo.Add <Follow>(follow);

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

            throw new Exception("Failed to follow the user");
        }