Пример #1
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            // Check if the user calling the api is the actual user who's profile is requested to be modified
            if (userId != 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);

            var messageToReturn = mapper.Map <MessageForCreationDto>(message);

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

            throw new Exception("Creating the message failed on save");
        }
Пример #2
0
        public async Task <IActionResult> LikeUser(int id, int recipientId)
        {
            // Check if the user calling the api is the actual user who's profile is requested to be modified
            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 liked 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!"));
        }