Skip to content

Commit

Permalink
Save many screens (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Dec 12, 2024
1 parent 21efcea commit 1aaa3b7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 49 deletions.
16 changes: 5 additions & 11 deletions src/Dto/ConsultationSaveScreenDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class ConsultationSaveScreenDto extends BaseDto
protected int $consultationId;
protected int $userTerminId;
protected string $userEmail;
protected UploadedFile|string $file;
protected string $timestamp;
protected string $executedAt;
protected array $files;

public function getConsultationId(): int
{
Expand All @@ -28,18 +27,13 @@ public function getUserEmail(): string
return $this->userEmail;
}

public function getFile(): string|UploadedFile
{
return $this->file;
}

public function getTimestamp(): string
public function getExecutedAt(): string
{
return $this->timestamp;
return $this->executedAt;
}

public function getExecutedAt(): string
public function setFiles(array $files): void
{
return $this->executedAt;
$this->files = $files;
}
}
24 changes: 15 additions & 9 deletions src/Http/Controllers/Swagger/ConsultationAPISwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,15 +646,21 @@ public function schedule(ScheduleConsultationAPIRequest $scheduleConsultationAPI
* example="2024-10-04 12:02:12"
* ),
* @OA\Property(
* property="file",
* type="string",
* format="binary"
* ),
* @OA\Property(
* property="executed_at",
* type="string",
* example="2024-10-04 12:02:12"
* )
* property="files",
* type="array",
* @OA\Items(
* @OA\Property(
* property="file",
* type="string",
* format="binary"
* ),
* @OA\Property(
* property="timestamp",
* type="string",
* example="2024-10-04 12:02:12"
* )
* )
* )
* )
* )
* ),
Expand Down
11 changes: 6 additions & 5 deletions src/Http/Requests/ConsultationScreenSaveRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class ConsultationScreenSaveRequest extends FormRequest
public function rules(): array
{
return [
'consultation_id' => ['required', 'exists:consultations,id'],
'user_termin_id' => ['required', 'exists:consultation_user,id'],
'file' => ['required'],
'user_email' => ['required', 'email', 'exists:users,email'],
'timestamp' => ['required', 'date_format:Y-m-d H:i:s'],
'consultation_id' => ['required', 'integer'],
'user_termin_id' => ['required', 'integer'],
'user_email' => ['required', 'email'],
'executed_at' => ['required'],
'files' => ['array', 'min:1'],
'files.*.file' => ['required'],
'files.*.timestamp' => ['required', 'date_format:Y-m-d H:i:s'],
];
}
}
23 changes: 8 additions & 15 deletions src/Services/ConsultationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function generateJitsi(int $consultationTermId, ConsultationUserTermDto $
)
->pluck('id')
->toArray();

if (in_array(auth()->user()->getKey(), $authorIds)) {
$configOverwrite = [
"disableModeratorIndicator" => true,
Expand Down Expand Up @@ -594,23 +594,16 @@ private function getReminderData(string $reminderStatus): Collection

public function saveScreen(ConsultationSaveScreenDto $dto): void
{
/** @var ConsultationUserPivot $consultationUser */
$consultationUser = ConsultationUserPivot::query()->where('consultation_id', '=', $dto->getConsultationId())->where('id', '=', $dto->getUserTerminId())->firstOrFail();
$user = User::query()->where('email', '=', $dto->getUserEmail())->firstOrFail();

/** @var ConsultationUserTerm $userTerm */
$userTerm = $consultationUser->userTerms()->where('executed_at', '=', $dto->getExecutedAt())->firstOrFail();

if ($user->getKey() !== $consultationUser->user_id) {
throw new NotFoundHttpException(__('Consultation term for this user is not available'));
}

$termin = Carbon::make($userTerm->executed_at);
$term = Carbon::make($dto->getExecutedAt());
// consultation_id/term_start_timestamp/user_id/timestamp.jpg
$folder = ConstantEnum::DIRECTORY . "/{$dto->getConsultationId()}/{$termin->getTimestamp()}/{$user->getKey()}";
$folder = ConstantEnum::DIRECTORY . "/{$dto->getConsultationId()}/{$term->getTimestamp()}/{$user->getKey()}";

$extension = $dto->getFile() instanceof UploadedFile ? $dto->getFile()->getClientOriginalExtension() : Str::between($dto->getFile(), 'data:image/', ';base64');
Storage::putFileAs($folder, $dto->getFile(), Carbon::make($dto->getTimestamp())->getTimestamp() . '.' . $extension);
foreach ($dto->getFiles() as $file) {
$screen = $file['file'];
$extension = $screen instanceof UploadedFile ? $screen->getClientOriginalExtension() : Str::between($screen, 'data:image/', ';base64');
Storage::putFileAs($folder, $screen, Carbon::make($file['timestamp'])->getTimestamp() . '.' . $extension);
}
}

public function finishTerm(int $consultationTermId, FinishTermDto $dto): bool
Expand Down
31 changes: 22 additions & 9 deletions tests/APIs/ConsultationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,13 @@ public function testConsultationSaveScreen(): void
'consultation_id' => $consultation->getKey(),
'user_email' => $student->email,
'user_termin_id' => $consultationUser->getKey(),
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $screenTime->format('Y-m-d H:i:s'),
'executed_at' => $userTerm->executed_at->format('Y-m-d H:i:s'),
'files' => [
[
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $screenTime->format('Y-m-d H:i:s'),
],
],
])
->assertOk();

Expand All @@ -251,20 +255,29 @@ public function testConsultationSaveScreen(): void

$this->response = $this->json('POST', '/api/consultations/save-screen', [
'consultation_id' => $consultation->getKey(),
'user_email' => $admin->email,
'user_email' => '[email protected]',
'user_termin_id' => $consultationUser->getKey(),
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $time->format('Y-m-d H:i:s'),
'executed_at' => $userTerm->executed_at->format('Y-m-d H:i:s'),
])->assertNotFound();
'files' => [
[
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $time->format('Y-m-d H:i:s'),
],
],
])
->assertNotFound();

$this->response = $this->json('POST', '/api/consultations/save-screen', [
'consultation_id' => $consultation->getKey(),
'user_email' => $student->email,
'user_termin_id' => $consultationUser->getKey() + 1,
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $time->format('Y-m-d H:i:s'),
'user_termin_id' => null,
'executed_at' => $userTerm->executed_at->format('Y-m-d H:i:s'),
'files' => [
[
'file' => UploadedFile::fake()->image('image.jpg'),
'timestamp' => $time->format('Y-m-d H:i:s'),
],
],
])
->assertUnprocessable()
->assertJsonValidationErrors(['user_termin_id']);
Expand Down

0 comments on commit 1aaa3b7

Please sign in to comment.