Skip to content

Commit

Permalink
Merge pull request #3 from dicani0/feature/courses
Browse files Browse the repository at this point in the history
Feature/courses
  • Loading branch information
dicani0 authored May 11, 2024
2 parents 027a699 + 243c107 commit b85d64a
Show file tree
Hide file tree
Showing 33 changed files with 501 additions and 200 deletions.
12 changes: 0 additions & 12 deletions app/Application/Http/Course/Commands/CreateCourseCommand.php

This file was deleted.

26 changes: 20 additions & 6 deletions app/Application/Http/Course/Controllers/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,39 @@

namespace App\Application\Http\Course\Controllers;

use App\Application\Http\Course\Commands\CreateCourseCommand;
use App\Application\Http\Course\Handlers\CreateCourseHandler;
use Domains\Course\DTO\CreateCourseDto;

use App\Course\Application\Command\CreateCourseCommand;
use App\Course\Application\CommandHandler\CreateCourseHandler;
use App\Course\Application\DTO\CreateCourseDto;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Inertia\Response;

class CourseController
{
use AuthorizesRequests;

public function __construct(private CreateCourseHandler $handler)
{
}

public function create(): Response
{
$this->authorize('create_course');
return inertia('Course/CreateCourse');
}

public function store(CreateCourseDto $dto, CreateCourseHandler $handler): Response
public function store(CreateCourseDto $dto): Response
{
$command = new CreateCourseCommand(data: $dto);
$handler->handle(command: $command);
$command = new CreateCourseCommand(
name: $dto->name,
description: $dto->description,
author_id: $dto->author->id,
starts_at: $dto->starts_at,
ends_at: $dto->ends_at,
published_at: $dto->published_at,
);

$this->handler->handle(command: $command);

return inertia(component: 'Course/CreateCourse');
}
Expand Down
13 changes: 0 additions & 13 deletions app/Application/Http/Course/Handlers/CreateCourseHandler.php

This file was deleted.

20 changes: 0 additions & 20 deletions app/Application/Http/Course/Requests/CourseRequest.php

This file was deleted.

20 changes: 0 additions & 20 deletions app/Application/Http/Course/Resources/CourseResource.php

This file was deleted.

16 changes: 6 additions & 10 deletions app/Application/Http/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Application\Http\Providers;

use Domains\Course\Repositories\CourseRepositoryContract;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -13,20 +11,18 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->app->bind(
CourseRepositoryContract::class,
);

}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Factory::guessFactoryNamesUsing(function (string $modelName) {
$modelName = class_basename($modelName);

return "Database\\Factories\\{$modelName}Factory";
});
// Factory::guessFactoryNamesUsing(function (string $modelName) {
// $modelName = class_basename($modelName);
//
// return "Database\\Factories\\{$modelName}Factory";
// });
}
}
18 changes: 18 additions & 0 deletions app/Course/Application/Command/CreateCourseCommand.php
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 app/Course/Application/CommandHandler/CreateCourseHandler.php
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);
}
}
18 changes: 18 additions & 0 deletions app/Course/Application/CourseServiceProvider.php
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
);
}
}
35 changes: 35 additions & 0 deletions app/Course/Application/DTO/CreateCourseDto.php
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;
}
}
55 changes: 55 additions & 0 deletions app/Course/Domain/Entity/Course.php
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;
}

}
15 changes: 15 additions & 0 deletions app/Course/Domain/Repository/CourseRepositoryContract.php
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 app/Course/Infrastructure/Persistence/Model/CourseModel.php
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();
}
});
}
}
Loading

0 comments on commit b85d64a

Please sign in to comment.