add projet
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Calendar;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CalendarController extends BaseApiController
|
||||
{
|
||||
protected Calendar $calendar;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->calendar = model(Calendar::class);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
||||
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
||||
$month = $this->request->getGet('month');
|
||||
$year = $this->request->getGet('year');
|
||||
|
||||
$query = $this->calendar->newQuery();
|
||||
|
||||
if ($month && $year) {
|
||||
$query->whereMonth('start_date', $month)->whereYear('start_date', $year);
|
||||
} elseif ($year) {
|
||||
$query->whereYear('start_date', $year);
|
||||
}
|
||||
|
||||
$query->orderBy('start_date', 'ASC');
|
||||
|
||||
$result = $this->paginate($query, $page, $perPage);
|
||||
|
||||
return $this->success($result, 'Calendar events retrieved successfully');
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
$event = $this->calendar->find($id);
|
||||
if (!$event) {
|
||||
return $this->respondError('Event not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $this->success($event, 'Calendar event retrieved successfully');
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
$data = $this->payloadData();
|
||||
$rules = [
|
||||
'title' => 'required|string|max_length[255]',
|
||||
'description' => 'permit_empty|string',
|
||||
'start_date' => 'required|valid_date[Y-m-d]',
|
||||
'end_date' => 'permit_empty|valid_date[Y-m-d]',
|
||||
'location' => 'permit_empty|string|max_length[255]',
|
||||
];
|
||||
|
||||
$errors = $this->validateRequest($data, $rules);
|
||||
if (!empty($errors)) {
|
||||
return $this->respondValidationError($errors);
|
||||
}
|
||||
|
||||
$event = $this->calendar->create($data);
|
||||
|
||||
return $this->respondCreated([
|
||||
'id' => $event->id,
|
||||
'data' => $event,
|
||||
], 'Calendar event created successfully');
|
||||
}
|
||||
|
||||
public function update($id = null)
|
||||
{
|
||||
$event = $this->calendar->find($id);
|
||||
if (!$event) {
|
||||
return $this->respondError('Event not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$data = $this->payloadData();
|
||||
$rules = [
|
||||
'title' => 'permit_empty|string|max_length[255]',
|
||||
'description' => 'permit_empty|string',
|
||||
'start_date' => 'permit_empty|valid_date[Y-m-d]',
|
||||
'end_date' => 'permit_empty|valid_date[Y-m-d]',
|
||||
'location' => 'permit_empty|string|max_length[255]',
|
||||
];
|
||||
|
||||
$errors = $this->validateRequest($data, $rules);
|
||||
if (!empty($errors)) {
|
||||
return $this->respondValidationError($errors);
|
||||
}
|
||||
|
||||
$event->fill($data);
|
||||
$event->save();
|
||||
|
||||
return $this->success($event->fresh(), 'Calendar event updated successfully');
|
||||
}
|
||||
|
||||
public function delete($id = null)
|
||||
{
|
||||
$event = $this->calendar->find($id);
|
||||
if (!$event) {
|
||||
return $this->respondError('Event not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$event->delete();
|
||||
return $this->respondDeleted(null, 'Calendar event deleted successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user