示例#1
0
        public async Task <ActionResult> RemoveListing(int listingId)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + listingId);

            string           responseData = responseMessage.Content.ReadAsStringAsync().Result;
            ListingDetailDTO listing      = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            responseMessage = await client.GetAsync("http://localhost:58999/API/user/GetUser/" + int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value));

            responseData = responseMessage.Content.ReadAsStringAsync().Result;
            UserDTO user = JsonConvert.DeserializeObject <UserDTO>(responseData);

            ListingDTO listingToDelete = new ListingDTO();

            listingToDelete      = listing;
            listingToDelete.Id   = listingId;
            listingToDelete.User = user;

            string content = JsonConvert.SerializeObject(listingToDelete);

            Byte[]           buffer      = System.Text.Encoding.UTF8.GetBytes(content);
            ByteArrayContent byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            await client.PostAsync(apiUrl + "/RemoveListing", byteContent);

            ClaimsPrincipal prinicpal = (ClaimsPrincipal)Thread.CurrentPrincipal;
            string          userId    = prinicpal.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).Select(c => c.Value).SingleOrDefault();

            return(RedirectToAction("Detail", "User", new { id = userId }));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, ListingDetailDTO listingDetailDTO, bool follow)
        {
            if (id != listingDetailDTO.Id)

            {
                _logger.LogError($"Id did not match for to {nameof(Edit)}.  Expected:{id}, Actual{listingDetailDTO.Id}");
                return(RedirectToAction(nameof(Index)));
            }


            if (ModelState.IsValid)
            {
                try
                {
                    var mapped = _mapper.Map <Listing>(listingDetailDTO);

                    var listing = await _repositoryWrapper.Listing.GetListingByIdAsync(id);

                    string user = await _repositoryWrapper.Employee.GetUserId(User);

                    await _repositoryWrapper.Customer.ToggleFollow(user, listing, follow);



                    await _repositoryWrapper.SaveAsync();
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error updating listing: {ex}");
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(nameof(Details), listingDetailDTO));
        }
示例#3
0
        public async Task <ActionResult> EditListing(int listingId)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + listingId);

            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                string responseData = responseMessage.Content.ReadAsStringAsync().Result;

                ListingDetailDTO listing = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

                if (User.Identity.Name != listing.User.Username)
                {
                    return(View("Unauthorised"));
                }

                if (listing.Bids.Count > 0)
                {
                    ViewBag.BidAmount = (listing.Bids.Max(bid => bid.BidAmount)) + bidIncrement;
                }
                else
                {
                    ViewBag.BidAmount = (listing.Price) + bidIncrement;
                }

                responseMessage = await client.GetAsync(apiUrl + "/GetAllProductCategories");

                responseData = responseMessage.Content.ReadAsStringAsync().Result;
                List <ProductCategoryDTO> categoryList = JsonConvert.DeserializeObject <List <ProductCategoryDTO> >(responseData);
                ViewData["Categories"] = categoryList;

                return(View(listing));
            }
            return(View("Error"));
        }
示例#4
0
        public async Task <ActionResult> EditComment(int commentId, int listingId, string comment)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + listingId);

            string           responseData = responseMessage.Content.ReadAsStringAsync().Result;
            ListingDetailDTO listing      = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            responseMessage = await client.GetAsync("http://localhost:58999/API/user/GetUser/" + int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value));

            responseData = responseMessage.Content.ReadAsStringAsync().Result;
            UserDTO user = JsonConvert.DeserializeObject <UserDTO>(responseData);

            CommentDTO commentToEdit = new CommentDTO();

            commentToEdit.Id      = commentId;
            commentToEdit.Content = comment;
            commentToEdit.Listing = listing;
            commentToEdit.User    = user;

            string content = JsonConvert.SerializeObject(commentToEdit);

            Byte[]           buffer      = System.Text.Encoding.UTF8.GetBytes(content);
            ByteArrayContent byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            await client.PostAsync(apiUrl + "/EditComment", byteContent);

            return(RedirectToAction("Detail", "Listing", new { id = listingId }));
        }
示例#5
0
        public async Task <ActionResult> Detail(int?id)
        {
            if (id == null || id == 0)
            {
                return(RedirectToAction("Index", "Home"));
            }

            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + id);

            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                string responseData = responseMessage.Content.ReadAsStringAsync().Result;

                ListingDetailDTO listing = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

                if (listing.Bids.Count > 0)
                {
                    ViewBag.BidAmount = (listing.Bids.Max(bid => bid.BidAmount)) + bidIncrement;
                }
                else
                {
                    ViewBag.BidAmount = (listing.Price) + bidIncrement;
                }

                ViewBag.Listing = listing;

                return(View(listing));
            }
            return(View("Error"));
        }
