<commit_before><?php namespace DMA\Friends\API\Resources;

use Session;
use Response;
use DMA\Friends\Models\Reward;
use DMA\Friends\Classes\RewardManager;
use DMA\Friends\Classes\API\BaseResource;

use RainLab\User\Models\User;


class RewardResource extends BaseResource {

    protected $model        = '\DMA\Friends\Models\Reward';

    protected $transformer  = '\DMA\Friends\API\Transformers\RewardTransformer';

    public function __construct()
    {
        // Add additional routes to Activity resource
        $this->addAdditionalRoute('redeem',   'redeem/{reward}/user/{user}',   ['GET', 'POST']);
    }
    
    public function redeem($rewardId, $userId)
    {
        try{
            
            if(is_null($user = User::find($userId))){
                return Response::api()->errorNotFound('User not found');
            }
            
            if(is_null($reward = Reward::find($rewardId))){
                return Response::api()->errorNotFound('Reward not found');
            }
            
            RewardManager::redeem($rewardId, $user);
            
            // Check if redeem was successful 
            $message = Session::pull('rewardMessage');
            $type    = ($message) ? 'info' : 'error'; 
            
            $success = true;
            $httpCode = 200;
            
            
            if($type == 'error'){
                $success = false;
                $httpCode = 201;
                $message = Session::pull('rewardError');
            }
            
    
            $payload = [
                    'data' => [
                            'success' => $success,
                            'message' => $message
                    ]
            ];
            
            return Response::api()->setStatusCode($httpCode)->withArray($payload);
            
             
        } catch(Exception $e) {
            return Response::api()->errorInternalError($e->getMessage());
        }
    }
    
    
}
<commit_msg>Fixed wrong HTTP code returned when redeem a reward<commit_after><?php namespace DMA\Friends\API\Resources;

use Session;
use Response;
use DMA\Friends\Models\Reward;
use DMA\Friends\Classes\RewardManager;
use DMA\Friends\Classes\API\BaseResource;

use RainLab\User\Models\User;


class RewardResource extends BaseResource {

    protected $model        = '\DMA\Friends\Models\Reward';

    protected $transformer  = '\DMA\Friends\API\Transformers\RewardTransformer';

    public function __construct()
    {
        // Add additional routes to Activity resource
        $this->addAdditionalRoute('redeem',   'redeem/{reward}/user/{user}',   ['GET', 'POST']);
    }
    
    public function redeem($rewardId, $userId)
    {
        try{
            
            if(is_null($user = User::find($userId))){
                return Response::api()->errorNotFound('User not found');
            }
            
            if(is_null($reward = Reward::find($rewardId))){
                return Response::api()->errorNotFound('Reward not found');
            }
            
            RewardManager::redeem($rewardId, $user);
            
            // Check if redeem was successful 
            $message = Session::pull('rewardMessage');
            $type    = ($message) ? 'info' : 'error'; 
            
            $success = true;
            $httpCode = 201;
            
            
            if($type == 'error'){
                $success = false;
                $httpCode = 200;
                $message = Session::pull('rewardError');
            }
            
    
            $payload = [
                    'data' => [
                            'success' => $success,
                            'message' => $message
                    ]
            ];
            
            return Response::api()->setStatusCode($httpCode)->withArray($payload);
            
             
        } catch(Exception $e) {
            return Response::api()->errorInternalError($e->getMessage());
        }
    }
    
    
}

{'fraction_non_alphanumeric': 0.12692882030861125, 'fraction_numerical': 0.0029865604778496766, 'mean_word_length': 1.513445903689806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3288007', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1036, 'n_words': 192}