public Result <Reward> CreateReward(int rewardPackageId, CreateRewardOptions options) { if (options == null) { return(Result <Reward> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Name)) { return(Result <Reward> .ActionFailed( StatusCode.BadRequest, "Null or empty Name")); } var rewardPackage = context_ .Set <RewardPackage>() .Include(x => x.Rewards) .Where(rp => rp.RewardPackageId == rewardPackageId) .SingleOrDefault(); if (rewardPackage == null) { return(Result <Reward> .ActionFailed( StatusCode.BadRequest, "Invalid Reward Package")); } var reward = new Reward() { Name = options.Name }; rewardPackage.Rewards.Add(reward); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <Reward> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Reward> .ActionFailed( StatusCode.InternalServerError, "Reward could not be created")); } return(Result <Reward> .ActionSuccessful(reward)); }
public Result <Video> CreateVideo(int projectId, CreateVideoOptions options) { if (options == null) { return(Result <Video> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Url)) { return(Result <Video> .ActionFailed( StatusCode.BadRequest, "Null or empty Url")); } var project = context_ .Set <Project>() .Where(p => p.ProjectId == projectId) .SingleOrDefault(); if (project == null) { return(Result <Video> .ActionFailed( StatusCode.BadRequest, "Invalid projectId")); } var video = new Video() { Url = options.Url, }; project.Videos.Add(video); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <Video> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Video> .ActionFailed( StatusCode.InternalServerError, "Video could not be added")); } return(Result <Video> .ActionSuccessful(video)); }
public Result <Posts> CreatePost(int projectId, CreatePostOptions options) { if (options == null) { return(Result <Posts> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Post)) { return(Result <Posts> .ActionFailed( StatusCode.BadRequest, "Null or empty Post")); } var project = context_ .Set <Project>() .Where(p => p.ProjectId == projectId) .SingleOrDefault(); if (project == null) { return(Result <Posts> .ActionFailed( StatusCode.BadRequest, "Invalid projectId")); } var post = new Posts() { Post = options.Post }; project.Posts.Add(post); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <Posts> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Posts> .ActionFailed( StatusCode.InternalServerError, "Post could not be created")); } return(Result <Posts> .ActionSuccessful(post)); }
public Result <BackedProjects> CreateBackedProject( CreateBackedProjectOptions options) { if (options == null) { return(Result <BackedProjects> .ActionFailed( StatusCode.BadRequest, "Null options")); } var user = userService_ .GetById(options.UserId) .Include(x => x.BackedProjectsList) .SingleOrDefault(); if (user == null) { return(Result <BackedProjects> .ActionFailed( StatusCode.BadRequest, "Invalid UserId")); } var project = projectService_ .GetProjectById(options.ProjectId); if (project == null) { return(Result <BackedProjects> .ActionFailed( StatusCode.BadRequest, "Invalid ProjectId")); } var backedProject = new BackedProjects() { Amount = options.Amount, ProjectId = project.ProjectId, Name = project.Name, Category = project.Category, Description = project.Description, Photo = project.Photos.ElementAt(0).Url, NumberOfBackers = project.NumberOfBackers, ProjectCreatorId = project.UserId, ProjectCreatorFirstName = project.User.FirstName, ProjectCreatorLastName = project.User.LastName }; var backedProjectIds = user.BackedProjectsList .Select(s => s.ProjectId).ToList(); if (backedProjectIds.Contains(project.ProjectId)) { project.AmountGathered += options.Amount; context_.SaveChanges(); return(Result <BackedProjects> .ActionFailed( StatusCode.OK, "Reward Package was succesfully purchased")); } project.NumberOfBackers += 1; backedProject.NumberOfBackers += 1; project.AmountGathered += options.Amount; user.BackedProjectsList.Add(backedProject); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <BackedProjects> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <BackedProjects> .ActionFailed( StatusCode.InternalServerError, "Backed Project could not be created")); } return(Result <BackedProjects> .ActionSuccessful(backedProject)); }
public Result <Project> CreateProject(CreateProjectOptions options) { if (options == null) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Name)) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Null or empty Name")); } if (string.IsNullOrWhiteSpace(options.Description)) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Null or empty Description")); } if (options.AmountRequired <= 0 || options.AmountRequired == null) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Null or empty AmountRequired")); } if (options.DueTo == null || options.DueTo.CompareTo(options.CreatedOn) <= 0) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Invalid Due to date")); } if (options.Videos == null || options.Videos.Any() == false) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "No Videos given")); } if (options.Photos == null || options.Photos.Any() == false) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "No Photos given")); } if ((int)options.Category < 1 || (int)options.Category > 8) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Null or invalid Category")); } var user = userService_ .GetById(options.UserId) .Include(x => x.CreatedProjectsList) .SingleOrDefault(); if (user == null) { return(Result <Project> .ActionFailed( StatusCode.BadRequest, "Invalid User")); } var project = new Project() { Name = options.Name, Description = options.Description, Category = options.Category, AmountRequired = options.AmountRequired.Value, DueTo = options.DueTo, User = user, }; foreach (var photo in options.Photos) { var a = new Photo() { Url = photo, }; project.Photos.Add(a); } foreach (var video in options.Videos) { var v = new Video() { Url = video, }; project.Videos.Add(v); } user.CreatedProjectsList.Add(project); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <Project> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <Project> .ActionFailed( StatusCode.InternalServerError, "Project could not be created")); } return(Result <Project> .ActionSuccessful(project)); }
public Result <User> CreateUser(CreateUserOptions options) { if (options == null) { return(Result <User> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.FirstName)) { return(Result <User> .ActionFailed( StatusCode.BadRequest, "Null or empty FirstName")); } if (string.IsNullOrWhiteSpace(options.LastName)) { return(Result <User> .ActionFailed( StatusCode.BadRequest, "Null or empty LastName")); } if (string.IsNullOrWhiteSpace(options.Email)) { return(Result <User> .ActionFailed( StatusCode.BadRequest, "Null or empty Email")); } if (string.IsNullOrWhiteSpace(options.Country)) { return(Result <User> .ActionFailed( StatusCode.BadRequest, "Null or empty Country")); } var user = new User() { FirstName = options.FirstName, LastName = options.LastName, Email = options.Email, Country = options.Country, Description = options.Description, }; context.Add(user); var rows = 0; try { rows = context.SaveChanges(); } catch (Exception ex) { return(Result <User> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <User> .ActionFailed( StatusCode.InternalServerError, "User could not be created")); } return(Result <User> .ActionSuccessful(user)); }
public Result <RewardPackage> CreateRewardPackage( CreateRewardPackageOptions options) { if (options == null) { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Description)) { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (string.IsNullOrWhiteSpace(options.Name)) { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Null options")); } if (options.Amount == null || options.Amount <= 0) { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Invalid Amount")); } var project = context_ .Set <Project>() .Where(p => p.ProjectId == options.ProjectId) .SingleOrDefault(); if (project == null) { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Invalid ProjectId")); } var rewardPackage = new RewardPackage() { Amount = options.Amount, Description = options.Description, Name = options.Name }; foreach (var option in options.RewardOptions) { if (option == null) { continue; } var createdReward = rewardService_.AddRewardToList(option); if (createdReward != null) { rewardPackage.Rewards.Add(createdReward.Data); } else { return(Result <RewardPackage> .ActionFailed( StatusCode.BadRequest, "Invalid Rewards given")); } } project.RewardPackages.Add(rewardPackage); context_.Add(rewardPackage); var rows = 0; try { rows = context_.SaveChanges(); } catch (Exception ex) { return(Result <RewardPackage> .ActionFailed( StatusCode.InternalServerError, ex.ToString())); } if (rows <= 0) { return(Result <RewardPackage> .ActionFailed( StatusCode.InternalServerError, "Reward Package could not be created")); } return(Result <RewardPackage> .ActionSuccessful(rewardPackage)); }