1
0

CRUD Ticket Insidace Type

This commit is contained in:
Wian Drs
2026-06-25 13:06:24 +07:00
parent faab64e0dd
commit ad472a9880
10 changed files with 338 additions and 0 deletions
@@ -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,
];
}
}
+20
View File
@@ -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();
}
}
@@ -0,0 +1,29 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('ticket_incident_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_active')->default(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('ticket_incident_types');
}
};
@@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('ticket_types', function (Blueprint $table) {
$table->boolean('need_incident_type')
->default(false)
->after('name');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('ticket_types', function (Blueprint $table) {
$table->dropColumn('need_incident_type');
});
}
};
@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('ticket_incident_types', function (Blueprint $table) {
$table->boolean('deleted')
->default(false)
->after('is_active');
$table->timestamp('deleted_at')
->nullable()
->after('deleted');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('ticket_incident_types', function (Blueprint $table) {
$table->dropColumn([
'deleted',
'deleted_at'
]);
});
}
};
+3
View File
@@ -6,6 +6,7 @@ use App\Http\Controllers\AuthController;
use App\Http\Controllers\Ticket\TicketController;
use App\Http\Controllers\Ticket\TicketTypeController;
use App\Http\Controllers\Ticket\TicketWorkflowController;
use App\Http\Controllers\Ticket\TicketIncidentTypeController;
use App\Http\Controllers\Material\MaterialController;
Route::post('/login', [AuthController::class, 'login']);
@@ -30,6 +31,7 @@ Route::middleware('auth:sanctum')->group(function () {
Route::apiResource('tickets', TicketController::class);
Route::apiResource('ticket-types', TicketTypeController::class);
Route::apiResource('ticket-incident-types', TicketIncidentTypeController::class);
Route::prefix('tickets/{ticket}')
->controller(TicketWorkflowController::class)
@@ -67,5 +69,6 @@ Route::middleware('auth:sanctum')->group(function () {
Route::get('/history/{barcode_id}', [MaterialController::class, 'history']);
});
});