Skip to content

Commit

Permalink
GEST-36 Delete category if it is not used (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Feb 21, 2024
1 parent 8017477 commit 7dd4de4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/Exceptions/CategoryIsUsed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace EscolaLms\Categories\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class CategoryIsUsed extends Exception
{
public function __construct(string $message = null) {
parent::__construct($message ?? __('Category is used'));
}

public function render(): JsonResponse
{
return response()->json([
'message' => $this->getMessage(),
'success' => false,
],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/CategoryAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function delete(int $id, CategoryDeleteRequest $categoryDeleteRequest): J
{
$this->categoryService->delete($id);

return response()->json(null, 200);
return $this->sendSuccess(__('Category deleted successfully.'));
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Http/Controllers/Swagger/CategorySwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function tree(CategoryListRequest $request): JsonResponse;
* required=true,
* in="path",
* @OA\Schema(
* type="string",
* type="integer",
* ),
* ),
* @OA\Response(
Expand Down Expand Up @@ -160,6 +160,14 @@ public function create(CategoryCreateRequest $categoryCreateRequest): JsonRespon
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* name="id",
* required=true,
* in="path",
* @OA\Schema(
* type="integer",
* ),
* ),
* @OA\RequestBody(
* @OA\JsonContent(
* @OA\Property(
Expand Down Expand Up @@ -220,6 +228,14 @@ public function update(int $id, CategoryUpdateRequest $request): JsonResponse;
* path="/api/admin/categories/{id}",
* summary="Destroy category",
* description="Destroy the specified category",
* @OA\Parameter(
* name="id",
* required=true,
* in="path",
* @OA\Schema(
* type="integer",
* ),
* ),
* @OA\Response(
* response=200,
* description="successful operation",
Expand Down
8 changes: 8 additions & 0 deletions src/Repositories/CategoriesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
* Class CategoriesRepositoryRepository
Expand Down Expand Up @@ -92,6 +93,13 @@ public function getByPopularity(PaginationDto $pagination, ?Carbon $from = null,
return $query->get();
}

public function get(int $id): Category
{
$query = $this->model->newQuery();

return $query->findOrFail($id);
}

public function listAll(CategoryCriteriaFilterDto $criteriaDto, OrderDto $dto, array $columns = ['*'], ?int $perPage = 15, ?bool $isActive = null): LengthAwarePaginator
{
$query = $this->queryWithAppliedCriteria($criteriaDto->toArray())->with('parent');
Expand Down
2 changes: 2 additions & 0 deletions src/Repositories/Contracts/CategoriesRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Categories\Repositories\Contracts;

use EscolaLms\Categories\Dtos\CategoryCriteriaFilterDto;
use EscolaLms\Categories\Models\Category;
use EscolaLms\Core\Dtos\OrderDto;
use EscolaLms\Core\Repositories\Contracts\ActivationContract;
use EscolaLms\Core\Repositories\Contracts\BaseRepositoryContract;
Expand All @@ -12,4 +13,5 @@ interface CategoriesRepositoryContract extends BaseRepositoryContract, Activatio
{
public function allRoots(array $search = [], ?int $skip = null, ?int $limit = null);
public function listAll(CategoryCriteriaFilterDto $criteriaDto, OrderDto $dto, array $columns = ['*'], ?int $perPage = 15, ?bool $isActive): LengthAwarePaginator;
public function get(int $id): Category;
}
16 changes: 15 additions & 1 deletion src/Services/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EscolaLms\Categories\Dtos\CategoryDto;
use EscolaLms\Categories\Dtos\CategorySortDto;
use EscolaLms\Categories\Enums\ConstantEnum;
use EscolaLms\Categories\Exceptions\CategoryIsUsed;
use EscolaLms\Categories\Models\Category;
use EscolaLms\Categories\Repositories\Contracts\CategoriesRepositoryContract;
use EscolaLms\Categories\Services\Contracts\CategoryServiceContracts;
Expand Down Expand Up @@ -92,9 +93,22 @@ public function update(int $id, CategoryDto $categoryDto): Category
});
}

/**
* @throws CategoryIsUsed
*/
public function delete(int $id): void
{
Category::destroy($id);
$category = $this->categoryRepository->get($id);

if ($category->children()->count() > 0) {
throw new CategoryIsUsed(__('The category has categories'));
}

if (class_exists(\EscolaLms\Courses\Models\Course::class) && $category->courses()->count() > 0) {
throw new CategoryIsUsed(__('The category is used in courses'));
}

$this->categoryRepository->delete($id);
}

public function getPopular(PaginationDto $pagination, PeriodDto $period): Collection
Expand Down
14 changes: 14 additions & 0 deletions tests/API/CategoriesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,20 @@ public function testAdminSortCategories(): void
$this->assertEquals(13, $category2->refresh()->order);
}

public function testShouldNotDeleteCategoryWhenHasCategories(): void
{
$category = Category::factory()
->has(Category::factory()->count(2), 'children')
->create();

$this->actingAs($this->createAdmin(), 'api')->delete('/api/admin/categories/' . $category->getKey())
->assertUnprocessable()
->assertJsonFragment([
'message' => __('The category has categories'),
'success' => false,
]);
}

private function createAdmin()
{
$user = config('auth.providers.users.model')::factory()->create();
Expand Down

0 comments on commit 7dd4de4

Please sign in to comment.