Initial commit of testapi
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Carbon\Carbon;
|
||||
|
||||
use App\Models\Project;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /Projects
|
||||
* Query params:
|
||||
* - projectNumber (string)
|
||||
* - projectName (string)
|
||||
* - updatedSince (date-time)
|
||||
* - createdSince (date-time)
|
||||
*/
|
||||
public function index(Request $request) {
|
||||
$q = Project::with(['departments', 'profiles']);
|
||||
|
||||
if ($num = $request->query('projectNumber')) {
|
||||
$q->where('project_number', $num);
|
||||
}
|
||||
if ($name = $request->query('projectName')) {
|
||||
$q->where('project_name', $name);
|
||||
}
|
||||
if ($sinceUp = $request->query('updatedSince')) {
|
||||
$q->where('updated_at', '>=', Carbon::parse($sinceUp));
|
||||
}
|
||||
if ($sinceCr = $request->query('createdSince')) {
|
||||
$q->where('created_at', '>=', Carbon::parse($sinceCr));
|
||||
}
|
||||
|
||||
$out = $q->get()->map(function ($p) {
|
||||
return [
|
||||
'Id' => $p->id,
|
||||
'ProjectName' => $p->project_name,
|
||||
'ProjectNumber' => $p->project_number,
|
||||
'Departments' => $p->departments->pluck('id')->all(),
|
||||
'UserIds' => $p->profiles->pluck('id')->all(),
|
||||
'Updated' => $p->updated_at->toIso8601String().'Z',
|
||||
'Created' => $p->created_at->toIso8601String().'Z',
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /Projects/{id}
|
||||
*/
|
||||
public function show(int $id) {
|
||||
$p = Project::with(['departments', 'profiles'])->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'Id' => $p->id,
|
||||
'ProjectName' => $p->project_name,
|
||||
'ProjectNumber' => $p->project_number,
|
||||
'Departments' => $p->departments->pluck('id')->all(),
|
||||
'UserIds' => $p->profiles->pluck('id')->all(),
|
||||
'Updated' => $p->updated_at->toIso8601String().'Z',
|
||||
'Created' => $p->created_at->toIso8601String().'Z',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user