public HttpResponseMessage CreateFundingForProject(UserFundingModel funding, int projectId)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var identity = User.Identity as ClaimsIdentity;

            using (var s = new UserFundingRepository())
            {
                //STEP 1 - Create the Project Funding from UserFundingModel coming from the client
                long newFundingId = s.Insert(funding, projectId, identity);

                //STEP 2 - Update Project Stats Screen Amount + NoOfBackers
                using (var sr = new ProjectStatRepository())
                {
                    bool statAmountUpdated      = sr.IncrementProjectStatMoneyPledged(projectId, funding.AmountPaid);
                    bool statNoOfBackersUpdated = sr.IncrementProjectStatBackersNo(projectId);

                    if (!statAmountUpdated || !statNoOfBackersUpdated)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.Created, newFundingId));
            }
        }
        public HttpResponseMessage CreateProject(ProjectModel project)
        {
            //STEP 1 - Create the Project from Project Model coming from the client
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var identity = User.Identity as ClaimsIdentity;

            using (var s = new ProjectRepository())
            {
                //STEP 1 - Create new Attachment Set and assign it to the model coming from the client
                using (var attRepo = new AttachmentSetRepository())
                {
                    long attachmentSetId = attRepo.CreateAttachmentSet();
                    project.AttachmentSetId = attachmentSetId;
                }

                //STEP 2 - Create the Project and save to Projects table
                long newProjectId = s.Insert(project, identity);

                //STEP 3 - Create Project Stats Screen and Save to ProjectStats Table
                using (var sr = new ProjectStatRepository())
                {
                    bool statCreated = sr.CreateProjectStat((int)newProjectId);
                    if (!statCreated)
                    {
                        return(Request.CreateResponse(HttpStatusCode.NotFound));
                    }
                }

                //STEP 4 - Create new Project Funding Package for Donations
                using (var fpRepo = new FundingPackageRepository())
                {
                    FundingPackageModel newFundingPackageModel = new FundingPackageModel();
                    newFundingPackageModel.AttachmentSetId = null;
                    newFundingPackageModel.Title           = "Donations Funding Package";
                    newFundingPackageModel.Description     = "Feel free to donate whatever amount you wish!";

                    fpRepo.CreateFundingPackage(newFundingPackageModel, identity, (int)newProjectId, true);
                }

                return(Request.CreateResponse(HttpStatusCode.Created, newProjectId));
            }
        }
        public HttpResponseMessage CreateProjectExternalShare(ProjectExternalShareModel projectExternalShare, int projectId)
        {
            if (!ModelState.IsValid || projectId <= 0)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var identity = User.Identity as ClaimsIdentity;

            //STEP 1 - Update the project statistic for external share
            using (var ps = new ProjectStatRepository())
            {
                bool hasUpdatedProjectStat = ps.IncrementProjectStatSharesNo(projectId);

                //project to update stat not found
                if (!hasUpdatedProjectStat)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }

            using (var s = new ProjectExternalShareRepository())
            {
                var httpStatusCode = HttpStatusCode.Created;

                // STEP 2: Create new external share and save it to database table of external shares
                bool hasInserted = s.CreateExternalShare(projectExternalShare, identity, projectId);

                switch (hasInserted)
                {
                //project for creating external share not found
                case false:
                    httpStatusCode = HttpStatusCode.NotFound;
                    break;

                //external share inserted ok
                case true:
                    httpStatusCode = HttpStatusCode.Created;
                    break;
                }

                return(Request.CreateResponse(httpStatusCode));
            }
        }
        public HttpResponseMessage GetProjectStatsByProjectIdForLoggedOutUsers(int projectId)
        {
            if (projectId <= 0)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            using (var s = new ProjectStatRepository())
            {
                var v = s.GetProjectStats(projectId);

                if (v == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, v));
            }
        }
        public HttpResponseMessage GetProjectStatsByProjectIdForLoggedInUsers(int projectId)
        {
            if (projectId <= 0)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var identity = User.Identity as ClaimsIdentity;

            using (var s = new ProjectStatRepository())
            {
                var v = s.GetProjectStats(projectId, identity);

                if (v == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, v));
            }
        }