<commit_before><?php

namespace Rogue\Http\Controllers\Api;

use Rogue\Models\Reaction;
use Rogue\Models\Photo;
use Rogue\Http\Requests\ReactionRequest;
use Rogue\Http\Transformers\ReactionTransformer;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ReactionController extends ApiController
{
    /**
     * @var \Rogue\Http\Transformers\ReactionTransformer.
     */
    protected $reactionTransformer;

    /**
     * Create new ReactionController instance.
     */
    public function __construct()
    {
        $this->middleware('api');

        $this->transformer = new ReactionTransformer;
    }

    /**
     * Store or update a reportback_item's reaction.
     * Referenced from https://mydnic.be/post/the-perfect-like-system-for-laravel
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(ReactionRequest $request)
    {
        $userId = $request['northstar_id'];
        $reactionableId = $request['reactionable_id'];
        $reactionableType = $request['reactionable_type'];

        // Check to see if the post has a reaction from this particular user with id of northstar_id.
        $reaction = Reaction::withTrashed()->where(['northstar_id' => $userId, 'reactionable_id' => $reactionableId, 'reactionable_type' => $reactionableType])->first();

        // If a post does not have a reaction from this user, create a reaction.
        if (is_null($reaction)) {
            $reaction = Reaction::create([
                'northstar_id' => $userId,
                'reactionable_id' => $reactionableId,
                'reactionable_type' => $reactionableType,
            ]);

            $code = 200;
            $action = 'liked';
        } else {
            // If the post was previously "liked" by this user, soft delete the reaction. Otherwise, restore the reaction.
            $code = 201;
            if (is_null($reaction->deleted_at)) {
                $action = 'unliked';
                $reaction->delete();
            } else {
                $action = 'liked';
                $reaction->restore();
            }
        }

        $reactionableType = explode('\\', $reactionableType)[2];

        // @TODO: as we add more post types, we should break the below into a helper function and add different cases.
        if ($reactionableType === "Photo") {
            $photo = Photo::where('id', $reactionableId)->first();
            $totalReactions = count($photo->reactions);
        } else {
            throw new HttpException(405, 'Not a valid post type');
        }

        $meta = [
            'action' => $action,
            'total_reactions' => $totalReactions,
        ];

        return $this->item($reaction, $code, $meta);
    }
}<commit_msg>style ci<commit_after><?php

namespace Rogue\Http\Controllers\Api;

use Rogue\Models\Photo;
use Rogue\Models\Reaction;
use Rogue\Http\Requests\ReactionRequest;
use Rogue\Http\Transformers\ReactionTransformer;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ReactionController extends ApiController
{
    /**
     * @var \Rogue\Http\Transformers\ReactionTransformer.
     */
    protected $reactionTransformer;

    /**
     * Create new ReactionController instance.
     */
    public function __construct()
    {
        $this->middleware('api');

        $this->transformer = new ReactionTransformer;
    }

    /**
     * Store or update a reportback_item's reaction.
     * Referenced from https://mydnic.be/post/the-perfect-like-system-for-laravel
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(ReactionRequest $request)
    {
        $userId = $request['northstar_id'];
        $reactionableId = $request['reactionable_id'];
        $reactionableType = $request['reactionable_type'];

        // Check to see if the post has a reaction from this particular user with id of northstar_id.
        $reaction = Reaction::withTrashed()->where(['northstar_id' => $userId, 'reactionable_id' => $reactionableId, 'reactionable_type' => $reactionableType])->first();

        // If a post does not have a reaction from this user, create a reaction.
        if (is_null($reaction)) {
            $reaction = Reaction::create([
                'northstar_id' => $userId,
                'reactionable_id' => $reactionableId,
                'reactionable_type' => $reactionableType,
            ]);

            $code = 200;
            $action = 'liked';
        } else {
            // If the post was previously "liked" by this user, soft delete the reaction. Otherwise, restore the reaction.
            $code = 201;
            if (is_null($reaction->deleted_at)) {
                $action = 'unliked';
                $reaction->delete();
            } else {
                $action = 'liked';
                $reaction->restore();
            }
        }

        $reactionableType = explode('\\', $reactionableType)[2];

        // @TODO: as we add more post types, we should break the below into a helper function and add different cases.
        if ($reactionableType === 'Photo') {
            $photo = Photo::where('id', $reactionableId)->first();
            $totalReactions = count($photo->reactions);
        } else {
            throw new HttpException(405, 'Not a valid post type');
        }

        $meta = [
            'action' => $action,
            'total_reactions' => $totalReactions,
        ];

        return $this->item($reaction, $code, $meta);
    }
}
{'fraction_non_alphanumeric': 0.12439331296063275, 'fraction_numerical': 0.003595182455509617, 'mean_word_length': 2.2576112412177984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 2, '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': '4034787', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1512, 'n_words': 373}