public Result <Pledge> CreatePledges(PledgeOptions pledgeOptions) { if (pledgeOptions == null) { return(Result <Pledge> .CreateFailed( StatusCode.BadRequest, "Null options")); } var project = projectservices.FindProjectById(pledgeOptions.ProjectId); var pledge = new Pledge { Project = project, PledgeDescription = pledgeOptions.PledgeDescription, PledgePrice = pledgeOptions.PledgePrice, PledgeReward = pledgeOptions.PledgeReward, }; _db.Add(pledge); var rows = 0; try { rows = _db.SaveChanges(); } catch (Exception ex) { return(Result <Pledge> .CreateFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Pledge> .CreateFailed( StatusCode.InternalServerError, "Pledge could not be updated")); } return(Result <Pledge> .CreateSuccessful(pledge)); }
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)); }
//Create post TODO public Result <Post> CreatePost(PostOptions postOptions) { if (postOptions == null) { return(Result <Post> .CreateFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(postOptions.PostDescription)) { return(Result <Post> .CreateFailed( StatusCode.BadRequest, "Null or empty PostDescription")); } string userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); var project = projectServices.FindProjectById(postOptions.ProjectId); Post post = new Post { UserId = userId, Project = project, PostDescription = postOptions.PostDescription }; _db.Add(post); var rows = 0; try { rows = _db.SaveChanges(); } catch (Exception ex) { return(Result <Post> .CreateFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Post> .CreateFailed( StatusCode.InternalServerError, "Post could not be updated")); } return(Result <Post> .CreateSuccessful(post)); }
public Result <Project> CreateProject(ProjectOptions projectoption) { if (projectoption == null) { return(Result <Project> .CreateFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(projectoption.ProjectTitle)) { return(Result <Project> .CreateFailed( StatusCode.BadRequest, "Null or empty ProjectTitle")); } if (projectoption.ProjectTargetAmount == 0) { return(Result <Project> .CreateFailed( StatusCode.BadRequest, "Null or empty ProjectTargetAmount")); } //get username and userid from httpcontext string userName = httpContextAccessor.HttpContext.User.Identity.Name; userName = userName.Substring(0, userName.IndexOf('@')); string userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); //am i sure? //if (string.IsNullOrWhiteSpace(projectoption.MultimediaURL)) //{ // return Result<Project>.CreateFailed( // StatusCode.BadRequest, "Null or empty Multimedia URL"); //} var project = new Project() { UserId = userId, ProjectCreator = userName, ProjectTitle = projectoption.ProjectTitle, ProjectDescription = projectoption.ProjectDescription, ProjectTargetAmount = projectoption.ProjectTargetAmount, ProjectEndingDate = projectoption.ProjectEndingDate, ProjectCategory = projectoption.ProjectCategory, IsActive = true, ProjectMultimedia = new List <Multimedia> { new Multimedia { MultimediaTypes = projectoption.MultimediaTypes, MultimediaURL = projectoption.FilePath }, }, ProjectPosts = new List <Post> { new Post { PostDescription = "You can help us by funding our project, or simply share it to your friends who might be interested!" }, }, }; _db.Add(project); var rows = 0; try { rows = _db.SaveChanges(); } catch (Exception ex) { return(Result <Project> .CreateFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Project> .CreateFailed( StatusCode.InternalServerError, "Project could not be updated")); } return(Result <Project> .CreateSuccessful(project)); }