public async Task <IActionResult> Post([FromBody] Reward reward) { if (reward == null) { // return HTTP 400 badrequest as something is wrong return(BadRequest("Reward information formatted incorrectly.")); } var result = await _rewardRepository.CreateReward(reward); if (result == 1) { // Generate a 'user reward' and add it to all users in the DB. var rewards = new List <Reward> { reward }; var userReward = UsersController.GenerateUserRewards(rewards); await _userRepository.AddUserRewardToAllUsers(userReward.First()); // return HTTP 201 Created with reward object in body of return and a 'location' header with URL of newly created object return(CreatedAtAction("Get", new { id = reward.Id }, reward)); } else if (result == -10) { // return HTTP 409 Conflict as reward already exists in DB return(Conflict("Reward with ID '" + reward.Id + "' already exists. Cannot create a duplicate.")); } else { // return HTTP 400 badrequest as something is wrong return(BadRequest("An internal error occurred. Please contact the system administrator.")); } }
public DomainResult <Reward> CreateReward(CreateRewardDto createReward) { var result = new DomainResult <Reward>(); var player = playerRepository.GetPlayer(createReward.RewardCreatorId); if (player == null) { result.Errors.Add(new MissingPlayerErrorModel(createReward.RewardCreatorId)); return(result); } var rewardModel = Mapper.Map <Reward>(createReward); result.Entity = rewardRepository.CreateReward(rewardModel); rewardRepository.SaveChanges(); return(result); }