-
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.
* Dictionary CRUD * Fix phpstan
- Loading branch information
Showing
28 changed files
with
1,301 additions
and
6 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
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), | ||
]; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
database/migrations/2024_03_01_074821_create_dictionaries_table.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,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'); | ||
} | ||
}; |
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,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); | ||
} | ||
} |
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,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'), | ||
); | ||
} | ||
} |
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 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; | ||
} | ||
} |
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
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')); | ||
} | ||
} |
Oops, something went wrong.