Skip to content

Commit

Permalink
STAR-57 - list for front (#12)
Browse files Browse the repository at this point in the history
* STAR-57 - list for front

* STAR-57 - swagger + tests

* STAR-57 - throw

* Update src/Http/Requests/QuestionnaireFrontAnswerRequest.php

Co-authored-by: Paweł Chołaj <[email protected]>

* Update src/Repository/QuestionnaireRepository.php

Co-authored-by: Paweł Chołaj <[email protected]>

* STAR-57 - CR

* STAR-57 - CR

Co-authored-by: KD <do>
Co-authored-by: Paweł Chołaj <[email protected]>
  • Loading branch information
1 parent b965244 commit b552a4c
Show file tree
Hide file tree
Showing 25 changed files with 353 additions and 64 deletions.
13 changes: 13 additions & 0 deletions src/Exceptions/QuestionCanNotDeleteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace EscolaLms\Questionnaire\Exceptions;

use Exception;

class QuestionCanNotDeleteException extends Exception
{
public function __construct(string $questionId)
{
parent::__construct(__('Question :id can not be delete', ['id' => $questionId]));
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/QuestionnaireCanNotDeleteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace EscolaLms\Questionnaire\Exceptions;

use Exception;

class QuestionnaireCanNotDeleteException extends Exception
{
public function __construct(string $questionnaireId)
{
parent::__construct(__('Questionnaire :id can not be delete', ['id' => $questionnaireId]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function list(QuestionnaireListingRequest $request): JsonResponse;
/**
* @OA\Post(
* path="/api/admin/questionnaire",
* summary="Create a new questionnaire identified by id",
* summary="Create a new questionnaire",
* tags={"Questionnaire"},
* security={
* {"passport": {}},
Expand Down
120 changes: 112 additions & 8 deletions src/Http/Controllers/Contracts/QuestionnaireApiContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\Questionnaire\Http\Controllers\Contracts;

use EscolaLms\Questionnaire\Http\Requests\QuestionnaireFrontAnswerRequest;
use EscolaLms\Questionnaire\Http\Requests\QuestionnaireFrontListingRequest;
use EscolaLms\Questionnaire\Http\Requests\QuestionnaireFrontReadRequest;
use Illuminate\Http\JsonResponse;
Expand All @@ -10,9 +11,30 @@ interface QuestionnaireApiContract
{
/**
* @OA\Get(
* path="/api/questionnaire",
* path="/api/questionnaire/{model_type_title}/{model_id}",
* summary="Lists available questionnaires",
* tags={"Questionnaire"},
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* name="model_type_title",
* description="Name of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="string",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="model_id",
* description="id of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Response(
* response=200,
* description="list of available questionnaires",
Expand Down Expand Up @@ -45,17 +67,38 @@ public function list(QuestionnaireFrontListingRequest $request): JsonResponse;

/**
* @OA\Get(
* path="/api/questionnaire/{slug}",
* summary="Read a questionnaire identified by a given slug identifier",
* path="/api/questionnaire/{model_type_title}/{model_id}/{id}",
* summary="Read a questionnaire identified by a given id identifier and model",
* tags={"Questionnaire"},
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* description="Unique human-readable questionnaire identifier",
* in="path",
* name="slug",
* name="model_type_title",
* description="Name of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="string",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="model_id",
* description="id of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="integer"
* )
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="id",
* description="id of Questionnaire",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Response(
* response=200,
Expand All @@ -77,4 +120,65 @@ public function list(QuestionnaireFrontListingRequest $request): JsonResponse;
* )
*/
public function read(QuestionnaireFrontReadRequest $request): JsonResponse;

/**
* @OA\Post(
* path="/api/questionnaire/{model_type_title}/{model_id}/{id}",
* summary="Save a questionnaire answers",
* tags={"Questionnaire"},
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* name="model_type_title",
* description="Name of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="string",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="model_id",
* description="id of Model (Course, Webinar etd.)",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="id",
* description="id of questionnaire",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\RequestBody(
* description="Answer attributes",
* required=true,
* @OA\JsonContent(ref="#/components/schemas/QuestionnaireAnswerRequest")
* ),
* @OA\Response(
* response=200,
* description="",
* @OA\JsonContent(ref="#/components/schemas/Questionnaire")
* ),
* @OA\Response(
* response=401,
* description="endpoint requires authentication",
* ),
* @OA\Response(
* response=403,
* description="user doesn't have required access rights",
* ),
* @OA\Response(
* response=500,
* description="server-side error",
* ),
* )
*/
public function answer(QuestionnaireFrontAnswerRequest $request): JsonResponse;
}
11 changes: 10 additions & 1 deletion src/Http/Controllers/QuestionAdminApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Questionnaire\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Questionnaire\Exceptions\QuestionCanNotDeleteException;
use EscolaLms\Questionnaire\Http\Controllers\Contracts\QuestionAdminApiContract;
use EscolaLms\Questionnaire\Http\Requests\QuestionCreateRequest;
use EscolaLms\Questionnaire\Http\Requests\QuestionDeleteRequest;
Expand All @@ -12,6 +13,7 @@
use EscolaLms\Questionnaire\Http\Resources\QuestionResource;
use EscolaLms\Questionnaire\Repository\Contracts\QuestionRepositoryContract;
use EscolaLms\Questionnaire\Services\Contracts\QuestionServiceContract;
use Exception;
use Illuminate\Http\JsonResponse;

class QuestionAdminApiController extends EscolaLmsBaseController implements QuestionAdminApiContract
Expand Down Expand Up @@ -55,9 +57,16 @@ public function update(QuestionUpdateRequest $request, int $id): JsonResponse
);
}

/**
* @throws QuestionCanNotDeleteException
*/
public function delete(QuestionDeleteRequest $request, int $id): JsonResponse
{
$this->questionService->deleteQuestion($request->getQuestion());
try {
$this->questionService->deleteQuestion($request->getQuestion());
} catch (Exception $err) {
throw new QuestionCanNotDeleteException($id);
}

return $this->sendResponse(true, __("Question delete successfully"));
}
Expand Down
11 changes: 10 additions & 1 deletion src/Http/Controllers/QuestionnaireAdminApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Questionnaire\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Questionnaire\Exceptions\QuestionnaireCanNotDeleteException;
use EscolaLms\Questionnaire\Http\Controllers\Contracts\QuestionnaireAdminApiContract;
use EscolaLms\Questionnaire\Http\Requests\QuestionnaireCreateRequest;
use EscolaLms\Questionnaire\Http\Requests\QuestionnaireDeleteRequest;
Expand All @@ -17,6 +18,7 @@
use EscolaLms\Questionnaire\Repository\Contracts\QuestionnaireRepositoryContract;
use EscolaLms\Questionnaire\Services\Contracts\QuestionnaireAnswerServiceContract;
use EscolaLms\Questionnaire\Services\Contracts\QuestionnaireServiceContract;
use Exception;
use Illuminate\Http\JsonResponse;

class QuestionnaireAdminApiController extends EscolaLmsBaseController implements QuestionnaireAdminApiContract
Expand Down Expand Up @@ -66,9 +68,16 @@ public function update(QuestionnaireUpdateRequest $request, int $id): JsonRespon
);
}

/**
* @throws QuestionnaireCanNotDeleteException
*/
public function delete(QuestionnaireDeleteRequest $request, int $id): JsonResponse
{
$this->questionnaireService->deleteQuestionnaire($request->getQuestionnaire());
try {
$this->questionnaireService->deleteQuestionnaire($request->getQuestionnaire());
} catch (Exception $err) {
throw new QuestionnaireCanNotDeleteException($id);
}

return $this->sendResponse(true, __("Questionnaire delete successfully"));
}
Expand Down
23 changes: 23 additions & 0 deletions src/Http/Requests/QuestionnaireFrontAnswerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;

/**
* @OA\Schema(
* schema="QuestionnaireAnswerRequest",
* @OA\Property(
* property="answers",
* type="array",
* description="answers for questionnaire"
* @OA\Items(
* type="object",
* @OA\Property(
* property="question_id",
* type="integer",
* description="question identified by id",
* ),
* @OA\Property(
* property="rate",
* type="integer",
* description="rate from 1 to 5",
* ),
* )
* ),
* )
*/
class QuestionnaireFrontAnswerRequest extends FormRequest
{
protected function prepareForValidation(): void
Expand Down
23 changes: 19 additions & 4 deletions src/Repository/QuestionnaireRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EscolaLms\Questionnaire\Models\Questionnaire;
use EscolaLms\Questionnaire\Repository\Contracts\QuestionnaireRepositoryContract;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;

class QuestionnaireRepository extends BaseRepository implements QuestionnaireRepositoryContract
{
Expand All @@ -16,12 +17,26 @@ public function model(): string

public function getFieldsSearchable(): array
{
return [];
return ['active', 'id', 'title'];
}

public function searchAndPaginate(array $search = [], ?int $perPage = null, string $orderDirection = 'asc', string $orderColumn = 'id'): LengthAwarePaginator
{
return $this->allQuery($search)->orderBy($orderColumn, $orderDirection)->paginate($perPage);
public function searchAndPaginate(
array $search = [],
?int $perPage = null,
string $orderDirection = 'asc',
string $orderColumn = 'id'
): LengthAwarePaginator {
$query = $this->allQuery($search);
if (isset($search['model_type_id'])) {
$query->whereHas(
'questionnaireModels',
fn(Builder $query) => $query
->where('model_type_id', $search['model_type_id'])
->where('model_id', $search['model_id'])
);
}

return $query->orderBy($orderColumn, $orderDirection)->paginate($perPage);
}

public function insert(Questionnaire $questionnaire): Questionnaire
Expand Down
16 changes: 6 additions & 10 deletions src/Services/QuestionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ public function __construct(

public function deleteQuestion(Question $question): bool
{
try {
DB::transaction(function () use ($question) {
$this->questionAnswerRepository->deleteByQuestionId($question->id);
$this->questionRepository->delete($question->id);
});

return true;
} catch (\Exception $err) {
return false;
}
DB::transaction(function () use ($question) {
$this->questionAnswerRepository->deleteByQuestionId($question->id);
$this->questionRepository->delete($question->id);
});

return true;
}

public function createQuestion(array $data): Question
Expand Down
14 changes: 5 additions & 9 deletions src/Services/QuestionnaireModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ public function __construct(

public function deleteQuestionnaireModel(QuestionnaireModel $questionnaireModel): bool
{
try {
DB::transaction(function () use ($questionnaireModel) {
$this->questionAnswerRepository->deleteByModelId($questionnaireModel->id);
$this->questionnaireModelRepository->delete($questionnaireModel->id);
});
DB::transaction(function () use ($questionnaireModel) {
$this->questionAnswerRepository->deleteByModelId($questionnaireModel->id);
$this->questionnaireModelRepository->delete($questionnaireModel->id);
});

return true;
} catch (\Exception $err) {
return false;
}
return true;
}

public function saveModelsForQuestionnaire(int $questionnaireId, array $models): void
Expand Down
Loading

0 comments on commit b552a4c

Please sign in to comment.