Skip to content

Commit

Permalink
Dictionary CRUD (#1)
Browse files Browse the repository at this point in the history
* Dictionary CRUD

* Fix phpstan
  • Loading branch information
mako321 committed Mar 5, 2024
1 parent f06ea00 commit 9414a8b
Show file tree
Hide file tree
Showing 28 changed files with 1,301 additions and 6 deletions.
20 changes: 20 additions & 0 deletions database/factories/DictionaryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace EscolaLms\Dictionaries\Database\Factories;

use EscolaLms\Dictionaries\Models\Dictionary;
use Illuminate\Database\Eloquent\Factories\Factory;

class DictionaryFactory extends Factory
{
protected $model = Dictionary::class;

public function definition(): array
{
return [
'name' => $this->faker->name,
'slug' => $this->faker->unique()->slug,
'free_views_count' => $this->faker->numberBetween(1),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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::create('dictionaries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->integer('free_views_count')->nullable();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('dictionaries');
}
};
8 changes: 2 additions & 6 deletions database/seeders/DictionariesPermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ public function run(): void
Permission::findOrCreate($permission, 'api');
}

$admin->givePermissionTo(DictionariesPermissionEnum::getValues());

$student->givePermissionTo([
DictionariesPermissionEnum::DICTIONARY_LIST,
DictionariesPermissionEnum::DICTIONARY_READ,
]);
$admin->givePermissionTo(DictionariesPermissionEnum::getAdminPermissions());
$student->givePermissionTo(DictionariesPermissionEnum::getStudentPermissions());
}
}
28 changes: 28 additions & 0 deletions src/Dtos/DictionaryCriteriaDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EscolaLms\Dictionaries\Dtos;

use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use EscolaLms\Core\Dtos\CriteriaDto as BaseCriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\LikeCriterion;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class DictionaryCriteriaDto extends BaseCriteriaDto implements DtoContract, InstantiateFromRequest
{
public static function instantiateFromRequest(Request $request): self
{
$criteria = new Collection();

if ($request->get('name')) {
$criteria->push(new LikeCriterion('name', $request->get('name')));
}

if ($request->get('slug')) {
$criteria->push(new LikeCriterion('slug', $request->get('slug')));
}

return new self($criteria);
}
}
45 changes: 45 additions & 0 deletions src/Dtos/DictionaryDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace EscolaLms\Dictionaries\Dtos;

use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use Illuminate\Http\Request;

class DictionaryDto implements DtoContract, InstantiateFromRequest
{
private string $name;
private ?int $freeViewsCount;

public function __construct(string $name, ?int $freeViewsCount)
{
$this->name = $name;
$this->freeViewsCount = $freeViewsCount;
}

public function getName(): string
{
return $this->name;
}

public function getFreeViewsCount(): ?int
{
return $this->freeViewsCount;
}

public function toArray(): array
{
return [
'name' => $this->getName(),
'free_views_count' => $this->getFreeViewsCount(),
];
}

public static function instantiateFromRequest(Request $request): self
{
return new self(
$request->input('name'),
$request->input('free_views_count'),
);
}
}
55 changes: 55 additions & 0 deletions src/Dtos/PageDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace EscolaLms\Dictionaries\Dtos;

use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use Illuminate\Http\Request;

class PageDto implements DtoContract, InstantiateFromRequest
{
private ?int $skip;

private ?int $per_page;

public function __construct(?int $skip, ?int $per_page)
{
$this->skip = $skip;
$this->per_page = $per_page;
}

public function toArray(): array
{
return [
'skip' => $this->getSkip(),
'per_page' => $this->getPerPage()
];
}

public static function instantiateFromRequest(Request $request): self
{
$per_page = config('paginate.default.limit', 15);

if ($request->get('page')) {
return new self(
$request->get('skip', ($request->get('page') - 1) * $per_page),
$request->get('per_page', $per_page),
);
}

return new self(
$request->get('skip', config('paginate.default.limit', 0)),
$request->get('per_page', $per_page),
);
}

public function getSkip(): ?int
{
return $this->skip;
}

public function getPerPage(): ?int
{
return $this->per_page;
}
}
22 changes: 22 additions & 0 deletions src/Enums/DictionariesPermissionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,26 @@ class DictionariesPermissionEnum extends BasicEnum
public const DICTIONARY_READ = 'dictionary_read';
public const DICTIONARY_UPDATE = 'dictionary_update';
public const DICTIONARY_DELETE = 'dictionary_delete';

