@@ -0,0 +1,150 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Controllers\BaseController;
|
||||||
|
use App\Models\AssignmentModel;
|
||||||
|
use CodeIgniter\API\ResponseTrait;
|
||||||
|
use CodeIgniter\HTTP\RequestInterface;
|
||||||
|
use CodeIgniter\HTTP\ResponseInterface;
|
||||||
|
|
||||||
|
class AssignmentController extends BaseController
|
||||||
|
{
|
||||||
|
use ResponseTrait;
|
||||||
|
|
||||||
|
protected $assignmentModel;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->assignmentModel = new AssignmentModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRequest(RequestInterface $request): void
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$page = (int) ($this->request->getGet('page') ?? 1);
|
||||||
|
$perPage = (int) ($this->request->getGet('per_page') ?? 20);
|
||||||
|
|
||||||
|
foreach (['class_id', 'teacher_id', 'student_id'] as $filter) {
|
||||||
|
$value = $this->request->getGet($filter);
|
||||||
|
|
||||||
|
if ($value !== null && $value !== '') {
|
||||||
|
$this->assignmentModel->where($filter, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$assignments = $this->assignmentModel->paginate(max(1, $perPage), 'default', max(1, $page));
|
||||||
|
$pager = $this->assignmentModel->pager ?? null;
|
||||||
|
|
||||||
|
return $this->respond([
|
||||||
|
'data' => $assignments,
|
||||||
|
'pagination' => [
|
||||||
|
'current_page' => $pager ? (int) $pager->getCurrentPage() : max(1, $page),
|
||||||
|
'per_page' => $pager ? (int) $pager->getPerPage() : max(1, $perPage),
|
||||||
|
'total' => $pager ? (int) $pager->getTotal() : count($assignments),
|
||||||
|
'total_pages' => $pager ? (int) $pager->getPageCount() : 1,
|
||||||
|
],
|
||||||
|
], ResponseInterface::HTTP_OK, 'Assignments retrieved successfully');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(int $id)
|
||||||
|
{
|
||||||
|
$assignment = $this->assignmentModel->find($id);
|
||||||
|
|
||||||
|
if (! $assignment) {
|
||||||
|
return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respond($assignment, ResponseInterface::HTTP_OK, 'Assignment retrieved successfully');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store()
|
||||||
|
{
|
||||||
|
$payload = $this->request->getJSON(true) ?? [];
|
||||||
|
$rules = [
|
||||||
|
'title' => 'required|string|max_length[255]',
|
||||||
|
'due_date' => 'required|valid_date',
|
||||||
|
'class_id' => 'required|integer',
|
||||||
|
'teacher_id' => 'required|integer',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (! $this->validate($rules)) {
|
||||||
|
return [
|
||||||
|
'status' => false,
|
||||||
|
'code' => ResponseInterface::HTTP_UNPROCESSABLE_ENTITY,
|
||||||
|
'message' => 'Validation failed',
|
||||||
|
'errors' => $this->validator->getErrors(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = $this->assignmentModel->insert($payload);
|
||||||
|
|
||||||
|
return $this->respond(
|
||||||
|
$this->assignmentModel->find($id),
|
||||||
|
ResponseInterface::HTTP_CREATED,
|
||||||
|
'Assignment created successfully'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(int $id)
|
||||||
|
{
|
||||||
|
if (! $this->assignmentModel->find($id)) {
|
||||||
|
return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = $this->request->getJSON(true) ?? [];
|
||||||
|
$rules = [
|
||||||
|
'title' => 'permit_empty|string|max_length[255]',
|
||||||
|
'description' => 'permit_empty|string',
|
||||||
|
'due_date' => 'permit_empty|valid_date',
|
||||||
|
'class_id' => 'permit_empty|integer',
|
||||||
|
'teacher_id' => 'permit_empty|integer',
|
||||||
|
'student_id' => 'permit_empty|integer',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (! $this->validate($rules)) {
|
||||||
|
return [
|
||||||
|
'status' => false,
|
||||||
|
'code' => ResponseInterface::HTTP_UNPROCESSABLE_ENTITY,
|
||||||
|
'message' => 'Validation failed',
|
||||||
|
'errors' => $this->validator->getErrors(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assignmentModel->update($id, $payload);
|
||||||
|
|
||||||
|
return $this->respond(
|
||||||
|
$this->assignmentModel->find($id),
|
||||||
|
ResponseInterface::HTTP_OK,
|
||||||
|
'Assignment updated successfully'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete(int $id)
|
||||||
|
{
|
||||||
|
if (! $this->assignmentModel->find($id)) {
|
||||||
|
return $this->respond(null, ResponseInterface::HTTP_NOT_FOUND, 'Assignment not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assignmentModel->delete($id);
|
||||||
|
|
||||||
|
return $this->respond(null, ResponseInterface::HTTP_OK, 'Assignment deleted successfully');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function student(int $studentId)
|
||||||
|
{
|
||||||
|
$assignments = $this->assignmentModel
|
||||||
|
->where('student_id', $studentId)
|
||||||
|
->findAll();
|
||||||
|
|
||||||
|
return $this->respond(
|
||||||
|
$assignments,
|
||||||
|
ResponseInterface::HTTP_OK,
|
||||||
|
'Assignments retrieved successfully for student'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use CodeIgniter\Model;
|
||||||
|
|
||||||
|
class AssignmentModel extends Model
|
||||||
|
{
|
||||||
|
protected $table = 'assignments';
|
||||||
|
protected $primaryKey = 'id';
|
||||||
|
protected $useAutoIncrement = true;
|
||||||
|
protected $returnType = 'array';
|
||||||
|
protected $useSoftDeletes = false;
|
||||||
|
protected $protectFields = true;
|
||||||
|
protected $allowedFields = [
|
||||||
|
'title',
|
||||||
|
'description',
|
||||||
|
'due_date',
|
||||||
|
'class_id',
|
||||||
|
'teacher_id',
|
||||||
|
'student_id',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected bool $allowEmptyInserts = false;
|
||||||
|
protected bool $updateOnlyChanged = true;
|
||||||
|
|
||||||
|
protected $useTimestamps = true;
|
||||||
|
protected $dateFormat = 'datetime';
|
||||||
|
protected $createdField = 'created_at';
|
||||||
|
protected $updatedField = 'updated_at';
|
||||||
|
protected $deletedField = 'deleted_at';
|
||||||
|
|
||||||
|
protected $validationRules = [
|
||||||
|
'title' => 'required|string|max_length[255]',
|
||||||
|
'due_date' => 'required|valid_date',
|
||||||
|
'class_id' => 'required|integer',
|
||||||
|
'teacher_id' => 'required|integer',
|
||||||
|
];
|
||||||
|
protected $validationMessages = [];
|
||||||
|
protected $skipValidation = false;
|
||||||
|
protected $cleanValidationRules = true;
|
||||||
|
protected $allowCallbacks = true;
|
||||||
|
protected $beforeInsert = [];
|
||||||
|
protected $afterInsert = [];
|
||||||
|
protected $beforeUpdate = [];
|
||||||
|
protected $afterUpdate = [];
|
||||||
|
protected $beforeFind = [];
|
||||||
|
protected $afterFind = [];
|
||||||
|
protected $beforeDelete = [];
|
||||||
|
protected $afterDelete = [];
|
||||||
|
}
|
||||||
+3
-1
@@ -33,7 +33,9 @@
|
|||||||
"Config\\": "app/Config/"
|
"Config\\": "app/Config/"
|
||||||
},
|
},
|
||||||
"exclude-from-classmap": [
|
"exclude-from-classmap": [
|
||||||
"**/Database/Migrations/**"
|
"**/Database/Migrations/**",
|
||||||
|
"app/ThirdParty/fpdf/",
|
||||||
|
"app/Views/invoice_payment/pdf_template.php"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class FakeGetRequest extends MockIncomingRequest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StubPager
|
class AssignmentStubPager
|
||||||
{
|
{
|
||||||
public function __construct(private readonly int $current, private readonly int $perPage, private readonly int $total)
|
public function __construct(private readonly int $current, private readonly int $perPage, private readonly int $total)
|
||||||
{
|
{
|
||||||
@@ -84,7 +84,7 @@ class StubAssignmentModel
|
|||||||
|
|
||||||
public function paginate(int $perPage, string $group, int $page)
|
public function paginate(int $perPage, string $group, int $page)
|
||||||
{
|
{
|
||||||
$this->pager = new StubPager($this->pagerData['current'], $this->pagerData['perPage'], $this->pagerData['total']);
|
$this->pager = new AssignmentStubPager($this->pagerData['current'], $this->pagerData['perPage'], $this->pagerData['total']);
|
||||||
return $this->findAllResult;
|
return $this->findAllResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user