Beispiel #1
0
        public async Task <IActionResult> WonAuctions()
        {
            MyAuctionsModel model        = new MyAuctionsModel();
            User            loggedInUser = await this.userManager.GetUserAsync(base.User);

            List <Auction> auctions = this.context.auction.Where(a => a.winner == loggedInUser.Id).ToList();

            List <string> images = new List <string>();

            foreach (Auction auction in auctions)
            {
                string image = Convert.ToBase64String(auction.image);
                images.Add(image);
            }

            model.myAuctions = auctions;
            model.images     = images;



            return(View(model));
        }
Beispiel #2
0
        // GET ALL MY AUCTIONS

        public async Task <IActionResult> MyAuctions()
        {
            User loggedInUser = await this.userManager.GetUserAsync(base.User);

            IList <Auction> auctions = await this.context.auction.
                                       Where(item => item.userId == loggedInUser.Id).ToListAsync();

            IList <string> images = new List <string>();

            foreach (Auction auction in auctions)
            {
                string image = Convert.ToBase64String(auction.image);
                images.Add(image);
            }

            MyAuctionsModel model = new MyAuctionsModel()
            {
                myAuctions = auctions,
                images     = images
            };

            return(View(model));
        }
Beispiel #3
0
        public IActionResult SearchAuctions(string keyWord, string minPrice, string maxPrice, string state, string page)
        {
            if (minPrice == null)
            {
                minPrice = "0";
            }
            if (maxPrice == null)
            {
                maxPrice = "999999999999";
            }

            var auctionsQuery = this.context.auction
                                .Where(item => item.startPrice > float.Parse(minPrice))
                                .Where(item => item.startPrice < float.Parse(maxPrice));

            if (keyWord != null)
            {
                auctionsQuery = auctionsQuery.Where(item => item.name.Contains(keyWord));
            }

            State?stateType = null;

            if (state != "ALL")
            {
                switch (state)
                {
                case "DRAFT":
                    stateType = State.DRAFT;
                    break;

                case "OPEN":
                    stateType = State.OPEN;
                    break;

                case "READY":
                    stateType = State.READY;
                    break;

                case "EXPIRED":
                    stateType = State.EXPIRED;
                    break;

                case "SOLD":
                    stateType = State.SOLD;
                    break;
                }
                auctionsQuery = auctionsQuery.Where(item => item.state == stateType);
            }
            int pageNumber = 1;

            if (page != "undefined")
            {
                pageNumber = int.Parse(page);
            }
            int pageSize = 12;

            auctionsQuery = auctionsQuery.OrderBy(x => x.creationDate);
            auctionsQuery = auctionsQuery.Skip((pageNumber - 1) * pageSize);

            List <Auction> auctions = auctionsQuery.Take(pageSize).ToList();

            List <string>  images           = new List <string>();
            List <string>  timers           = new List <string>();
            List <string>  lastBidders      = new List <string>();
            List <Auction> modifideAuctions = new List <Auction>();

            foreach (Auction auction in auctions)
            {
                if (auction.openingDate <= DateTime.Now.AddSeconds(1) && auction.state == State.READY)
                {
                    auction.state = State.OPEN;
                    this.context.auction.Update(auction);
                    if (stateType != null && auction.state != stateType)
                    {
                        continue;
                    }
                }
                Bid lastBid = this.context.bid.Where(bid => bid.auctionId == auction.id).Include(bid => bid.User).OrderByDescending(bid => bid.timestamp).FirstOrDefault();
                if (auction.closingDate <= DateTime.Now.AddSeconds(1) && auction.state == State.OPEN)
                {
                    // MAKE IT SOLD OR EXPIRED DEPENDING ON THE BIDDING
                    if (lastBid == null)
                    {
                        auction.state = State.EXPIRED;
                    }
                    else
                    {
                        auction.state  = State.SOLD;
                        auction.winner = lastBid.UserId;
                    }
                    this.context.auction.Update(auction);
                    if (stateType != null && auction.state != stateType)
                    {
                        continue;
                    }
                }

                modifideAuctions.Add(auction);

                if (lastBid == null)
                {
                    lastBidders.Add(null);
                }
                else
                {
                    lastBidders.Add(lastBid.User.UserName);
                }

                string image = Convert.ToBase64String(auction.image);
                images.Add(image);

                if (auction.state == State.OPEN)
                {
                    TimeSpan difference = auction.closingDate - DateTime.Now;
                    string   strDiff    = String.Format("{0}:{1}:{2}",
                                                        difference.Days * 24 + difference.Hours,
                                                        difference.Minutes,
                                                        difference.Seconds);

                    timers.Add(strDiff);
                }
                else
                {
                    timers.Add(null);
                }
            }



            this.context.SaveChanges();

            var model = new MyAuctionsModel();

            model.myAuctions  = modifideAuctions;
            model.images      = images;
            model.pageNumber  = pageNumber.ToString();
            model.timers      = timers;
            model.lastBidders = lastBidders;
            return(PartialView(model));
        }