forked from admin/services_core
CRUD Ticket Insidace Type
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Ticket;
|
||||
|
||||
use App\Http\Controllers\ApiController;
|
||||
use App\Models\TicketIncidentType;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Services\Ticket\TicketIncidentTypeService;
|
||||
use App\Http\Resources\TicketIncidentType\TicketIncidentTypeResource;
|
||||
use App\Http\Requests\TicketIncidentType\StoreTicketIncidentTypeRequest;
|
||||
use App\Http\Requests\TicketIncidentType\UpdateTicketIncidentTypeRequest;
|
||||
|
||||
class TicketIncidentTypeController extends ApiController
|
||||
{
|
||||
public function __construct(
|
||||
private TicketIncidentTypeService $service
|
||||
) {}
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return $this->success(
|
||||
TicketIncidentTypeResource::collection(
|
||||
$this->service->getAll()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function store(
|
||||
StoreTicketIncidentTypeRequest $request
|
||||
): JsonResponse {
|
||||
$incidentType = $this->service->create(
|
||||
$request->validated()
|
||||
);
|
||||
|
||||
return $this->created(
|
||||
new TicketIncidentTypeResource($incidentType),
|
||||
'Incident Type berhasil dibuat'
|
||||
);
|
||||
}
|
||||
|
||||
public function show(
|
||||
TicketIncidentType $ticketIncidentType
|
||||
): JsonResponse {
|
||||
return $this->success(
|
||||
new TicketIncidentTypeResource(
|
||||
$ticketIncidentType
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function update(
|
||||
UpdateTicketIncidentTypeRequest $request,
|
||||
TicketIncidentType $ticketIncidentType
|
||||
): JsonResponse {
|
||||
$incidentType = $this->service->update(
|
||||
$ticketIncidentType,
|
||||
$request->validated()
|
||||
);
|
||||
|
||||
return $this->updated(
|
||||
new TicketIncidentTypeResource($incidentType),
|
||||
'Incident Type berhasil diupdate'
|
||||
);
|
||||
}
|
||||
|
||||
public function destroy(
|
||||
TicketIncidentType $ticketIncidentType
|
||||
): JsonResponse {
|
||||
$this->service->delete(
|
||||
$ticketIncidentType
|
||||
);
|
||||
|
||||
return $this->deleted(
|
||||
'Incident Type berhasil dihapus'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\TicketIncidentType;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreTicketIncidentTypeRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
'unique:ticket_incident_types,name'
|
||||
],
|
||||
'is_active' => [
|
||||
'sometimes',
|
||||
'boolean'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\TicketIncidentType;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateTicketIncidentTypeRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('ticket_incident_types', 'name')
|
||||
->ignore($this->route('ticketIncidentType'))
|
||||
],
|
||||
'is_active' => [
|
||||
'required',
|
||||
'boolean'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\TicketIncidentType;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class TicketIncidentTypeResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'is_active' => $this->is_active,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class TicketIncidentType extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'is_active'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\TicketIncidentType;
|
||||
|
||||
class TicketIncidentTypeService
|
||||
{
|
||||
public function getAll()
|
||||
{
|
||||
return TicketIncidentType::orderBy('name')->get();
|
||||
}
|
||||
|
||||
public function create(array $data): TicketIncidentType
|
||||
{
|
||||
return TicketIncidentType::create([
|
||||
'name' => $data['name'],
|
||||
'is_active' => $data['is_active'] ?? true
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(
|
||||
TicketIncidentType $ticketIncidentType,
|
||||
array $data
|
||||
): TicketIncidentType {
|
||||
$ticketIncidentType->update([
|
||||
'name' => $data['name'],
|
||||
'is_active' => $data['is_active']
|
||||
]);
|
||||
|
||||
return $ticketIncidentType->fresh();
|
||||
}
|
||||
|
||||
public function delete(TicketIncidentType $ticketIncidentType): void
|
||||
{
|
||||
$ticketIncidentType->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user