Skip to content

Commit

Permalink
Merge pull request #94 from HiEventsDev/develop
Browse files Browse the repository at this point in the history
main <- develop
  • Loading branch information
daveearley authored Jul 12, 2024
2 parents 685ef32 + 9d78493 commit 7c2801d
Show file tree
Hide file tree
Showing 43 changed files with 923 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class QuestionDomainObjectAbstract extends \HiEvents\DomainObjects\Abst
final public const DELETED_AT = 'deleted_at';
final public const ORDER = 'order';
final public const IS_HIDDEN = 'is_hidden';
final public const DESCRIPTION = 'description';

protected int $id;
protected int $event_id;
Expand All @@ -35,6 +36,7 @@ abstract class QuestionDomainObjectAbstract extends \HiEvents\DomainObjects\Abst
protected ?string $deleted_at = null;
protected int $order = 1;
protected bool $is_hidden = false;
protected ?string $description = null;

public function toArray(): array
{
Expand All @@ -51,6 +53,7 @@ public function toArray(): array
'deleted_at' => $this->deleted_at ?? null,
'order' => $this->order ?? null,
'is_hidden' => $this->is_hidden ?? null,
'description' => $this->description ?? null,
];
}

Expand Down Expand Up @@ -185,4 +188,15 @@ public function getIsHidden(): bool
{
return $this->is_hidden;
}

public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}

public function getDescription(): ?string
{
return $this->description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace HiEvents\DomainObjects;

use HiEvents\DomainObjects\Enums\QuestionTypeEnum;

/**
* As this is related to a view, and not a table, this was not auto-generated.
*/
Expand Down
7 changes: 6 additions & 1 deletion backend/app/Http/Actions/Questions/CreateQuestionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ public function __invoke(UpsertQuestionRequest $request, int $eventId): JsonResp
'ticket_ids' => $request->input('ticket_ids'),
'belongs_to' => $request->input('belongs_to'),
'is_hidden' => $request->boolean('is_hidden'),
'description' => $request->input('description'),
]));

return $this->resourceResponse(QuestionResource::class, $question, ResponseCodes::HTTP_CREATED);
return $this->resourceResponse(
resource: QuestionResource::class,
data: $question,
statusCode: ResponseCodes::HTTP_CREATED
);
}
}
27 changes: 17 additions & 10 deletions backend/app/Http/Actions/Questions/EditQuestionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use HiEvents\Services\Handlers\Question\DTO\UpsertQuestionDTO;
use HiEvents\Services\Handlers\Question\EditQuestionHandler;
use Illuminate\Http\JsonResponse;
use Throwable;

class EditQuestionAction extends BaseAction
{
Expand All @@ -21,20 +22,26 @@ public function __construct(EditQuestionHandler $editQuestionHandler)
$this->editQuestionHandler = $editQuestionHandler;
}

/**
* @throws Throwable
*/
public function __invoke(UpsertQuestionRequest $request, int $eventId, int $questionId): JsonResponse
{
$this->isActionAuthorized($eventId, EventDomainObject::class);

$question = $this->editQuestionHandler->handle($questionId, UpsertQuestionDTO::fromArray([
'title' => $request->input('title'),
'type' => QuestionTypeEnum::fromName($request->input('type')),
'required' => $request->boolean('required'),
'options' => $request->input('options'),
'event_id' => $eventId,
'ticket_ids' => $request->input('ticket_ids'),
'is_hidden' => $request->boolean('is_hidden'),
'belongs_to' => QuestionBelongsTo::fromName($request->input('belongs_to')),
]));
$question = $this->editQuestionHandler->handle(
questionId: $questionId,
createQuestionDTO: UpsertQuestionDTO::fromArray([
'title' => $request->input('title'),
'type' => QuestionTypeEnum::fromName($request->input('type')),
'required' => $request->boolean('required'),
'options' => $request->input('options'),
'event_id' => $eventId,
'ticket_ids' => $request->input('ticket_ids'),
'is_hidden' => $request->boolean('is_hidden'),
'belongs_to' => QuestionBelongsTo::fromName($request->input('belongs_to')),
'description' => $request->input('description'),
]));

return $this->resourceResponse(QuestionResource::class, $question);
}
Expand Down
3 changes: 2 additions & 1 deletion backend/app/Http/Request/Questions/UpsertQuestionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace HiEvents\Http\Request\Questions;

use Illuminate\Validation\Rule;
use HiEvents\DomainObjects\Enums\QuestionBelongsTo;
use HiEvents\DomainObjects\Enums\QuestionTypeEnum;
use HiEvents\Http\Request\BaseRequest;
use Illuminate\Validation\Rule;

