Exemplo n.º 1
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"));
        }