示例#6
0
        public HttpResponseMessage PostListing(ListingDetailDTO listingDTO)
        {
            Listing listToAdd = mapper.CreateListingEntity(listingDTO);

            listToAdd = listingRepo.AddListing(listToAdd);
            var    response = Request.CreateResponse <ListingDetailDTO>(HttpStatusCode.Created, mapper.CreateListingDetailDTO(listToAdd));
            string uri      = Url.Link("DefaultApi", new { id = listToAdd.Id });

            response.Headers.Location = new Uri(uri);
            return(response);
        }
示例#7
0
        public async Task <ActionResult> Bid(BidDTO bidDTO)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + bidDTO.Listing.Id);

            string           responseData = responseMessage.Content.ReadAsStringAsync().Result;
            ListingDetailDTO listing      = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

            if (listing.Bids.Count > 0)
            {
                if (bidDTO.BidAmount < (listing.Bids.Max(b => b.BidAmount) + 0.50M))
                {
                    TempData["BidError"] = "true";
                    return(RedirectToAction("Detail", "Listing", new { id = bidDTO.Listing.Id }));
                }
            }
            else
            {
                if (bidDTO.BidAmount < (listing.Price + 0.50M))
                {
                    TempData["BidError"] = "true";
                    return(RedirectToAction("Detail", "Listing", new { id = bidDTO.Listing.Id }));
                }
            }

            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            responseMessage = await client.GetAsync("http://localhost:58999/API/user/GetUser/" + int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value));

            responseData = responseMessage.Content.ReadAsStringAsync().Result;
            UserDTO user = JsonConvert.DeserializeObject <UserDTO>(responseData);

            BidDTO newBid = new BidDTO();

            newBid.BidAmount = bidDTO.BidAmount;
            newBid.User      = user;
            newBid.Listing   = listing;

            string content = JsonConvert.SerializeObject(newBid);

            Byte[]           buffer      = System.Text.Encoding.UTF8.GetBytes(content);
            ByteArrayContent byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            responseMessage = await client.PostAsync(apiUrl + "/bid", byteContent);

            if (responseMessage.StatusCode == HttpStatusCode.Created)
            {
                return(RedirectToAction("Detail", "Listing", new { id = bidDTO.Listing.Id }));
            }

            return(View("Error"));
        }
示例#8
0
        public async Task <ActionResult> EditListing(ListingDetailDTO listingDetailDto, int listingId)
        {
            HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetListing/" + listingId);

            string           responseData = responseMessage.Content.ReadAsStringAsync().Result;
            ListingDetailDTO listing      = JsonConvert.DeserializeObject <ListingDetailDTO>(responseData);

            ClaimsIdentity identity = (ClaimsIdentity)User.Identity;

            responseMessage = await client.GetAsync("http://localhost:58999/API/user/GetUser/" + int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value));

            responseData = responseMessage.Content.ReadAsStringAsync().Result;
            UserDTO user = JsonConvert.DeserializeObject <UserDTO>(responseData);

            if (user.Id != listing.User.Id)
            {
                return(View("Unauthorised"));
            }

            ListingDetailDTO listingToEdit = new ListingDetailDTO();

            listingToEdit                = listing;
            listingToEdit.Price          = listingDetailDto.Price;
            listingToEdit.ItemName       = listingDetailDto.ItemName;
            listingToEdit.ImageUrl       = listingDetailDto.ImageUrl;
            listingToEdit.Description    = listingDetailDto.Description;
            listingToEdit.AuctionEndTime = listingDetailDto.AuctionEndTime;
            listingToEdit.Quantity       = listingDetailDto.Quantity;

            responseMessage = await client.GetAsync(apiUrl + "/GetAllProductCategories");

            responseData = responseMessage.Content.ReadAsStringAsync().Result;
            List <ProductCategoryDTO> categoryList = JsonConvert.DeserializeObject <List <ProductCategoryDTO> >(responseData);

            listingToEdit.ProductCategory = categoryList.Find(c => c.ProductCategoryName == listingDetailDto.ProductCategory.ProductCategoryName);
            listingToEdit.User            = user;

            string content = JsonConvert.SerializeObject(listingToEdit);

            Byte[]           buffer      = System.Text.Encoding.UTF8.GetBytes(content);
            ByteArrayContent byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            await client.PostAsync(apiUrl + "/EditListing", byteContent);

            ClaimsPrincipal prinicpal = (ClaimsPrincipal)Thread.CurrentPrincipal;
            string          userId    = prinicpal.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).Select(c => c.Value).SingleOrDefault();

            return(RedirectToAction("Detail", "User", new { id = userId }));
        }
示例#9
0
        public Listing EditListingEntity(ListingDetailDTO listingDTO)
        {
            Listing listing = new Listing();

            listing                 = listingRepo.GetListingById(listingDTO.Id);
            listing.Price           = listingDTO.Price;
            listing.ItemName        = listingDTO.ItemName;
            listing.ImageUrl        = listingDTO.ImageUrl;
            listing.ProductCategory = categoryRepo.GetCategoryById(listingDTO.ProductCategory.Id);
            listing.Description     = listingDTO.Description;
            listing.AuctionEndTime  = listingDTO.AuctionEndTime;
            listing.Quantity        = listingDTO.Quantity;

            return(listing);
        }
