Beispiel #1
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));
        }
        public IActionResult Update(int id,
                                    [FromBody] UpdateRewardOptions options)
        {
            var result = rewardService.UpdateReward(id,
                                                    options);

            if (!result.Success)
            {
                return(StatusCode((int)result.ErrorCode,
                                  result.ErrorText));
            }

            return(Json(result.Data));;
        }
        public Result <bool> UpdateReward(int rewardId, UpdateRewardOptions options)
        {
            if (options == null)
            {
                return(Result <bool> .CreateFailed(StatusCode.BadRequest, "Null options"));
            }

            var reward = GetRewardById(rewardId).Data;

            if (reward == null)
            {
                return(Result <bool> .CreateFailed(StatusCode.BadRequest, $"Reward with {rewardId} was not found"));
            }

            if (reward.IsValidTitle(options.Title))
            {
                reward.Title = options.Title;
            }

            if (reward.IsValidDescription(options.Description))
            {
                reward.Description = options.Description;
            }

            if (reward.IsValidPrice(options.Price))
            {
                reward.Description = options.Description;
            }

            if (context_.SaveChanges() == 0)
            {
                return(Result <bool> .CreateFailed(StatusCode.InternalServerError, "Reward could not be updated"));
            }

            return(Result <bool> .CreateSuccessful(true));
        }