public async Task <Response <BidsDTO> > GetBuyerBids(string buyerId, BidsTime timeFilter)
        {
            var buyer = await _context.Buyers
                        .Where(buyer => buyer.Id == buyerId)
                        .Include(buyer => buyer.CurrentParticipancies)
                        .ThenInclude(participancy => participancy.Bid)
                        .ThenInclude(bid => bid.Product)
                        .FirstOrDefaultAsync().ConfigureAwait(false);

            if (buyer == null)
            {
                return(new Response <BidsDTO>()
                {
                    DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BuyerNotFoundFailString
                });
            }

            List <BidDTO> liveBids = buyer
                                     .CurrentParticipancies
                                     .Select(p => p.Bid)
                                     .Where(bid => FilterBuyerBids(bid, timeFilter))
                                     .Select(bid => _mapper.Map <BidDTO>(bid))
                                     .ToList();

            return(new Response <BidsDTO>()
            {
                DTOObject = BidsDTO.CreateDefaultBidsPage(liveBids), IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
            });
        }
        private static bool FilterBuyerBids(BidEntity buyerBid, BidsTime timeFiler)
        {
            DateTime currentTime = DateTime.Now;

            switch (timeFiler)
            {
            case BidsTime.Old:
                return(buyerBid.ExpirationDate <= currentTime);

            case BidsTime.Live:
                return(buyerBid.ExpirationDate > currentTime);

            default:     //null
                return(true);
            }
        }
        private static bool FilterBuyerBids(BidEntity buyerBid, BidsTime timeFilter)
        {
            HashSet <BidPhase> livePhases = new HashSet <BidPhase>()
            {
                BidPhase.Join, BidPhase.Vote, BidPhase.Payment
            };

            switch (timeFilter)
            {
            case BidsTime.Old:
                return(!livePhases.Contains(buyerBid.Phase));

            case BidsTime.Live:
                return(livePhases.Contains(buyerBid.Phase));

            default:     //null
                return(true);
            }
        }
        public async Task <Response <BidsDTO> > GetBidsCreatedByBuyer(string buyerId, BidsTime timeFiler)
        {
            //validate existence
            var buyer = await _context.Buyers.Where(b => b.Id == buyerId).Include(b => b.CurrentParticipancies).ThenInclude(p => p.Bid).FirstOrDefaultAsync().ConfigureAwait(false);

            if (buyer == null)
            {
                return(new Response <BidsDTO>()
                {
                    DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BuyerNotFoundFailString
                });
            }

            var ownedBids = _context.Bids.Where(b => b.OwnerId == buyerId).Select(bid => _mapper.Map <BidDTO>(bid)).ToList();

            return(new Response <BidsDTO>()
            {
                DTOObject = BidsDTO.CreateDefaultBidsPage(ownedBids), IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
            });
        }
        public async Task <Response <BidsDTO> > GetBidsCreatedByBuyer(string buyerId, BidsTime timeFilter)
        {
            //validate existence
            var buyer = await _context
                        .Buyers
                        .FindAsync(buyerId)
                        .ConfigureAwait(false);

            if (buyer == null)
            {
                return(new Response <BidsDTO>()
                {
                    DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BuyerNotFoundFailString
                });
            }

            List <BidEntity> ownedBidsEntities = await _context.Bids
                                                 .Where(b => b.OwnerId == buyerId)
                                                 .Include(bid => bid.Product)
                                                 .ToListAsync()
                                                 .ConfigureAwait(false);

            List <BidDTO> ownedBidsDTO = ownedBidsEntities
                                         .Where(bid => FilterBuyerBids(bid, timeFilter))
                                         .Select(bid => _mapper.Map <BidDTO>(bid))
                                         .ToList();

            return(new Response <BidsDTO>()
            {
                DTOObject = BidsDTO.CreateDefaultBidsPage(ownedBidsDTO), IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
            });
        }
        public async Task <Response <BidsDTO> > GetBidsWhereChosen(string supplierId, BidsTime timeFilter)
        {
            //validate existence
            var supplier = await _context.Suppliers
                           .Where(supplier => supplier.Id == supplierId)
                           .Include(supplier => supplier.CurrentProposals)
                           .ThenInclude(p => p.Bid)
                           .ThenInclude(bid => bid.Product)
                           .Include(supplier => supplier.CurrentProposals)
                           .ThenInclude(proposal => proposal.Bid)
                           .ThenInclude(b => b.ChosenProposal)
                           .FirstOrDefaultAsync()
                           .ConfigureAwait(false);

            if (supplier == null)
            {
                return(new Response <BidsDTO>()
                {
                    DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = SupplierNotFoundFailString
                });
            }

            var wonBids = supplier.CurrentProposals
                          .Where(p => p.Bid.ChosenProposal?.SupplierId == supplierId)
                          .Select(p => p.Bid).Where(bid => FilterBuyerBids(bid, timeFilter))
                          .Select(bid => _mapper.Map <BidDTO>(bid))
                          .ToList();

            return(new Response <BidsDTO>()
            {
                DTOObject = BidsDTO.CreateDefaultBidsPage(wonBids), IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
            });
        }