public async Task <Response> DeleteBuyer(string bidId, string buyerId) { BidEntity bid = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.CurrentParticipancies).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } ParticipancyEntity participancy = bid.CurrentParticipancies.Find(p => p.BuyerId == buyerId); if (participancy == null) { return(new Response() { IsOperationSucceeded = false, SuccessOrFailureMessage = ParticipantNotFoundFailString }); } bid.UnitsCounter -= participancy.NumOfUnits; bid.CurrentParticipancies.Remove(participancy); _context.Set <ParticipancyEntity>().Remove(participancy); 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() }); }
public async Task <Response <BidDTO> > GetBid(string bidId, string userId, string userRole) { BidEntity bid; BidDTO bidDTO; if (userRole?.Equals("anonymous", StringComparison.OrdinalIgnoreCase) ?? true) { bid = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.Product).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response <BidDTO>() { DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } bidDTO = _mapper.Map <BidDTO>(bid); return(new Response <BidDTO>() { DTOObject = bidDTO, IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); } else if (userRole.Equals("Consumer", StringComparison.OrdinalIgnoreCase)) { bid = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.Product).Include(b => b.CurrentParticipancies).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response <BidDTO>() { DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } bidDTO = _mapper.Map <BidDTO>(bid); ParticipancyEntity participancy = bid.CurrentParticipancies.Where(p => p.BuyerId == userId).FirstOrDefault(); bidDTO.IsUserInBid = participancy != null; bidDTO.HasVoted = participancy?.HasVoted ?? false; bidDTO.NumOfUnitsParticipant = participancy?.NumOfUnits ?? 0; return(new Response <BidDTO>() { DTOObject = bidDTO, IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); } else if (userRole.Equals("Supplier", StringComparison.OrdinalIgnoreCase)) { bid = await _context.Bids.Where(b => b.Id == bidId).Include(b => b.Product).Include(b => b.CurrentProposals).Include(b => b.ChosenProposal).FirstOrDefaultAsync().ConfigureAwait(false); if (bid == null) { return(new Response <BidDTO>() { DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = BidNotFoundFailString }); } bidDTO = _mapper.Map <BidDTO>(bid); bidDTO.IsUserInBid = bid.ChosenProposal != null ? bid.ChosenProposal.SupplierId == userId : bid.CurrentProposals.Any(proposal => proposal.SupplierId == userId); return(new Response <BidDTO>() { DTOObject = bidDTO, IsOperationSucceeded = true, SuccessOrFailureMessage = this.getSuccessMessage() }); } return(new Response <BidDTO>() { DTOObject = null, IsOperationSucceeded = false, SuccessOrFailureMessage = $"Bad input - unexpected user role: {userRole}" }); }