<?php

namespace App\Api\Controllers;

use App\Models\Article;
use App\Models\Vote;
use Jason\Api;

class ArticleController extends Controller
{

    function index()
    {
        $files   = Article::where('category_id', 2)->select(['id', 'title', 'desc'])->get();

        foreach ($files as $file) {
            $file->is = $file->audit()->where('user_id', Api::id())->exists();
        }
        return $this->success([
            'user'    => Api::user(),
            'files'   => $files,
        ]);
    }
    function show(Article $article)
    {
        return $this->success([
            'article' => $article,
            'audited' => $article->audit()->where('user_id', Api::id())->exists(),
        ]);
    }

    function audit(Article $article)
    {
        $article->audit()->firstOrCreate([
            'user_id' => Api::id(),
        ]);

        return $this->success('审阅成功');
    }

}
