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)); } }
// OK public StatusCodes CreateFundingPackage(FundingPackageModel source, ClaimsIdentity identity, int projectId, bool isForDonations) { try { //get the project var _project = uow.ProjectRepository.FindById(projectId); if (_project == null) { return(StatusCodes.NOT_FOUND); } else { //check if current user is the projectId's creator //else return NOT ALLOWED long requestorUserId = GetRequestorIdFromIdentity(identity); if (_project.User.Id != requestorUserId) { return(StatusCodes.NOT_AUTHORIZED); } var _projectFundingPackage = new FundingPackage() { ProjectId = projectId, AttachmentSetId = source.AttachmentSetId, Title = source.Title, PledgeAmount = (isForDonations ? null : source.PledgeAmount), Description = source.Description, WhenDateTime = DateTime.Now, EstimatedDeliveryDate = DateTime.Now //EstimatedDeliveryDate = (isForDonations? DateTime.Now : source.EstimatedDeliveryDate) }; uow.FundingPackageRepository.Insert(_projectFundingPackage, true); } return(StatusCodes.OK); } catch (Exception) { throw; } }
// OK public StatusCodes EditFundingPackage(FundingPackageModel source, ClaimsIdentity identity, int projectId, int fundingPackageId) { try { var _project = uow.ProjectRepository.FindById(projectId); var _fundingPackage = uow.FundingPackageRepository.FindById(fundingPackageId); if (_project == null || _fundingPackage == null) { return(StatusCodes.NOT_FOUND); } else { // funding package found. does the user that wishes to update it really is the project creator? check this here long requestorUserId = GetRequestorIdFromIdentity(identity); if (_fundingPackage.Project.UserId != requestorUserId) { return(StatusCodes.NOT_AUTHORIZED); } _fundingPackage.AttachmentSetId = source.AttachmentSetId; _fundingPackage.WhenDateTime = DateTime.Now; _fundingPackage.Title = source.Title; _fundingPackage.Description = source.Description; _fundingPackage.EstimatedDeliveryDate = source.EstimatedDeliveryDate; //if the funding package is not the donations package then you may also update the PledgeAmount if (_fundingPackage.PledgeAmount != null && _fundingPackage.PledgeAmount.HasValue) { _fundingPackage.PledgeAmount = source.PledgeAmount; } uow.FundingPackageRepository.Update(_fundingPackage, true); } return(StatusCodes.OK); } catch (Exception) { throw; } }
public HttpResponseMessage UpdateFundingPackage(FundingPackageModel fundingPackage, int projectId, int fundingPackageId) { if (!ModelState.IsValid || projectId <= 0 || fundingPackageId <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } var identity = User.Identity as ClaimsIdentity; using (var s = new FundingPackageRepository()) { var httpStatusCode = HttpStatusCode.OK; FundingPackageRepository.StatusCodes hasUpdated = s.EditFundingPackage(fundingPackage, identity, projectId, fundingPackageId); switch (hasUpdated) { //project or funding package not found case FundingPackageRepository.StatusCodes.NOT_FOUND: httpStatusCode = HttpStatusCode.NotFound; break; //not authorized to update this funding package. You are not the project creator that has this specific funding package case FundingPackageRepository.StatusCodes.NOT_AUTHORIZED: httpStatusCode = HttpStatusCode.MethodNotAllowed; break; //funding package updated ok case FundingPackageRepository.StatusCodes.OK: httpStatusCode = HttpStatusCode.OK; break; } return(Request.CreateResponse(httpStatusCode)); } }
public HttpResponseMessage CreateFundingPackage(FundingPackageModel fundingPackage, int projectId) { if (!ModelState.IsValid || projectId <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } var identity = User.Identity as ClaimsIdentity; using (var s = new FundingPackageRepository()) { var httpStatusCode = HttpStatusCode.Created; FundingPackageRepository.StatusCodes hasInserted = s.CreateFundingPackage(fundingPackage, identity, projectId, false); switch (hasInserted) { //project for inserting the funding package not found case FundingPackageRepository.StatusCodes.NOT_FOUND: httpStatusCode = HttpStatusCode.NotFound; break; //not authorized to add this funding package to this specific project case FundingPackageRepository.StatusCodes.NOT_AUTHORIZED: httpStatusCode = HttpStatusCode.MethodNotAllowed; break; //funding package inserted ok case FundingPackageRepository.StatusCodes.OK: httpStatusCode = HttpStatusCode.Created; break; } return(Request.CreateResponse(httpStatusCode)); } }