Your IP : 18.119.157.241


Current Path : /home/lentoinv/test.api.lentoria.com/app/Http/Controllers/
Upload File :
Current File : /home/lentoinv/test.api.lentoria.com/app/Http/Controllers/TypeController.php

<?php

namespace App\Http\Controllers;

use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class TypeController extends Controller
{
    function createCourseType(Request $request)
    {
        $val = Validator::make($request->all(), [
            'type' => 'required|string|unique:types,type',
            'description' => 'required|string'
        ]);

        if ($val->fails()) {
            return response(['errors' => $val->errors()->all()], 422);
        }

        Type::create([
            'type' => $request->type,
            'description' => $request->description
        ]);

        return response([
            'message' => 'Type has been added sucessfuly'
        ], 200);
    }

    function fetchTypes()
    {
        $types = Type::all();
        return response([
            'message' => $types
        ], 200);
    }


    function fetchTypesAdmin()
    {
        $types = Type::withCount('courses')->get();
        return response([
            'data' => $types
        ], 200);
    }

    function updateType(Request $request)
    {
        $val = Validator::make($request->all(), [
            'type_id' => 'required|exists:types,id',
            'type' => 'required|string|unique:types,type',
            'description' => 'required|string'
        ]);

        if ($val->fails()) {
            return response(['errors' => $val->errors()->all()], 422);
        }

        Type::where(['id' => $request->type_id])->update([
            'type' => $request->type,
            'description' => $request->description
        ]);

        return response([
            'message' => 'Type has been updated sucessfuly'
        ], 200);
    }
}

?>