public const DICTIONARY_LIST_SELF = 'dictionary_list-self';
public const DICTIONARY_READ_SELF = 'dictionary_read-self';

public static function getAdminPermissions(): array
{
return [
self::DICTIONARY_LIST,
self::DICTIONARY_CREATE,
self::DICTIONARY_READ,
self::DICTIONARY_UPDATE,
self::DICTIONARY_DELETE,
];
}

public static function getStudentPermissions(): array
{
return [
self::DICTIONARY_LIST_SELF,
self::DICTIONARY_READ_SELF,
];
}
}
6 changes: 6 additions & 0 deletions src/EscolaLmsDictionariesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use EscolaLms\Auth\EscolaLmsAuthServiceProvider;
use EscolaLms\Dictionaries\Providers\AuthServiceProvider;
use EscolaLms\Dictionaries\Repositories\Contracts\DictionaryRepositoryContract;
use EscolaLms\Dictionaries\Repositories\DictionaryRepository;
use EscolaLms\Dictionaries\Services\Contracts\DictionaryServiceContract;
use EscolaLms\Dictionaries\Services\DictionaryService;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -12,9 +16,11 @@
class EscolaLmsDictionariesServiceProvider extends ServiceProvider
{
public const SERVICES = [
DictionaryServiceContract::class => DictionaryService::class,
];

public const REPOSITORIES = [
DictionaryRepositoryContract::class => DictionaryRepository::class,
];

public $singletons = self::SERVICES + self::REPOSITORIES;
Expand Down
54 changes: 54 additions & 0 deletions src/Http/Controllers/DictionaryAdminApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace EscolaLms\Dictionaries\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Dictionaries\Http\Controllers\Swagger\DictionaryAdminApiControllerSwagger;
use EscolaLms\Dictionaries\Http\Requests\Dictionary\CreateDictionaryRequest;
use EscolaLms\Dictionaries\Http\Requests\Dictionary\DeleteDictionaryRequest;
use EscolaLms\Dictionaries\Http\Requests\Dictionary\ListDictionaryRequest;
use EscolaLms\Dictionaries\Http\Requests\Dictionary\ReadDictionaryRequest;
use EscolaLms\Dictionaries\Http\Requests\Dictionary\UpdateDictionaryRequest;
use EscolaLms\Dictionaries\Http\Resources\DictionaryResource;
use EscolaLms\Dictionaries\Services\Contracts\DictionaryServiceContract;
use Illuminate\Http\JsonResponse;

class DictionaryAdminApiController extends EscolaLmsBaseController implements DictionaryAdminApiControllerSwagger
{
public function __construct(private readonly DictionaryServiceContract $dictionaryService)
{
}

public function index(ListDictionaryRequest $request): JsonResponse
{
$results = $this->dictionaryService->list($request->getCriteria(), $request->getPage(), $request->getOrder());

return $this->sendResponseForResource(DictionaryResource::collection($results));
}

public function store(CreateDictionaryRequest $request): JsonResponse
{
$dictionary = $this->dictionaryService->create($request->toDto());

return $this->sendResponseForResource(DictionaryResource::make($dictionary));
}

public function show(ReadDictionaryRequest $request): JsonResponse
{
return $this->sendResponseForResource(DictionaryResource::make($request->getDictionary()));
}

public function update(UpdateDictionaryRequest $request): JsonResponse
{
$dictionary = $this->dictionaryService->update($request->getId(), $request->toDto());

return $this->sendResponseForResource(DictionaryResource::make($dictionary));
}

public function delete(DeleteDictionaryRequest $request): JsonResponse
{
$this->dictionaryService->delete($request->getDictionary());

return $this->sendSuccess(__('Dictionary deleted successfully'));
}
}

0 comments on commit 9414a8b

Please sign in to comment.