Exemplo n.º 1
0
        //Buy a pledge from single project view
        public Result <BackedPledges> AddPledge(int pledgeId, int projectId)
        {
            if (pledgeId == 0)
            {
                return(Result <BackedPledges> .CreateFailed(
                           StatusCode.BadRequest, "Null pledgeId"));
            }

            if (projectId == 0)
            {
                return(Result <BackedPledges> .CreateFailed(
                           StatusCode.BadRequest, "Null projectId"));
            }

            var project = projectservices.FindProjectById(projectId);
            var pledge  = _db
                          .Set <Pledge>()
                          .Where(i => i.PledgeId == pledgeId)
                          .SingleOrDefault();
            string userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);

            var backedPledge = new BackedPledges()
            {
                UserId   = userId,
                PledgeId = pledgeId
            };

            project.ProjectCurrentAmount += pledge.PledgePrice;
            project.ProjectProgress       = project.ProjectCurrentAmount / project.ProjectTargetAmount;
            if (project.ProjectCurrentAmount >= project.ProjectTargetAmount)
            {
                project.IsComplete = true;
            }

            _db.Add(backedPledge);
            _db.Update(project);


            var rows = 0;

            try
            {
                rows = _db.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <BackedPledges> .CreateFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <BackedPledges> .CreateFailed(
                           StatusCode.InternalServerError,
                           "Pledge could not be updated"));
            }

            return(Result <BackedPledges> .CreateSuccessful(backedPledge));
        }
Exemplo n.º 2
0
        public Result <Fund> AddFund(FundOptions fundOptions)
        {
            if (fundOptions == null)
            {
                return(Result <Fund> .CreateFailed(
                           StatusCode.BadRequest, "Null options"));
            }

            string userId  = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var    project = projectServices.FindProjectById(fundOptions.ProjectId);
            Fund   fund    = new Fund
            {
                UserId     = userId,
                Project    = project,
                FundAmount = fundOptions.FundAmount
            };

            project.ProjectCurrentAmount += fund.FundAmount;
            project.ProjectProgress       = project.ProjectCurrentAmount / project.ProjectTargetAmount;
            if (project.ProjectCurrentAmount >= project.ProjectTargetAmount)
            {
                project.IsComplete = true;
            }

            _db.Add(fund);
            _db.Update(project);

            var rows = 0;

            try
            {
                rows = _db.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Fund> .CreateFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Fund> .CreateFailed(
                           StatusCode.InternalServerError,
                           "Fund could not be updated"));
            }

            return(Result <Fund> .CreateSuccessful(fund));
        }