Exemplo n.º 1
0
        public async Task <IActionResult> LikeCarmodel(int id, int carmodelId)
        {
            // if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            // {
            //     return Unauthorized();
            // }

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

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

            if (await _repo.GetCarModel(carmodelId) == null)
            {
                return(NotFound());
            }

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

            _repo.Add <Like>(like);

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

            return(BadRequest("Failed to like car"));
        }
Exemplo n.º 2
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.GetCarModel(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);
                return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn));
            }

            throw new System.Exception("Creating the message failed on save");
        }