Exemplo n.º 1
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));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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));
        }