示例#10
0
 public Listing CreateListingEntity(ListingDetailDTO listingDTO)
 {
     return(new Listing()
     {
         Description = listingDTO.Description,
         ItemName = listingDTO.ItemName,
         Quantity = listingDTO.Quantity,
         ProductCategory = categoryRepo.GetCategoryById(listingDTO.ProductCategory.Id),
         Price = listingDTO.Price,
         ImageUrl = listingDTO.ImageUrl,
         Status = statusRepo.GetStatusById(listingDTO.Status.Id),
         Shipping = shippingRepo.GetShippingStatusById(listingDTO.Shipping.Id),
         AuctionStartTime = listingDTO.AuctionStartTime,
         AuctionEndTime = listingDTO.AuctionEndTime,
         User = userRepo.GetUserById(listingDTO.User.Id)
     });
 }
示例#11
0
        //// GET: Listing/Details/5
        //public async Task<IActionResult> Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return NotFound();
        //    }
        //    ListingDetailDTO listing = new ListingDetailDTO();
        //    try
        //    {
        //        listing = _mapper.Map<ListingDetailDTO>(await _repositoryWrapper.Listing.GetListingByIdAsync(id));

        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError($"Unable to retrieve the Listing with id-{id} : {ex}");
        //        throw new Exception($"Unable to retrieve the Listing with id-{id}: {ex}");
        //    }


        //    if (listing == null)
        //    {
        //        return NotFound();
        //    }
        //    listing.Listing = _mapper.Map<Listing>(listing);
        //    return View(listing);
        //}
        //// GET: Admin/Listing/Edit/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            ListingDetailDTO listing = new ListingDetailDTO();

            try
            {
                listing = _mapper.Map <ListingDetailDTO>(await _repositoryWrapper.Listing.GetListingByIdAsync(id));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Unable to retrieve the Listing with id-{id} : {ex}");
                throw new Exception($"Unable to retrieve the Listing with id-{id}: {ex}");
            }


            if (listing == null)
            {
                return(NotFound());
            }

            if (User.IsInRole("Customer"))
            {
                string user = await _repositoryWrapper.Employee.GetUserId(User);

                var cust = await _repositoryWrapper.Customer.GetCustomerByUserId(user);

                var props = await _repositoryWrapper.Customer.GetCustomersPropertyAsync(cust.Id);

                var r = props.PropertiesInterestedIn
                        .Where(x => x.CustomerId.Equals(cust.Id) && x.PropertyId.Equals(listing.Id)).FirstOrDefault();
                if (r == null)
                {
                    ViewData["following"] = false;
                }
                else
                {
                    ViewData["following"] = true;
                }
            }
            return(View(nameof(Details), listing));
        }
示例#12
0
        public async Task <ActionResult> NewListing(ListingDetailDTO newListing)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    HttpResponseMessage responseMessage = await client.GetAsync(apiUrl + "/GetAllProductCategories");

                    if (responseMessage.StatusCode == HttpStatusCode.OK)
                    {
                        string responseData = responseMessage.Content.ReadAsStringAsync().Result;

                        List <ProductCategoryDTO> categoryList = JsonConvert.DeserializeObject <List <ProductCategoryDTO> >(responseData);

                        ViewData["Categories"] = categoryList;

                        return(View(newListing));
                    }

                    return(View("Error"));
                }
                else
                {
                    ClaimsIdentity      identity        = (ClaimsIdentity)User.Identity;
                    HttpResponseMessage responseMessage = await client.GetAsync("http://localhost:58999/API/user/GetUser/" + int.Parse(identity.FindFirst(ClaimTypes.NameIdentifier).Value));

                    string  responseData = responseMessage.Content.ReadAsStringAsync().Result;
                    UserDTO user         = JsonConvert.DeserializeObject <UserDTO>(responseData);

                    newListing.User             = user;
                    newListing.AuctionStartTime = System.DateTime.Now;
                    newListing.Status           = new StatusDTO()
                    {
                        Id = 1
                    };
                    newListing.Shipping = new ShippingDTO()
                    {
                        Id = 1
                    };

                    responseMessage = await client.GetAsync(apiUrl + "/GetAllProductCategories");

                    responseData = responseMessage.Content.ReadAsStringAsync().Result;
                    List <ProductCategoryDTO> categoryList = JsonConvert.DeserializeObject <List <ProductCategoryDTO> >(responseData);

                    newListing.ProductCategory = categoryList.Find(c => c.ProductCategoryName == newListing.ProductCategory.ProductCategoryName);

                    responseMessage = await client.PostAsync(apiUrl + "/PostListing", new StringContent(
                                                                 new JavaScriptSerializer().Serialize(newListing), Encoding.UTF8, "application/json"));

                    if (responseMessage.IsSuccessStatusCode)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(View("Error"));
                    }
                }
            }
            catch
            {
                return(View());
            }
        }