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");
        }
Esempio n. 2
0
        public async Task <IActionResult> WatchListing(int id, int listingId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (await _repo.GetListing(listingId) == null)
            {
                return(NotFound());
            }

            var listingWatch = await _repo.GetListingWatch(id, listingId);

            string action;

            if (listingWatch != null)
            {
                action = "Unwatch";
                _repo.Delete <ListingWatch>(listingWatch);
            }
            else
            {
                listingWatch = new ListingWatch
                {
                    WatcherId  = id,
                    WatchingId = listingId
                };

                action = "Watch";
                _repo.Add <ListingWatch>(listingWatch);
            }

            var message = $"{action} listing successful. [WatcherId={listingWatch.WatcherId}, WatchingID={listingWatch.WatchingId}]";

            if (await _repo.SaveAll())
            {
                return(Ok(new { action, message }));
            }


            return(BadRequest($"Failed to {action} listing"));
        }