add projet
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\Message;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class MessageController extends BaseApiController
|
||||
{
|
||||
protected Message $message;
|
||||
protected Configuration $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->message = model(Message::class);
|
||||
$this->config = model(Configuration::class);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
||||
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
||||
$type = $this->request->getGet('type');
|
||||
$read = $this->request->getGet('read');
|
||||
|
||||
$query = $this->message->newQuery();
|
||||
|
||||
if ($type === 'sent') {
|
||||
$query->where('sender_id', $user->id);
|
||||
} else {
|
||||
$query->where('recipient_id', $user->id);
|
||||
}
|
||||
|
||||
if ($read !== null) {
|
||||
$isRead = filter_var($read, FILTER_VALIDATE_BOOLEAN);
|
||||
$query->where('read_status', $isRead ? 1 : 0);
|
||||
}
|
||||
|
||||
$result = $this->paginate($query, $page, $perPage);
|
||||
|
||||
return $this->success($result, 'Messages retrieved successfully');
|
||||
}
|
||||
|
||||
public function show($id = null)
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$message = $this->message->newQuery()
|
||||
->where('id', $id)
|
||||
->where(function ($query) use ($user) {
|
||||
$query->where('sender_id', $user->id)
|
||||
->orWhere('recipient_id', $user->id);
|
||||
})
|
||||
->first();
|
||||
|
||||
if (!$message) {
|
||||
return $this->error('Message not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (($message->recipient_id ?? $message['recipient_id'] ?? null) === $user->id) {
|
||||
$readStatus = $message->read_status ?? $message['read_status'] ?? null;
|
||||
if ($readStatus === 0 || $readStatus === '0') {
|
||||
$this->message->update($message->id ?? $message['id'], [
|
||||
'read_status' => 1,
|
||||
'read_datetime'=> Carbon::now('UTC')->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
$message->read_status = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$payload = $message instanceof Model ? $message->toArray() : (array) $message;
|
||||
|
||||
return $this->success($payload, 'Message retrieved successfully');
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user) {
|
||||
return $this->respondError('Authentication required', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
$payload = $this->payloadData();
|
||||
if (empty($payload)) {
|
||||
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$rules = [
|
||||
'to' => 'required|integer',
|
||||
'subject' => 'required|max_length[255]',
|
||||
'body' => 'required',
|
||||
];
|
||||
|
||||
$errors = $this->validateRequest($payload, $rules);
|
||||
if (!empty($errors)) {
|
||||
return $this->respondValidationError($errors);
|
||||
}
|
||||
|
||||
$schoolYear = $this->config->getConfig('school_year');
|
||||
$semester = $this->config->getConfig('semester');
|
||||
|
||||
$maxMessageNumber = $this->message->newQuery()
|
||||
->selectRaw('COALESCE(MAX(message_number), 0) AS message_number')
|
||||
->first();
|
||||
$messageNumber = (int) ($maxMessageNumber->message_number ?? $maxMessageNumber['message_number'] ?? 0) + 1;
|
||||
|
||||
$messageData = [
|
||||
'sender_id' => $user->id,
|
||||
'recipient_id' => (int) $payload['to'],
|
||||
'subject' => $payload['subject'],
|
||||
'message' => $payload['body'],
|
||||
'sent_datetime' => Carbon::now('UTC')->format('Y-m-d H:i:s'),
|
||||
'read_status' => 0,
|
||||
'message_number'=> $messageNumber,
|
||||
'priority' => $payload['priority'] ?? 'normal',
|
||||
'status' => 'sent',
|
||||
'semester' => $semester,
|
||||
'school_year' => $schoolYear,
|
||||
];
|
||||
|
||||
try {
|
||||
$message = $this->message->create($messageData);
|
||||
|
||||
return $this->success($message->toArray(), 'Message sent successfully', Response::HTTP_CREATED);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Message send error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to send message', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user