refactor: 规范工单接口代码规范

This commit is contained in:
xboard 2023-12-12 06:22:18 +08:00
parent 20466d07df
commit 1270a60ad8
4 changed files with 40 additions and 12 deletions

View File

@ -22,25 +22,20 @@ class TicketController extends Controller
if ($request->input('id')) {
$ticket = Ticket::where('id', $request->input('id'))
->where('user_id', $request->user['id'])
->first();
->first()
->load('message');
if (!$ticket) {
return $this->fail([400, __('Ticket does not exist')]);
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
if ($ticket['message'][$i]['user_id'] == $ticket->user_id) {
$ticket['message'][$i]['is_me'] = true;
} else {
$ticket['message'][$i]['is_me'] = false;
}
}
return $this->success($ticket);
$ticket['message']->each(function ($message) use ($ticket) {
$message['is_me'] = ($message['user_id'] == $ticket->user_id);
});
return $this->success(TicketResource::make($ticket)->additional(['message' => true]));
}
$ticket = Ticket::where('user_id', $request->user['id'])
->orderBy('created_at', 'DESC')
->get();
return $this->success(TicketResource::collection($ticket));
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class MessageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
"id" => $this['id'],
"ticket_id" => $this['ticket_id'],
"is_me" => $this['is_me'],
"message" => $this["message"],
"created_at" => $this['created_at'],
"updated_at" => $this['updated_at']
];
}
}

View File

@ -13,13 +13,14 @@ class TicketResource extends JsonResource
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
{
$data = [
"id" => $this['id'],
"level" => $this['level'],
"reply_status" => $this['reply_status'],
"status" => $this['status'],
"subject" => $this['subject'],
"message" => array_key_exists('message',$this->additional) ? MessageResource::collection($this['message']) : null,
"created_at" => $this['created_at'],
"updated_at" => $this['updated_at']
];

View File

@ -13,4 +13,10 @@ class Ticket extends Model
'created_at' => 'timestamp',
'updated_at' => 'timestamp'
];
public function message()
{
return $this->hasMany(TicketMessage::class, 'ticket_id', 'id');
}
}