private ChallengeStatusVote DbVoteToVote(ChallengeStatusVoteDb d)
        {
            ChallengeStatusVote v = new ChallengeStatusVote();

            v.Accepted = d.Accepted;
            v.ChallengeID = d.ChallengeID;
            v.ChallengeStatusUniqueID = d.ChallengeStatusUniqueID;
            v.UniqueID = d.UniqueID;

            return v;
        }
        private ChallengeStatusVote DbVoteToVote(ChallengeStatusVoteDb d)
        {
            ChallengeStatusVote v = new ChallengeStatusVote();

            v.Accepted = d.Accepted;
            v.ChallengeID = d.ChallengeID;
            v.CustomerID = d.CustomerID;
            v.BidderCustomerID = d.BidderCustomerID;

            return v;
        }
 public void Add(ChallengeStatusVote vote)
 {
     ChallengeStatusVoteDb d = VoteToDbVote(vote);
     context.AddObject(TableName, d);
     try
     {
         context.SaveChangesWithRetries();
     }
     catch (Exception e)
     {
         System.Diagnostics.Trace.WriteLine("Status repo vote exception: Couldn't add vote " + e.ToString());
     }
 }
        private ChallengeStatusVoteDb VoteToDbVote(ChallengeStatusVote v)
        {
            ChallengeStatusVoteDb d = new ChallengeStatusVoteDb();

            d.Accepted = v.Accepted;
            d.ChallengeID = v.ChallengeID;
            d.ChallengeStatusUniqueID = v.ChallengeStatusUniqueID;
            d.UniqueID = v.UniqueID;

            d.PartitionKey = v.ChallengeID + "_" + v.ChallengeStatusUniqueID;
            d.RowKey = v.UniqueID;

            return d;
        }
        public void AcceptClaim(ChallengeStatus status)
        {
            ChallengeStatus s = StatusRepo.Get(status.CustomerID, status.ChallengeID);

            ChallengeBid bid = BidRepo.CustomerDidBidOnChallenge(((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID, s.ChallengeID);
            if (bid==null)
                throw new HttpResponseException("You can't accept a claim on a challenge you don't have a stake in.", System.Net.HttpStatusCode.Forbidden);

            ChallengeStatusVote vote = new ChallengeStatusVote();
            vote.ChallengeID = s.ChallengeID;
            vote.CustomerID = s.CustomerID;
            vote.BidderCustomerID = ((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID;
            vote.Accepted = true;

            VoteRepo.Add(vote);

            int yesVotes = VoteRepo.GetYesVotes(s);
            if (yesVotes > (BidRepo.GetBidCountForChallenge(status.ChallengeID) * 0.33))
            {
                Challenge chal = ChalRepo.Get(s.ChallengeID);
                chal.State = (int)Challenge.ChallengeState.Completed;
                chal.TargetCustomerID = status.CustomerID;
                ChalRepo.Update(chal);

                BidRepo.UpdateStatusForBidsOnChallenge(s.ChallengeID, ChallengeBid.BidStatusCodes.Accepted);

                s.Status = (int)ChallengeStatus.StatusCodes.Completed;
                StatusRepo.Update(s);

                // challenge completed! award the money! DO IT DO IT!

                // queue the billing system to process this challenge status
                Dictionary<string, long> billingQueueItemData=new Dictionary<string,long>();
                billingQueueItemData.Add("ChalID", s.ChallengeID);
                billingQueueItemData.Add("CustID", s.CustomerID);
                RepoFactory.GetProcessingQueue().PutQueueMessage(ProcessingQueue.MessageType.Billing, billingQueueItemData);

                // notify the customer
                CustomerNotifier.NotifyChallengeAwardedToYou(s.CustomerID, s.ChallengeID);
            }
            else
            {
                // keep waitin'.

            }
        }
        private ChallengeStatusVoteDb VoteToDbVote(ChallengeStatusVote v)
        {
            ChallengeStatusVoteDb d = new ChallengeStatusVoteDb();

            d.Accepted = v.Accepted;
            d.ChallengeID = v.ChallengeID;
            d.CustomerID = v.CustomerID;
            d.BidderCustomerID = v.BidderCustomerID;

            d.PartitionKey = DbPartitionForTaker(d.ChallengeID, d.CustomerID);
            d.RowKey = DbRowForBidder(d.BidderCustomerID);

            return d;
        }
        public void RejectClaim(ChallengeStatus status)
        {
            ChallengeStatus s = StatusRepo.Get(status.CustomerID, status.ChallengeID);

            ChallengeBid bid=BidRepo.CustomerDidBidOnChallenge(((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID, s.ChallengeID);
            if (bid==null)
                throw new HttpResponseException("You can't deny a claim on a challenge you don't have a stake in.", System.Net.HttpStatusCode.Forbidden);

            // add the "no" vote
            ChallengeStatusVote vote = new ChallengeStatusVote();
            vote.ChallengeID = s.ChallengeID;
            vote.CustomerID = s.CustomerID;
            vote.BidderCustomerID = ((DareyaIdentity)HttpContext.Current.User.Identity).CustomerID;
            vote.Accepted = false;

            VoteRepo.Add(vote);

            int noVotes=VoteRepo.GetNoVotes(s);
            if(noVotes > (BidRepo.GetBidCountForChallenge(status.ChallengeID)*0.66))
            {
                s.Status = (int)ChallengeStatus.StatusCodes.SourceRejected;
                StatusRepo.Update(s);

                // get the next homey who's working on this, if any.
                ChallengeStatus nextStatus = StatusRepo.GetNextVotePendingStatusForChallenge(s.ChallengeID);
                if (nextStatus != null)
                {
                    BidRepo.UpdateVotePendingCustomerIDForChallenge(s.ChallengeID, nextStatus.CustomerID);
                }
                else
                {
                    // reopen the bidding! NO WINNER NO WINNER

                }

                // you've failed this challenge my friend.
                //CustomerNotifier.NotifyChallengeRejected(s.CustomerID, s.ChallengeID);
            }
            else
            {

            }
        }
 public void Add(ChallengeStatusVote vote)
 {
     ChallengeStatusVoteDb d = VoteToDbVote(vote);
     context.AttachTo(TableName, d);
     context.SaveChangesWithRetries();
 }