예제 #1
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));
        }