예제 #1
0
        public Result <RewardPackage> UpdateRewardPackage(
            int rewardPackageId,
            UpdateRewardPackageOptions options)
        {
            var result = new Result <RewardPackage>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var rewardPackage = GetRewardPackageById(rewardPackageId);

            if (rewardPackage == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Reward Package with id {rewardPackageId} was not found";

                return(result);
            }

            if (options.Amount != null)
            {
                rewardPackage.Amount = options.Amount;
            }

            if (!string.IsNullOrWhiteSpace(options.Description))
            {
                rewardPackage.Description = options.Description;
            }

            if (!string.IsNullOrWhiteSpace(options.Name))
            {
                rewardPackage.Name = options.Name;
            }

            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 updated"));
            }

            return(Result <RewardPackage> .ActionSuccessful(rewardPackage));
        }
예제 #2
0
        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));
        }
예제 #3
0
        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));
        }
예제 #4
0
        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));
        }
예제 #5
0
        public Result <Reward> UpdateReward(
            int rewardId,
            UpdateRewardOptions options)
        {
            var result = new Result <Reward>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var reward = SearchReward(new SearchRewardOptions()
            {
                RewardId = rewardId
            }).SingleOrDefault();

            if (reward == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Reward with id {rewardId} was not found";

                return(result);
            }

            if (!string.IsNullOrWhiteSpace(options.Name))
            {
                reward.Name = options.Name;
            }

            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 updated"));
            }

            return(Result <Reward> .ActionSuccessful(reward));
        }
예제 #6
0
        public Result <Posts> UpdatePost(
            int postId,
            UpdatePostOptions options)
        {
            var result = new Result <Posts>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var post = SearchPost(new SearchPostOptions()
            {
                PostId = postId
            }).SingleOrDefault();

            if (post == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Post with id {postId} was not found";

                return(result);
            }

            if (!string.IsNullOrWhiteSpace(options.Post))
            {
                post.Post = options.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 updated"));
            }

            return(Result <Posts> .ActionSuccessful(post));
        }
예제 #7
0
        public Result <Reward> AddRewardToList(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 reward = new Reward()
            {
                Name = options.Name
            };

            context_.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 <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));
        }
예제 #9
0
        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));
        }
예제 #10
0
        public Result <Project> UpdateProject(
            UpdateProjectOptions options)
        {
            var result = new Result <Project>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var project = context_
                          .Set <Project>()
                          .Where(x => x.ProjectId == options.ProjectId)
                          .Include(x => x.Photos)
                          .Include(x => x.Videos)
                          .SingleOrDefault();

            if (project == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"Project with id {options.ProjectId} was not found";

                return(result);
            }

            if (options.Category != null && options.Category > 0 && (int)options.Category < 9)
            {
                project.Category = options.Category.Value;
            }

            if (!string.IsNullOrWhiteSpace(options.Description))
            {
                project.Description = options.Description;
            }

            if (!string.IsNullOrWhiteSpace(options.Name))
            {
                project.Name = options.Name;
            }

            if (options.AmountRequired != null)
            {
                project.AmountRequired = options.AmountRequired.Value;
            }

            if (options.DueTo != null && options.DueTo.CompareTo(DateTime.Today) > 0)
            {
                project.DueTo = options.DueTo;
            }

            if (options.Photos != null)
            {
                if (options.Photos.Count > 0 && !string.IsNullOrWhiteSpace(options.Photos[0]))
                {
                    var p = new Photo()
                    {
                        Url = options.Photos[0],
                    };
                    project.Photos.Add(p);
                }
            }

            if (options.Videos != null)
            {
                if (options.Videos.Count > 0 && !string.IsNullOrWhiteSpace(options.Videos[0]))
                {
                    var v = new Video()
                    {
                        Url = options.Videos[0],
                    };
                    project.Videos.Add(v);
                }
            }

            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 updated"));
            }

            return(Result <Project> .ActionSuccessful(project));
        }
예제 #11
0
        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));
        }
예제 #12
0
        public Result <User> UpdateUser(int userId,
                                        UpdateUserOptions options)
        {
            var result = new Result <User>();

            if (options == null)
            {
                result.ErrorCode = StatusCode.BadRequest;
                result.ErrorText = "Null options";

                return(result);
            }

            var user = GetById(userId)
                       .Include(x => x.CreatedProjectsList)
                       .Include(c => c.BackedProjectsList)
                       .SingleOrDefault();

            if (user == null)
            {
                result.ErrorCode = StatusCode.NotFound;
                result.ErrorText = $"User with id {userId} was not found";

                return(result);
            }

            if (!string.IsNullOrWhiteSpace(options.FirstName))
            {
                user.FirstName = options.FirstName;
            }

            if (!string.IsNullOrWhiteSpace(options.Country))
            {
                user.Country = options.Country;
            }

            if (!string.IsNullOrWhiteSpace(options.Description))
            {
                user.Description = options.Description;
            }

            if (!string.IsNullOrWhiteSpace(options.Email))
            {
                user.Email = options.Email;
            }

            if (!string.IsNullOrWhiteSpace(options.LastName))
            {
                user.LastName = options.LastName;
            }

            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 updated"));
            }

            return(Result <User> .ActionSuccessful(user));
        }
예제 #13
0
        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));
        }