Implement ticket creation and backlog
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Tickets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateProjectTicket
|
||||
{
|
||||
/**
|
||||
* @param array{
|
||||
* acceptance_criteria: string|null,
|
||||
* description: string|null,
|
||||
* ticket_priority_id: int,
|
||||
* ticket_type_id: int,
|
||||
* title: string
|
||||
* } $attributes
|
||||
*/
|
||||
public function handle(Project $project, array $attributes): Ticket
|
||||
{
|
||||
return DB::transaction(function () use ($attributes, $project): Ticket {
|
||||
$nextSequenceNumber = (int) Ticket::query()
|
||||
->where('project_id', $project->id)
|
||||
->max('sequence_number') + 1;
|
||||
|
||||
return Ticket::query()->create([
|
||||
'acceptance_criteria' => $attributes['acceptance_criteria'],
|
||||
'board_column_id' => null,
|
||||
'description' => $attributes['description'],
|
||||
'project_id' => $project->id,
|
||||
'sequence_number' => $nextSequenceNumber,
|
||||
'ticket_key' => "{$project->key}-{$nextSequenceNumber}",
|
||||
'ticket_priority_id' => $attributes['ticket_priority_id'],
|
||||
'ticket_type_id' => $attributes['ticket_type_id'],
|
||||
'title' => $attributes['title'],
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
||||
use App\Actions\Tickets\CreateProjectTicket;
|
||||
use App\Http\Requests\StoreProjectTicketRequest;
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ProjectBacklogController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CreateProjectTicket $createProjectTicket,
|
||||
private readonly ProjectPermissionService $projectPermissionService,
|
||||
) {}
|
||||
|
||||
public function index(Request $request, Project $project): Response
|
||||
{
|
||||
abort_unless(
|
||||
$this->projectPermissionService->userCanViewProject($request->user(), $project),
|
||||
403,
|
||||
);
|
||||
|
||||
return Inertia::render('projects/Backlog', [
|
||||
'canCreateTickets' => $this->projectPermissionService->userHasPermission($request->user(), $project, 'create'),
|
||||
'project' => $project,
|
||||
'status' => session('status'),
|
||||
'ticketPriorities' => $project->ticketPriorities()->get(),
|
||||
'ticketTypes' => $project->ticketTypes()->get(),
|
||||
'tickets' => Ticket::query()
|
||||
->with(['ticketPriority', 'ticketType'])
|
||||
->where('project_id', $project->id)
|
||||
->whereNull('board_column_id')
|
||||
->orderByDesc('sequence_number')
|
||||
->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreProjectTicketRequest $request, Project $project): RedirectResponse
|
||||
{
|
||||
abort_unless(
|
||||
$this->projectPermissionService->userHasPermission($request->user(), $project, 'create'),
|
||||
403,
|
||||
);
|
||||
|
||||
$ticket = $this->createProjectTicket->handle($project, $request->validated());
|
||||
|
||||
return to_route('projects.backlog.index', $project)
|
||||
->with('status', "{$ticket->ticket_key} was added to the backlog.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreProjectTicketRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var Project $project */
|
||||
$project = $this->route('project');
|
||||
|
||||
return [
|
||||
'acceptance_criteria' => ['nullable', 'string'],
|
||||
'description' => ['nullable', 'string'],
|
||||
'ticket_priority_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('ticket_priorities', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'ticket_type_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('ticket_types', 'id')->where('project_id', $project->id),
|
||||
],
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -46,4 +46,10 @@ class Project extends Model
|
||||
return $this->hasMany(TicketType::class)
|
||||
->orderBy('position');
|
||||
}
|
||||
|
||||
public function tickets(): HasMany
|
||||
{
|
||||
return $this->hasMany(Ticket::class)
|
||||
->orderByDesc('sequence_number');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\TicketFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'acceptance_criteria',
|
||||
'board_column_id',
|
||||
'description',
|
||||
'project_id',
|
||||
'sequence_number',
|
||||
'ticket_key',
|
||||
'ticket_priority_id',
|
||||
'ticket_type_id',
|
||||
'title',
|
||||
])]
|
||||
class Ticket extends Model
|
||||
{
|
||||
/** @use HasFactory<TicketFactory> */
|
||||
use HasFactory;
|
||||
|
||||
public function boardColumn(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(BoardColumn::class);
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function ticketPriority(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TicketPriority::class);
|
||||
}
|
||||
|
||||
public function ticketType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TicketType::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user