-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dicani0/feature/courses
Feature/courses
- Loading branch information
Showing
33 changed files
with
501 additions
and
200 deletions.
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
app/Application/Http/Course/Commands/CreateCourseCommand.php
This file was deleted.
Oops, something went wrong.
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
13 changes: 0 additions & 13 deletions
13
app/Application/Http/Course/Handlers/CreateCourseHandler.php
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
<?php | ||
|
||
namespace App\Course\Application\Command; | ||
|
||
use DateTime; | ||
|
||
class CreateCourseCommand | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public string $description, | ||
public int $author_id, | ||
public DateTime $starts_at, | ||
public ?DateTime $ends_at, | ||
public ?DateTime $published_at, | ||
) { | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/Course/Application/CommandHandler/CreateCourseHandler.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,30 @@ | ||
<?php | ||
|
||
namespace App\Course\Application\CommandHandler; | ||
|
||
use App\Course\Application\Command\CreateCourseCommand; | ||
use App\Course\Domain\Entity\Course; | ||
use App\Course\Domain\Repository\CourseRepositoryContract; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
readonly class CreateCourseHandler | ||
{ | ||
public function __construct(private CourseRepositoryContract $courseRepository) | ||
{ | ||
} | ||
|
||
public function handle(CreateCourseCommand $command): void | ||
{ | ||
$course = new Course( | ||
id: Uuid::uuid4()->toString(), | ||
name: $command->name, | ||
description: $command->description, | ||
authorId: $command->author_id, | ||
startsAt: $command->starts_at, | ||
endsAt: $command->ends_at, | ||
publishedAt: $command->published_at, | ||
); | ||
|
||
$this->courseRepository->save(course: $course); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace App\Course\Application; | ||
|
||
use App\Course\Domain\Repository\CourseRepositoryContract; | ||
use App\Course\Infrastructure\Persistence\Repository\EloquentCourseRepository; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class CourseServiceProvider extends ServiceProvider | ||
{ | ||
public function register(): void | ||
{ | ||
$this->app->bind( | ||
CourseRepositoryContract::class, | ||
EloquentCourseRepository::class | ||
); | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace App\Course\Application\DTO; | ||
|
||
use DateTime; | ||
use Domains\User\Models\User; | ||
use Illuminate\Support\Facades\Auth; | ||
use Spatie\LaravelData\Attributes\Computed; | ||
use Spatie\LaravelData\Attributes\WithCast; | ||
use Spatie\LaravelData\Casts\DateTimeInterfaceCast; | ||
use Spatie\LaravelData\Data; | ||
|
||
class CreateCourseDto extends Data | ||
{ | ||
#[Computed] | ||
public User|null $author; | ||
|
||
public function __construct( | ||
public string $name, | ||
public string|null $description, | ||
#[WithCast(DateTimeInterfaceCast::class)] | ||
public DateTime $starts_at, | ||
#[WithCast(DateTimeInterfaceCast::class)] | ||
public DateTime|null $ends_at, | ||
#[WithCast(DateTimeInterfaceCast::class)] | ||
public DateTime|null $published_at, | ||
) { | ||
$this->author = Auth::user(); | ||
} | ||
|
||
public static function authorize(): bool | ||
{ | ||
return Auth::user()?->can('create_course') ?? false; | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace App\Course\Domain\Entity; | ||
|
||
use DateTime; | ||
|
||
readonly class Course | ||
{ | ||
public function __construct( | ||
private ?string $id, | ||
private string $name, | ||
private string $description, | ||
private int $authorId, | ||
private ?DateTime $startsAt, | ||
private ?DateTime $endsAt, | ||
private ?DateTime $publishedAt, | ||
) { | ||
} | ||
|
||
public function getId(): ?string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getDescription(): string | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function getAuthorId(): int | ||
{ | ||
return $this->authorId; | ||
} | ||
|
||
public function getStartsAt(): ?DateTime | ||
{ | ||
return $this->startsAt; | ||
} | ||
|
||
public function getEndsAt(): ?DateTime | ||
{ | ||
return $this->endsAt; | ||
} | ||
|
||
public function getPublishedAt(): ?DateTime | ||
{ | ||
return $this->publishedAt; | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace App\Course\Domain\Repository; | ||
|
||
use App\Course\Domain\Entity\Course; | ||
use Illuminate\Support\Collection; | ||
|
||
interface CourseRepositoryContract | ||
{ | ||
public function save(Course $course): void; | ||
|
||
public function findById(int $id): ?Course; | ||
|
||
public function findAll(): Collection; | ||
} |
36 changes: 36 additions & 0 deletions
36
app/Course/Infrastructure/Persistence/Model/CourseModel.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,36 @@ | ||
<?php | ||
|
||
namespace App\Course\Infrastructure\Persistence\Model; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class CourseModel extends Model | ||
{ | ||
use HasFactory, HasUuids; | ||
|
||
public $incrementing = false; | ||
protected $table = "courses"; | ||
protected $keyType = 'string'; | ||
|
||
protected $fillable = [ | ||
"name", | ||
"description", | ||
"author_id", | ||
"starts_at", | ||
"ends_at", | ||
"published_at", | ||
]; | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::creating(function (self $model) { | ||
if (empty($model->{$model->getKeyName()})) { | ||
$model->{$model->getKeyName()} = $this->newUniqueId(); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.