| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | <?php
namespace App\Http\Controllers\Backend\ZooManagement;
use Illuminate\Http\Request;
use App\Http\Services\Backend\ZooManagement\TigerManagementService;
use App\Http\Controllers\Controller;
class TigerManagementController extends Controller
{
    
    // 相關私有服務層調用器宣告
    private $tigerManagementSv;
    
    public function __construct()
    {
        // 建構服務層調用器
        $this->tigerManagementSv = new TigerManagementService();
    }
    
    /**
     * 列表頁
     *   本端點非 API,於 controller 取列表值後,存於變量渲染到 view
     *   排序、分頁均未實現
     * @return array:          返回渲染頁
     */
    public function index()
    {
        // 取得參數與驗證
        // 服務層取得資料(以及實現各種業務上的邏輯)
        $tigers = $this->tigerManagementSv->getTigers();
        
        // 渲染
        return view('admin.ZooManagement.TigerManagement', [
            'operdata' => $tigers,
        ]);
    }
    
    /**
     * 新增老虎頁面
     *   本端點非 API,直接到 view
     * @return array:          返回渲染頁
     */
    public function create()
    {
        // 渲染
        return view('admin.ZooManagement.TigerManagementEdit', [
            'operdata' => "",
        ]);
    }
    
    /**
     * 編輯老虎頁面
     *   本端點非 API,依照變量取得資料後,渲染到 view
     *
     * @param  integer $id :    老虎的自增量
     *
     * @return array:          返回渲染頁
     */
    public function edit($id)
    {
        // 取得參數與驗證
        // 服務層取得資料(以及實現各種業務上的邏輯)
        $tiger = $this->tigerManagementSv->getTigerById($id);
        
        // 渲染
        return view('admin.ZooManagement.TigerManagementEdit', [
            'operdata' => $tiger,
        ]);
    }
    
    /**
     * 新增或編輯老虎的端點
     *   本端點非 API,依照參數新增或修改資料後,跳轉到既有的 view
     *
     * @param  Request $request :  整包參數
     *
     * @return array:             返回跳轉渲染頁的資訊
     */
    public function store(Request $request)
    {
        // 取得參數與驗證
        $mode = $request->mode;
        $id = ($request->mode == 'insert') ? '' : $request->id;
        $tigerName = $request->tigerName;
        // 服務層設置(以及實現各種業務上的邏輯)
        if ($mode == "insert") {
            // 新增模式
            $this->tigerManagementSv->insertTiger($tigerName);
        } else {
            // 編輯模式
            $this->tigerManagementSv->modifyTiger($id, $tigerName);
        }
        
        // 跳轉
        return redirect('/backend/zooManagement/tigerManagement');
    }
    
    /**
     * 刪除老虎的端點
     *   本端點非 API,依照參數刪除資料後,跳轉到既有的 view
     *
     * @param  integer $id :    老虎的自增量
     *
     * @return array:           返回跳轉渲染頁的資訊
     */
    public function delete($id)
    {
        // 取得參數與驗證
        // 服務層取得資料(以及實現各種業務上的邏輯)
        $this->tigerManagementSv->deleteTigerById($id);
    
        // 跳轉
        return Redirect::back();
    }
}
 |