class UpsertQuestionRequest extends BaseRequest
{
public function rules(): array
{
return [
'title' => ['string', 'required'],
'description' => ['string', 'nullable', 'max:10000'],
'type' => ['required', Rule::in(QuestionTypeEnum::valuesArray())],
'ticket_ids' => ['array', 'required_if:belongs_to,TICKET'],
'belongs_to' => [
Expand Down
4 changes: 2 additions & 2 deletions backend/app/Resources/Question/QuestionAnswerViewResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace HiEvents\Resources\Question;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use HiEvents\DomainObjects\Enums\QuestionTypeEnum;
use HiEvents\DomainObjects\QuestionAndAnswerViewDomainObject;
use HiEvents\Services\Domain\Question\QuestionAnswerFormatter;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin QuestionAndAnswerViewDomainObject
Expand Down
1 change: 1 addition & 0 deletions backend/app/Resources/Question/QuestionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function toArray(Request $request): array
'id' => $this->getId(),
'type' => $this->getType(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'options' => $this->getOptions(),
'required' => $this->getRequired(),
'event_id' => $this->getEventId(),
Expand Down
4 changes: 2 additions & 2 deletions backend/app/Resources/Question/QuestionResourcePublic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace HiEvents\Resources\Question;

use Illuminate\Http\Request;
use HiEvents\DomainObjects\QuestionDomainObject;
use HiEvents\Resources\BaseResource;
use HiEvents\Resources\Ticket\TicketResourcePublic;
use Illuminate\Http\Request;

/**
* @mixin QuestionDomainObject
Expand All @@ -18,6 +17,7 @@ public function toArray(Request $request): array
'id' => $this->getId(),
'type' => $this->getType(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'options' => $this->getOptions(),
'required' => $this->getRequired(),
'event_id' => $this->getEventId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
use HiEvents\DomainObjects\Generated\QuestionDomainObjectAbstract;
use HiEvents\DomainObjects\QuestionDomainObject;
use HiEvents\Repository\Interfaces\QuestionRepositoryInterface;
use HTMLPurifier;
use Illuminate\Database\DatabaseManager;
use Throwable;

class CreateQuestionService
{
public function __construct(
private readonly QuestionRepositoryInterface $questionRepository,
private readonly DatabaseManager $databaseManager
private readonly DatabaseManager $databaseManager,
private readonly HTMLPurifier $purifier,
)
{
}
Expand All @@ -33,6 +35,7 @@ public function createQuestion(
QuestionDomainObjectAbstract::REQUIRED => $question->getRequired(),
QuestionDomainObjectAbstract::OPTIONS => $question->getOptions(),
QuestionDomainObjectAbstract::IS_HIDDEN => $question->getIsHidden(),
QuestionDomainObjectAbstract::DESCRIPTION => $this->purifier->purify($question->getDescription()),
], $ticketIds));
}
}
53 changes: 53 additions & 0 deletions backend/app/Services/Domain/Question/EditQuestionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace HiEvents\Services\Domain\Question;

use HiEvents\DomainObjects\Generated\QuestionDomainObjectAbstract;
use HiEvents\DomainObjects\QuestionDomainObject;
use HiEvents\DomainObjects\TicketDomainObject;
use HiEvents\Repository\Interfaces\QuestionRepositoryInterface;
use HTMLPurifier;
use Illuminate\Database\DatabaseManager;
use Throwable;

class EditQuestionService
{
public function __construct(
private readonly QuestionRepositoryInterface $questionRepository,
private readonly DatabaseManager $databaseManager,
private readonly HTMLPurifier $purifier,
)
{
}

/**
* @throws Throwable
*/
public function editQuestion(
QuestionDomainObject $question,
array $ticketIds,
): QuestionDomainObject
{
return $this->databaseManager->transaction(function () use ($question, $ticketIds) {
$this->questionRepository->updateQuestion(
questionId: $question->getId(),
eventId: $question->getEventId(),
attributes: [
QuestionDomainObjectAbstract::TITLE => $question->getTitle(),
QuestionDomainObjectAbstract::EVENT_ID => $question->getEventId(),
QuestionDomainObjectAbstract::BELONGS_TO => $question->getBelongsTo(),
QuestionDomainObjectAbstract::TYPE => $question->getType(),
QuestionDomainObjectAbstract::REQUIRED => $question->getRequired(),
QuestionDomainObjectAbstract::OPTIONS => $question->getOptions(),
QuestionDomainObjectAbstract::IS_HIDDEN => $question->getIsHidden(),
QuestionDomainObjectAbstract::DESCRIPTION => $this->purifier->purify($question->getDescription()),
],
ticketIds: $ticketIds
);

return $this->questionRepository
->loadRelation(TicketDomainObject::class)
->findById($question->getId());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getAnswerAsText(string|array $answer, QuestionTypeEnum $question
$answer['country'] ?? null,
];

return implode(', ', array_filter($addressLines, function ($line) {
return implode(', ', array_filter($addressLines, static function ($line) {
return !empty($line);
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use HiEvents\DomainObjects\QuestionDomainObject;
use HiEvents\Services\Domain\Question\CreateQuestionService;
use HiEvents\Services\Handlers\Question\DTO\UpsertQuestionDTO;
use HTMLPurifier;
use Throwable;

class CreateQuestionHandler
{
public function __construct(
private readonly CreateQuestionService $createQuestionService,
private readonly HTMLPurifier $purifier,
)
{
}
Expand All @@ -27,7 +29,8 @@ public function handle(UpsertQuestionDTO $createQuestionDTO): QuestionDomainObje
->setType($createQuestionDTO->type->name)
->setRequired($createQuestionDTO->required)
->setOptions($createQuestionDTO->options)
->setIsHidden($createQuestionDTO->is_hidden);
->setIsHidden($createQuestionDTO->is_hidden)
->setDescription($this->purifier->purify($createQuestionDTO->description));

return $this->createQuestionService->createQuestion(
$question,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public array $ticket_ids,
public bool $is_hidden,
public QuestionBelongsTo $belongs_to,
public ?string $description = null,
)
{
}
Expand Down
46 changes: 23 additions & 23 deletions backend/app/Services/Handlers/Question/EditQuestionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@

namespace HiEvents\Services\Handlers\Question;

use HiEvents\DomainObjects\Generated\QuestionDomainObjectAbstract;
use HiEvents\DomainObjects\QuestionDomainObject;
use HiEvents\DomainObjects\TicketDomainObject;
use HiEvents\Repository\Eloquent\QuestionRepository;
use HiEvents\Repository\Interfaces\QuestionRepositoryInterface;
use HiEvents\Services\Domain\Question\EditQuestionService;
use HiEvents\Services\Handlers\Question\DTO\UpsertQuestionDTO;
use Illuminate\Support\Facades\DB;
use Throwable;

readonly class EditQuestionHandler
class EditQuestionHandler
{
private QuestionRepository $questionRepository;
public function __construct(
private readonly EditQuestionService $editQuestionService,

public function __construct(QuestionRepositoryInterface $questionRepository)
)
{
$this->questionRepository = $questionRepository;
}

/**
* @throws Throwable
*/
public function handle(int $questionId, UpsertQuestionDTO $createQuestionDTO): QuestionDomainObject
{
return DB::transaction(function () use ($questionId, $createQuestionDTO) {
$this->questionRepository->updateQuestion($questionId, $createQuestionDTO->event_id, [
QuestionDomainObjectAbstract::TITLE => $createQuestionDTO->title,
QuestionDomainObjectAbstract::BELONGS_TO => $createQuestionDTO->belongs_to->name,
QuestionDomainObjectAbstract::TYPE => $createQuestionDTO->type->name,
QuestionDomainObjectAbstract::REQUIRED => $createQuestionDTO->required,
QuestionDomainObjectAbstract::OPTIONS => $createQuestionDTO->options,
QuestionDomainObjectAbstract::IS_HIDDEN => $createQuestionDTO->is_hidden
$question = (new QuestionDomainObject())
->setId($questionId)
->setTitle($createQuestionDTO->title)
->setEventId($createQuestionDTO->event_id)
->setBelongsTo($createQuestionDTO->belongs_to->name)
->setType($createQuestionDTO->type->name)
->setRequired($createQuestionDTO->required)
->setOptions($createQuestionDTO->options)
->setIsHidden($createQuestionDTO->is_hidden)
->setDescription($createQuestionDTO->description);

], $createQuestionDTO->ticket_ids);

return $this->questionRepository
->loadRelation(TicketDomainObject::class)
->findById($questionId);
});
return $this->editQuestionService->editQuestion(
question: $question,
ticketIds: $createQuestionDTO->ticket_ids,
);
}
}
3 changes: 2 additions & 1 deletion backend/app/Validators/Rules/AttendeeQuestionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace HiEvents\Validators\Rules;

use HiEvents\DomainObjects\QuestionDomainObject;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use HiEvents\DomainObjects\QuestionDomainObject;

class AttendeeQuestionRule extends BaseQuestionRule
{
Expand All @@ -20,6 +20,7 @@ protected function validateRequiredQuestionArePresent(Collection $orderAttendees
$requiredQuestionIds = $this->questions
->filter(function (QuestionDomainObject $question) use ($ticketId) {
return $question->getRequired()
&& !$question->getIsHidden()
&& $question->getTickets()?->map(fn($ticket) => $ticket->getId())->contains($ticketId);
})
->map(fn(QuestionDomainObject $question) => $question->getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('questions', static function (Blueprint $table) {
$table->text('description')->nullable();
});
}

public function down(): void
{
Schema::table('questions', static function (Blueprint $table) {
$table->dropColumn('description');
});
}
};
Loading

0 comments on commit 7c2801d

Please sign in to comment.