-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1fb1702
commit 01d679f
Showing
21 changed files
with
484 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/Http/Controllers/Api/CalculateOvertimeHoursController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Http\Controllers\Api; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
use Toby\Domain\OvertimeCalculator; | ||
use Toby\Http\Controllers\Controller; | ||
use Toby\Http\Requests\Api\CalculateOvertimeHoursRequest; | ||
|
||
class CalculateOvertimeHoursController extends Controller | ||
{ | ||
public function __invoke(CalculateOvertimeHoursRequest $request, OvertimeCalculator $overtimeCalculator): JsonResponse | ||
{ | ||
$hours = $overtimeCalculator->calculateHours($request->from(), $request->to()); | ||
|
||
return new JsonResponse($hours); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Http\Requests\Api; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Carbon; | ||
use Toby\Http\Rules\YearPeriodExists; | ||
use Toby\Models\YearPeriod; | ||
|
||
class CalculateOvertimeHoursRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"from" => ["required", "date_format:Y-m-d H:i", new YearPeriodExists()], | ||
"to" => ["required", "date_format:Y-m-d H:i", new YearPeriodExists()], | ||
]; | ||
} | ||
|
||
public function from(): Carbon | ||
{ | ||
return Carbon::create($this->request->get("from")); | ||
} | ||
|
||
public function to(): Carbon | ||
{ | ||
return Carbon::create($this->request->get("to")); | ||
} | ||
|
||
public function yearPeriod(): YearPeriod | ||
{ | ||
return YearPeriod::findByYear(Carbon::create($this->request->get("from"))->year); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Validation; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Validation\ValidationException; | ||
use Toby\Models\OvertimeRequest; | ||
use Toby\Validation\Rules\OvertimeRequest\NoPendingOvertimeRequestInRange; | ||
use Toby\Validation\Rules\OvertimeRequest\OvertimeRequestRule; | ||
|
||
class OvertimeRequestValidator | ||
{ | ||
protected array $rules = [ | ||
NoPendingOvertimeRequestInRange::class, | ||
]; | ||
|
||
public function __construct( | ||
protected Container $container, | ||
) {} | ||
|
||
/** | ||
* @throws ValidationException | ||
*/ | ||
public function validate(OvertimeRequest $overtimeRequest): void | ||
{ | ||
foreach ($this->rules as $rule) { | ||
$rule = $this->makeRule($rule); | ||
|
||
if (!$rule->check($overtimeRequest)) { | ||
throw ValidationException::withMessages([ | ||
"overtimeRequest" => $rule->errorMessage(), | ||
]); | ||
} | ||
} | ||
} | ||
|
||
protected function makeRule(string $class): OvertimeRequestRule | ||
{ | ||
return $this->container->make($class); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
app/Validation/Rules/OvertimeRequest/NoPendingOvertimeRequestInRange.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Validation\Rules\OvertimeRequest; | ||
|
||
use Toby\Domain\OvertimeRequestStatesRetriever; | ||
use Toby\Models\OvertimeRequest; | ||
|
||
class NoPendingOvertimeRequestInRange implements OvertimeRequestRule | ||
{ | ||
public function check(OvertimeRequest $overtimeRequest): bool | ||
{ | ||
return !$overtimeRequest | ||
->user | ||
->overtimeRequests() | ||
->overlapsWith($overtimeRequest) | ||
->states(OvertimeRequestStatesRetriever::pendingStates()) | ||
->exists(); | ||
} | ||
|
||
public function errorMessage(): string | ||
{ | ||
return __("You have a pending request in this date range."); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Validation/Rules/OvertimeRequest/OvertimeRequestRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Toby\Validation\Rules\OvertimeRequest; | ||
|
||
use Toby\Models\OvertimeRequest; | ||
|
||
interface OvertimeRequestRule | ||
{ | ||
public function check(OvertimeRequest $overtimeRequest): bool; | ||
|
||
public function errorMessage(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.