<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-10-5
 * Time: 下午3:18
 */

namespace App\Http\Controllers\Api\V1;

use App\Models\Collection;

class CollectionController extends AuthController
{
    public function submitCollect()
    {
        $req = request()->post();
        $this->validate(request(), [
            'type' => 'required|in:1,2',
            'relation_id' => 'required|integer',
        ]);
        $user = $this->user;

        $map = [1 => 'docter_id', 2 => 'article_id'];
        if (Collection::where('user_id', $user['id'])->where($map[$req['type']], $req['relation_id'])->count()) {
            Collection::where('user_id', $user['id'])->where($map[$req['type']], $req['relation_id'])->delete();
            $is_collect = 0;
        }
        else {
            Collection::create([
                'user_id' => $user['id'],
                $map[$req['type']] => $req['relation_id']
            ]);
            $is_collect = 1;
        }

        return out(['is_collect' => $is_collect]);
    }

    public function collectList()
    {
        $req = request()->post();
        $this->validate(request(), [
            'type' => 'required|in:1,2',
        ]);
        $user = $this->user;

        $map = [1 => 'docter_id', 2 => 'article_id'];
        $data = Collection::with(['docter', 'article'])->where('user_id', $user['id'])->where($map[$req['type']], '>', 0)->paginate();

        return out($data);
    }
}
