Пример #1
0
        private async Task <Response <BidsDTO> > GetDefaultHomePageBids(int page)
        {
            List <BidDTO> bids = await _context.Bids
                                 .OrderByDescending(bid => bid.UnitsCounter)
                                 .OrderByDescending(bid => bid.PotenialSuplliersCounter)
                                 .OrderBy(bid => bid.ExpirationDate)
                                 .Skip(page * _pageDefaultSize)
                                 .Take(_pageDefaultSize)
                                 .Include(bidEntity => bidEntity.Product)
                                 .Select(bidEntitiy => _mapper.Map <BidDTO>(bidEntitiy))
                                 .ToListAsync().ConfigureAwait(false);


            int numberOfBids = await _context.Bids.CountAsync().ConfigureAwait(false);


            BidsDTO bidsDTO = new BidsDTO(
                pageNumber: page,
                numberOfPages: (int)Math.Ceiling((double)numberOfBids / _pageDefaultSize),
                bidsPage: bids);

            return(new Response <BidsDTO>()
            {
                DTOObject = bidsDTO, IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
            });
        }
Пример #2
0
        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()
            });
        }
Пример #3
0
        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()
            });
        }
Пример #4
0
        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()
            });
        }
Пример #5
0
        public async Task <Response <BidsDTO> > GetBids(BidsQueryOptions bidsFilters)
        {
            // firts category and sub category
            // then
            string bidsSearchString = bidsFilters.Search;

            if (bidsFilters.Category == null && bidsSearchString == null)
            {
                return(await this.GetDefaultHomePageBids(bidsFilters.Page));
            }
            else if (!ValidateBidsFilters(bidsFilters, out string validationErrorString))
            {
                return(new Response <BidsDTO>()
                {
                    DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = validationErrorString
                });
            }
            else // TODO : extract to sub methods
            {
                IEnumerable <BidEntity> filteredBids = this.GetFilteredBids(bidsFilters);

                IEnumerable <BidEntity> sortedBids = this.GetSortBids(filteredBids, bidsFilters.SortOrder, bidsFilters.SortBy);

                // return page
                int pageSize = bidsFilters.Limit == 0 || bidsFilters.Limit > _maxPageSize ? _pageDefaultSize : bidsFilters.Limit;

                var bidsPage = sortedBids
                               .Skip(bidsFilters.Page * pageSize)
                               .Take(pageSize)
                               .Select(bidEntity => _mapper.Map <BidDTO>(bidEntity))
                               .ToList();

                BidsDTO bidsDTO = new BidsDTO(
                    pageNumber: bidsFilters.Page,
                    numberOfPages: (int)Math.Ceiling((double)filteredBids.Count() / pageSize),
                    bidsPage: bidsPage);

                return(new Response <BidsDTO>()
                {
                    DTOObject = bidsDTO, IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage()
                });
            }
        }
Пример #6
0
        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()
            });
        }