<commit_before><?php

class CollectionsApiController extends \BaseController
{

    /**
     * Display a listing of the resource.
     * GET /collections
     * @param string $username
     * @return Response
     */
    public function index($username)
    {
        try {

            $user = User::where('username', $username)->firstOrFail(['id', 'name', 'username']);

            $collections = $user->collection()->where('public', 1)->get(['title', 'id', 'user_id', 'created_at']);

            if (count($collections) < 1) {
                throw new \Illuminate\Database\Eloquent\ModelNotFoundException('no results returned for collections');
            }

            $results = [
                'user' => $user->toArray(),
                'collections' => $collections->toArray()
            ];

            return Response::json($results, 200);
        }

        catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return Response::json(['error' => 'No collections found']);
        }
    }

    public function doesItemExistInCollection($item_id, $collection_id)
    {
        $count = DB::table('collection_item')->where('collection_id', $collection_id)->where('item_id', $item_id)->count();
        if ($count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function updateCollectionPreviewImage(Collection $collection, Item $item) {
        $url = $item->photos()->where('type', 'full_res')->orderBy('created_at', 'dsc')->first(['url']);

        if (!isset($url)) {
            $url = ['url' => 'https://sailr.co/img/default-lg.jpg'];
        }

        $collection->preview_image = $url['url'];
        $collection->save();

        return $collection;

    }

    public function favourite()
    {

        $id = Input::get('item_id');

        $user = User::findOrFail(12); //Auth::user()
        $item = Item::findOrFail($id, ['id', 'user_id', 'public']);

        $collection = [];

        if ($user->collection()->where('title', 'Likes')->count() < 1) {
            //So we better make a favourite collection
            $collection = new Collection;
            $collection->title = 'Likes';
            $collection->user_id = $user->id;
            $collection->save();

            $user->likes_collection_id = $collection->id;
            $user->save();
        } else {
            $collection = $user->collection()->where('title', 'Likes')->first();
        }

        $res = Response::json(['message' => 'Successfully liked'], 201);


        if ($this->doesItemExistInCollection($item->id, $collection->id)) {
            $res = Response::json(['message' => 'You have already liked this product'], 400);
        } else {
            if ($item->public != 1) return Response::json(['message' => 'Products that are not public can not be put into collections'], 403);

            $collection->items()->save($item);
            $this->updateCollectionPreviewImage($collection, $item);
        }

        return $res;
    }


    /**
     * Create a new collection and set its first product, or add a new product to a collection.
     * POST /collections
     *
     * @return Response
     */
    public function store()
    {

        $user = User::findOrFail(12); //Auth::user()
        $item = Item::findOrFail(Input::get('item_id'), ['id', 'user_id', 'public']);

        $input = Input::all();
        $validator = \Sailr\Validators\CollectionsValidator::make();

        $result = $validator->validate($input, 'update');
        if (!$result) {
            return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
        }

        if ($item->public != 1) return Response::json(['message' => 'Products that are not public can not be put into collections'], 403);

        $createNewCollection = Input::has('collection_title');

        if ($createNewCollection) {

            $result = $validator->validate($input, 'create');
            if (!$result) {
                return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
            }

            $collection = new Collection;
            $collection->title = $input['collection_title'];
            $collection->user_id = $user->id;
            $collection->save();
        }

        else {
            $result = $validator->validate($input, 'update');
            if (!$result) {
                return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
            }

            try {
                $collection = Collection::findOrFail(Input::get('collection_id'));
            }

            catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
                return Response::json(['message' => "Sorry, we couldn't find a collection to add the product to"], 404);
            }
        }

        if ($this->doesItemExistInCollection($item->id, $collection->id)) {
            $res = Response::json(['message' => 'You already have this product in your collection'], 400);
        } else {

            $collection->items()->save($item);
            $this->updateCollectionPreviewImage($collection, $item);

            $res = Response::json($collection->toArray(), 201);
        }

        return $res;
    }

    public function getCollection($id, $currentCollection = null) {

        $response = [];

        if (isset($currentCollection)) {
            $collection = $currentCollection;
        }
        else {
            $collection = Collection::findOrFail($id);
        }

        $response['items'] = $collection->items()->get()->toArray();
        $response['followers'] = $collection->users()->count();
        $response['user'] = $collection->user()->firstOrFail(['id', 'name', 'username'])->toArray();

        return Response::json($response);
    }

    public function getLikes($user_id) {
        $c = Collection::where('title', 'Likes')->where('user_id', $user_id)->firstOrFail();
        return $this->getCollection($c, $c->id);
    }

    /**
     * Update the specified resource in storage.
     * PUT /collections/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function update($id)<commit_msg>Removes unused blank variable inited as array, makes getCollection only get public items, fixes order of getCollection call in getLikes()<commit_after><?php

class CollectionsApiController extends \BaseController
{

    /**
     * Display a listing of the resource.
     * GET /collections
     * @param string $username
     * @return Response
     */
    public function index($username)
    {
        try {

            $user = User::where('username', $username)->firstOrFail(['id', 'name', 'username']);

            $collections = $user->collection()->where('public', 1)->get(['title', 'id', 'user_id', 'created_at']);

            if (count($collections) < 1) {
                throw new \Illuminate\Database\Eloquent\ModelNotFoundException('no results returned for collections');
            }

            $results = [
                'user' => $user->toArray(),
                'collections' => $collections->toArray()
            ];

            return Response::json($results, 200);
        }

        catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return Response::json(['error' => 'No collections found']);
        }
    }

    public function doesItemExistInCollection($item_id, $collection_id)
    {
        $count = DB::table('collection_item')->where('collection_id', $collection_id)->where('item_id', $item_id)->count();
        if ($count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function updateCollectionPreviewImage(Collection $collection, Item $item) {
        $url = $item->photos()->where('type', 'full_res')->orderBy('created_at', 'dsc')->first(['url']);

        if (!isset($url)) {
            $url = ['url' => 'https://sailr.co/img/default-lg.jpg'];
        }

        $collection->preview_image = $url['url'];
        $collection->save();

        return $collection;

    }

    public function favourite()
    {

        $id = Input::get('item_id');

        $user = User::findOrFail(12); //Auth::user()
        $item = Item::findOrFail($id, ['id', 'user_id', 'public']);
        
        if ($user->collection()->where('title', 'Likes')->count() < 1) {
            //So we better make a favourite collection
            $collection = new Collection;
            $collection->title = 'Likes';
            $collection->user_id = $user->id;
            $collection->save();

            $user->likes_collection_id = $collection->id;
            $user->save();
        } else {
            $collection = $user->collection()->where('title', 'Likes')->first();
        }

        $res = Response::json(['message' => 'Successfully liked'], 201);


        if ($this->doesItemExistInCollection($item->id, $collection->id)) {
            $res = Response::json(['message' => 'You have already liked this product'], 400);
        } else {
            if ($item->public != 1) return Response::json(['message' => 'Products that are not public can not be put into collections'], 403);

            $collection->items()->save($item);
            $this->updateCollectionPreviewImage($collection, $item);
        }

        return $res;
    }


    /**
     * Create a new collection and set its first product, or add a new product to a collection.
     * POST /collections
     *
     * @return Response
     */
    public function store()
    {

        $user = User::findOrFail(12); //Auth::user()
        $item = Item::findOrFail(Input::get('item_id'), ['id', 'user_id', 'public']);

        $input = Input::all();
        $validator = \Sailr\Validators\CollectionsValidator::make();

        $result = $validator->validate($input, 'update');
        if (!$result) {
            return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
        }

        if ($item->public != 1) return Response::json(['message' => 'Products that are not public can not be put into collections'], 403);

        $createNewCollection = Input::has('collection_title');

        if ($createNewCollection) {

            $result = $validator->validate($input, 'create');
            if (!$result) {
                return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
            }

            $collection = new Collection;
            $collection->title = $input['collection_title'];
            $collection->user_id = $user->id;
            $collection->save();
        }

        else {
            $result = $validator->validate($input, 'update');
            if (!$result) {
                return Response::json(['message' => $validator->getErrorMessages()->first()], 400);
            }

            try {
                $collection = Collection::findOrFail(Input::get('collection_id'));
            }

            catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
                return Response::json(['message' => "Sorry, we couldn't find a collection to add the product to"], 404);
            }
        }

        if ($this->doesItemExistInCollection($item->id, $collection->id)) {
            $res = Response::json(['message' => 'You already have this product in your collection'], 400);
        } else {

            $collection->items()->save($item);
            $this->updateCollectionPreviewImage($collection, $item);

            $res = Response::json($collection->toArray(), 201);
        }

        return $res;
    }

    public function getCollection($id, $currentCollection = null) {

        $response = [];

        if (isset($currentCollection)) {
            $collection = $currentCollection;
        }
        else {
            $collection = Collection::findOrFail($id);
        }

        $response['items'] = $collection->items()->where('public', 1)->get()->toArray();
        $response['followers'] = $collection->users()->count();
        $response['user'] = $collection->user()->firstOrFail(['id', 'name', 'username'])->toArray();

        return Response::json($response);
    }

    public function getLikes($user_id) {
        $c = Collection::where('title', 'Likes')->where('user_id', $user_id)->firstOrFail();
        return $this->getCollection($c->id, $c);
    }

    /**
     * Update the specified resource in storage.
     * PUT /collections/{id}
     *
     * @param  int $id
     * @return Response
     */
    public function update($id)
{'fraction_non_alphanumeric': 0.16649439452969705, 'fraction_numerical': 0.006917388884471655, 'mean_word_length': 2.299580272822665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 168, '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': '5440928', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3307, 'n_words': 738}