<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\plugins\notice\adminapi\controller;
use app\adminapi\controller\BaseAdminController;
use Applications\sample\plugins\notice\service\admin\NoticeService;
use Applications\sample\plugins\notice\validate\admin\NoticeValidator;
use Applications\sample\plugins\notice\validate\domain\NoticeDomainValidator;
use think\Request;
/**
 * 示例应用公告插件后台控制器
 * Class Notice
 * @package Applications\sample\plugins\notice\adminapi\controller
 */
class Notice extends BaseAdminController
{
    /**
     * 获取公告列表
     */
    public function list(Request $request)
    {
        $this->validate($request->get(), NoticeValidator::class . '.list');
        return json($this->service()->list($request));
    }
    /**
     * 保存公告（新增/编辑，自动识别场景）
     */
    public function save(Request $request)
    {
        $params = $request->post();
        $scene = !empty($params['id']) ? 'update' : 'create';
        $this->validate($params, NoticeValidator::class . '.' . $scene);
        $this->validateDomain($this->app->make(NoticeDomainValidator::class), $params, $scene);
        return json($this->service()->save($request));
    }
    /**
     * 删除公告
     */
    public function delete(Request $request)
    {
        $params = $request->post();
        $this->validate($params, NoticeValidator::class . '.delete');
        $this->validateDomain($this->app->make(NoticeDomainValidator::class), $params, 'delete');
        return json($this->service()->delete($request));
    }
    /**
     * 获取服务实例
     * @return NoticeService
     */
    private function service(): NoticeService
    {
        return $this->app->make(NoticeService::class);
    }
}
