Ejemplo n.º 1
0
        public ActionResult Login(string returnUrl)
        {
            string userIP     = UtilityClass.GetIP();
            string postalcode = UtilityClass.GetZip(userIP);
            JArray items;
            string keyword = "lamp";
            int    page;

            for (page = 1; page < 5; page++)
            {
                HttpWebRequest WR = WebRequest.CreateHttp($"http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords" +
                                                          $"&SERVICE-VERSION=1.0.0&GLOBAL-ID=EBAY-US&SECURITY-APPNAME=shaunitt-studenta-PRD-b7141958a-1d5c6f0c&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD=TRUE" +
                                                          $"&keywords={keyword}&buyerPostalCode={postalcode}&itemFilter.name=MaxDistance&itemFilter.value=40" +
                                                          $"&paginationInput.entriesPerPage=100&paginationInput.pageNumber={page}");

                HttpWebResponse Response;
                try
                {
                    Response = (HttpWebResponse)WR.GetResponse();
                }
                catch (WebException e)
                {
                    ViewBag.Error            = "Exception";
                    ViewBag.ErrorDescription = e.Message;
                    return(View());
                }

                //reads response
                StreamReader reader     = new StreamReader(Response.GetResponseStream());
                string       searchData = reader.ReadToEnd();
                //parses JSON
                JObject JsonData = JObject.Parse(searchData);

                //J array for items returned
                items = (JArray)JsonData["findItemsByKeywordsResponse"][0]["searchResult"][0]["item"];


                //instantiate new ebaylisting object
                Listing listing = new Listing();

                //instantiate new lamp entities database
                LampBaeEntities1 db = new LampBaeEntities1();

                List <Listing> LampDBList = new List <Listing>();

                LampDBList = (from p in db.Listings
                              where p.ID != 0
                              select p).ToList();

                for (int i = 0; i < items.Count; i++)
                {
                    if (LampDBList.Exists(x => x.EbayItemNumber != null && x.EbayItemNumber.ToString() == (string)items[i]["itemId"][0]))
                    {
                        //dupes detected
                    }
                    else
                    {
                        listing.ItemSearchURL  = (string)items[i]["viewItemURL"][0];
                        listing.Title          = (string)items[i]["title"][0];
                        listing.Image          = (string)items[i]["galleryURL"][0];
                        listing.PostalCode     = (string)items[i]["postalCode"][0];
                        listing.EbayItemNumber = (string)items[i]["itemId"][0];
                        listing.EndDate        = (DateTime)items[i]["listingInfo"][0]["endTime"][0];
                        listing.Price          = (decimal)items[i]["sellingStatus"][0]["currentPrice"][0]["__value__"];
                        new Listing()
                        {
                            EbayItemNumber = listing.EbayItemNumber, ItemSearchURL = listing.ItemSearchURL, Title = listing.Title, PostalCode = listing.PostalCode, Image = listing.Image
                        };
                        db.Listings.Add(listing);
                        db.SaveChanges();
                    }
                }
            }
            ViewBag.ReturnUrl = returnUrl;
            return(View());
        }
Ejemplo n.º 2
0
        public ActionResult Lamps(int?lampid)
        {
            ViewBag.Title = "Lamps";

            string userIP  = UtilityClass.GetIP();
            string userZip = UtilityClass.GetZip(userIP);

            //instantiate DB from model
            LampBaeEntities1 db = new LampBaeEntities1();

            //instantiate new list for lamp id's
            List <Listing> LampDBList = new List <Listing>();

            //populate list from Listing
            LampDBList = (from p in db.Listings
                          where p.ID != 0 ||
                          p.EndDate > DateTime.Now || //doesnt show lamps after its ended auction
                          p.ReportCount > 0    //doesn't show lamps that have been reported/flagged
                          select p).ToList();

            //instantiate new list for ratings
            List <ViewCount> ViewCountList = (from p in db.ViewCounts
                                              where p.ViewID != 0
                                              select p).ToList();

            //declaring our listing object
            Listing listing     = null;
            string  ItemZipCode = "";

            // if no lampid is provided, a random id will be generated and assigned to x
            if (lampid == null || lampid > LampDBList.Count || lampid < 0)
            {
                //instantiate new random object & create a new random int based on range of list count as max value
                Random r = new Random();
                lampid  = r.Next(1, LampDBList.Count());
                listing = (from p in db.Listings
                           where p.ID == lampid
                           select p).Single();
                ItemZipCode = listing.PostalCode;
            }

            else
            {
                //grabs a listing from list
                listing = (from p in db.Listings
                           where p.ID == lampid
                           select p).Single();
                ItemZipCode = listing.PostalCode;
            }

            //handling for ebay listings vs user listings (the image url structure is different)
            if (listing.EbayItemNumber == null)
            {
                ViewData["ItemTitle"]   = listing.Title;
                ViewData["Price"]       = listing.Price;
                ViewData["viewItemURL"] = ("/Home/LinkImage?lampid=" + listing.ID);
                ViewData["ImageURL"]    = Url.Content("~/Content/" + listing.Image);
            }
            else
            {
                ViewData["ItemTitle"]   = listing.Title;
                ViewData["Price"]       = listing.Price;
                ViewData["viewItemURL"] = listing.ItemSearchURL;
                ViewData["ImageURL"]    = listing.Image;
            }

            //test if view record exists already
            ViewCount existingViewCount = null;

            try
            {
                existingViewCount = (from m in db.ViewCounts
                                     where m.ItemID == lampid &&
                                     m.UserID == User.Identity.Name
                                     select m).Single();
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: '{0}'", e);
            }

            // if a view record already exists, simply increment
            if (existingViewCount != null)
            {
                existingViewCount.ViewCount1 = existingViewCount.ViewCount1 + 1;
                db.SaveChanges();
            }

            //if a view record doesn't already exist, create a new record and assign it a value of one
            else
            {
                ViewCount newviewcount = new ViewCount();
                newviewcount.UserID     = User.Identity.Name;
                newviewcount.ItemID     = (int)lampid;
                newviewcount.ViewCount1 = 1;
                newviewcount.ViewDate   = DateTime.Now;
                new ViewCount()
                {
                    UserID = newviewcount.UserID, ItemID = (int)lampid, ViewCount1 = 1, ViewDate = DateTime.Now
                };
                db.ViewCounts.Add(newviewcount);
                db.SaveChanges();
            }


            string distance = UtilityClass.GetZipCodeDistance(userZip, ItemZipCode);

            ViewBag.Distance          = distance;
            ViewData["CurrentLampID"] = listing.ID;
            ViewBag.UserZip           = userZip;
            ViewBag.CurrentUser       = User.Identity.Name;
            ViewBag.CurrentUserIP     = userIP;
            return(View());
        }