Initial commit of testapi
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\Department;
|
||||
|
||||
class DepartmentController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /Departments
|
||||
* Query params:
|
||||
* - departmentName (string)
|
||||
* - updatedSince (date-time)
|
||||
*/
|
||||
public function index(Request $request) {
|
||||
$q = Department::query();
|
||||
|
||||
if ($name = $request->query('departmentName')) {
|
||||
$q->where('name', $name);
|
||||
}
|
||||
|
||||
if ($since = $request->query('updatedSince')) {
|
||||
$q->where('updated_at', '>=', Carbon::parse($since));
|
||||
}
|
||||
|
||||
$out = $q->get()->map(function ($d) {
|
||||
return [
|
||||
'Id' => $d->id,
|
||||
'Name' => $d->name,
|
||||
'Updated' => $d->updated_at->toIso8601String().'Z',
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /Departments/{id}
|
||||
*/
|
||||
public function show(int $id) {
|
||||
$d = Department::findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'Id' => $d->id,
|
||||
'Name' => $d->name,
|
||||
'Updated' => $d->updated_at->toIso8601String().'Z',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user