public async Task <Response> AddSupplierProposal(SupplierProposalRequest supplierProposalRequest) { BidEntity bid = await _context.Bids.Where(b => b.Id == supplierProposalRequest.BidId).Include(b => b.CurrentProposals).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } // TODO? validate supplier SupplierProposalEntity new_proposal_ent = _mapper.Map <SupplierProposalEntity>(supplierProposalRequest); bid.CurrentProposals.Add(new_proposal_ent); bid.PotenialSuplliersCounter += 1; try { _context.Bids.Update(bid); await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ex.Message }); } return(new Response() { IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); }
public async Task <Response> GetProposalWithMaxVotes(string bidId) { BidEntity bid = await _context.Bids.Where(bid => bid.Id == bidId).Include(bid => bid.CurrentProposals).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } SupplierProposalEntity chosenProposalEntity = bid.CurrentProposals.Where(proposal => proposal.MinimumUnits <= bid.UnitsCounter && proposal.ProposedPrice <= bid.MaxPrice).Aggregate( (currWinner, x) => (currWinner == null || x.Votes > currWinner.Votes ? x : currWinner)); bid.ChosenProposal = chosenProposalEntity; bid.PotenialSuplliersCounter = 1; try { _context.Bids.Update(bid); await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ex.Message }); } return(new Response() { IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); }
public async Task <Response> DeleteSupplierProposal(string bidId, string supplierId) { BidEntity bid = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.CurrentProposals).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } SupplierProposalEntity proposal = bid.CurrentProposals.Find(p => p.SupplierId == supplierId); if (proposal == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ProposalNotFoundFailString }); } bid.CurrentProposals.Remove(proposal); bid.PotenialSuplliersCounter--; _context.Set <SupplierProposalEntity>().Remove(proposal); try { _context.Bids.Update(bid); await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ex.Message }); } return(new Response() { IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); }
public async Task <Response> VoteForSupplier(VotingRequest votingRequest) { BidEntity bid = await _context.Bids.Where(b => b.Id == votingRequest.BidId).Include(b => b.CurrentProposals).Include(b => b.CurrentParticipancies).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } ParticipancyEntity participancy = bid.CurrentParticipancies.Where(p => p.BuyerId == votingRequest.BuyerId).FirstOrDefault(); if (participancy == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = $"Buyer {votingRequest.BuyerId} is not part of the bids buyers." }); } else if (participancy.HasVoted) { // TODO enable change after vote return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = $"Buyer {votingRequest.BuyerId} has already voted" }); } SupplierProposalEntity proposal = bid.CurrentProposals.Where(p => p.SupplierId == votingRequest.VotedSupplierId).FirstOrDefault(); if (proposal == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = $"no proposal for supplier: {votingRequest.VotedSupplierId} has found for the bid." }); } participancy.HasVoted = true; proposal.Votes += 1; try { _context.Set <ParticipancyEntity>().Update(participancy); _context.Set <SupplierProposalEntity>().Update(proposal); await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ex.Message }); } return(new Response() { IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); }