Skip to content

Commit

Permalink
Remove topic preivew from course details as it super slow. Topic prev… (
Browse files Browse the repository at this point in the history
#327)

* Remove topic preivew from course details as it super slow. Topic preview available from endpoint

* tests for preview

* tests for preview

* spelling

---------

Co-authored-by: Mateusz Wojczal <mateusz@wojczal.com>
  • Loading branch information
qunabu and Mateusz Wojczal authored Jan 16, 2025
1 parent 8140414 commit 2a534a2
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 76 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: Tests PHPUnit in environments
on: [pull_request]

jobs:
php8-laravel-latest-phpunit-mysql:
php83-laravel-latest-phpunit-mysql:
runs-on: ubuntu-latest
container:
image: escolalms/php:8
image: escolalms/php:8.3-alpine

services:
mysql:
Expand Down Expand Up @@ -144,5 +144,4 @@ jobs:

- name: Run tests
run: vendor/bin/phpunit

### TODO add behat tests here
25 changes: 25 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
networks:
escola_lms_tests:
name: escola_lms_tests
driver: bridge

services:
courses_tests:
image: escolalms/php:8.3-alpine
environment:
- "COMPOSER_ROOT_VERSION=0.9.9"
volumes:
- ./:/var/www/html
command: bash -c "COMPOSER_ROOT_VERSION=0.9.9 composer install && vendor/bin/testbench migrate:fresh && vendor/bin/phpunit"
networks:
- escola_lms_tests

postgres:
image: postgres:16
networks:
- escola_lms_tests
environment:
- "POSTGRES_DB=${POSTGRES_DB:-default}"
- "POSTGRES_USER=${POSTGRES_USER:-default}"
- "POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-secret}"
- TZ=Europe/Warsaw
12 changes: 2 additions & 10 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
<testsuite name="Escola LMS Courses">
<directory suffix="Test.php">./tests</directory>
</testsuite>
<php>
<server name="SERVER_NAME" value="localhost"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="pgsql"/>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_HOST" value="postgres"/>
<env name="DB_PORT" value="5432"/>
<env name="DB_DATABASE" value="default"/>
<env name="DB_USERNAME" value="default"/>
Expand Down
22 changes: 21 additions & 1 deletion src/Http/Controllers/CourseAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\Courses\Http\Controllers;

use EscolaLms\Courses\Http\Resources\TopicResource;
use EscolaLms\Core\Dtos\OrderDto;
use EscolaLms\Courses\Enum\CoursesPermissionsEnum;
use EscolaLms\Courses\Enum\CourseStatusEnum;
Expand Down Expand Up @@ -73,7 +74,9 @@ public function authoredCourses(ListAuthoredCourseAPIRequest $request): JsonResp

return $this->sendResponseForResource(CourseListResource::collection(
$this->courseRepository->getAuthoredCourses(
$user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))
$user->getKey(),
OrderDto::instantiateFromRequest($request)
)->paginate($request->get('per_page', 20))

Check warning on line 79 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "DecrementInteger": @@ @@ public function authoredCourses(ListAuthoredCourseAPIRequest $request): JsonResponse { $user = $request->user(); - return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))), __('Courses retrieved successfully')); + return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 19))), __('Courses retrieved successfully')); } public function store(CreateCourseAPIRequest $request): JsonResponse {

Check warning on line 79 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "IncrementInteger": @@ @@ public function authoredCourses(ListAuthoredCourseAPIRequest $request): JsonResponse { $user = $request->user(); - return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 20))), __('Courses retrieved successfully')); + return $this->sendResponseForResource(CourseListResource::collection($this->courseRepository->getAuthoredCourses($user->getKey(), OrderDto::instantiateFromRequest($request))->paginate($request->get('per_page', 21))), __('Courses retrieved successfully')); } public function store(CreateCourseAPIRequest $request): JsonResponse {
), __('Courses retrieved successfully'));
}

Expand Down Expand Up @@ -176,4 +179,21 @@ public function uniqueTags(): JsonResponse
$this->sendResponse($tags, 'Tags unique fetched successfully') :
$this->sendError('Tags not found', 404);

Check warning on line 180 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "DecrementInteger": @@ @@ public function uniqueTags(): JsonResponse { $tags = $this->tagRepositoryContract->uniqueTagsFromActiveCourses(); - return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 404); + return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 403); } public function preview($id, $topic_id, GetCourseAPIRequest $request): JsonResponse {

Check warning on line 180 in src/Http/Controllers/CourseAPIController.php

View workflow job for this annotation

GitHub Actions / php81-mysql

Escaped Mutant for Mutator "IncrementInteger": @@ @@ public function uniqueTags(): JsonResponse { $tags = $this->tagRepositoryContract->uniqueTagsFromActiveCourses(); - return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 404); + return $tags ? $this->sendResponse($tags, 'Tags unique fetched successfully') : $this->sendError('Tags not found', 405); } public function preview($id, $topic_id, GetCourseAPIRequest $request): JsonResponse {
}

public function preview($id, $topic_id, GetCourseAPIRequest $request): JsonResponse
{
$course = $request->getCourse();

if (empty($course)) {
return $this->sendError(__('Course not found'));
}

$topic = $request->getTopic();

if (empty($topic) || !$topic->preview) {
return $this->sendError(__('Topic not found or not able to preview'));
}

return $this->sendResponseForResource(TopicResource::make($topic), __('Topic preview retrieved successfully'));
}
}
52 changes: 52 additions & 0 deletions src/Http/Controllers/Swagger/CourseAPISwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,56 @@ public function sort(SortAPIRequest $request);
* )
*/
public function uniqueTags(): JsonResponse;


/**
* @OA\Get(
* path="/api/courses/{id}/preview/{topic_id}",
* summary="Preview the specified Course Topic is set to preview",
* tags={"Courses"},
* description="Get Topic with Topicable",
* @OA\Parameter(
* name="id",
* description="id of Course",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Parameter(
* name="topic_id",
* description="id of Topic",
* @OA\Schema(
* type="integer",
* ),
* required=true,
* in="path"
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\MediaType(
* mediaType="application/json"
* ),
* @OA\Schema(
* type="object",
* @OA\Property(
* property="success",
* type="boolean"
* ),
* @OA\Property(
* property="data",
* ref="#/components/schemas/Topic"
* ),
* @OA\Property(
* property="message",
* type="string"
* )
* )
* )
* )
*/

public function preview($id, $topic_id, GetCourseAPIRequest $request);
}
6 changes: 6 additions & 0 deletions src/Http/Requests/GetCourseAPIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Courses\Http\Requests;

use EscolaLms\Courses\Models\Course;
use EscolaLms\Courses\Models\Topic;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

Expand Down Expand Up @@ -37,4 +38,9 @@ public function getCourse(): ?Course
{
return Course::find($this->route('course'));
}

public function getTopic(): ?Topic
{
return Topic::find($this->route('topic_id'));
}
}
33 changes: 24 additions & 9 deletions src/Http/Resources/TopicSimpleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@

namespace EscolaLms\Courses\Http\Resources;

use EscolaLms\Auth\Traits\ResourceExtandable;
use EscolaLms\Courses\Facades\Topic;
use Illuminate\Http\Resources\Json\JsonResource;

class TopicSimpleResource extends TopicResource
class TopicSimpleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function toArray($request)
{
$response = parent::toArray($request);
if (!$this->resource->preview) {
unset($response['topicable']);
unset($response['resources']);
}

return $response;
return [
'id' => $this->resource->id,
'title' => $this->resource->title,
'lesson_id' => $this->resource->lesson_id,
'active' => $this->resource->active,
'preview' => $this->resource->preview,
'topicable_id' => $this->resource->topicable_id,
'topicable_type' => $this->resource->topicable_type,
'summary' => $this->resource->summary,
'introduction' => $this->resource->introduction,
'description' => $this->resource->description,
'order' => $this->resource->order,
'json' => $this->resource->json,
'can_skip' => $this->resource->can_skip,
'duration' => $this->resource->duration,
];
}
}
1 change: 1 addition & 0 deletions src/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
* @property int $order
* @property int $topicable_id
* @property string $topicable_type
* @property bool $preview
*/
class Topic extends Model
{
Expand Down
1 change: 1 addition & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
// user endpoints
Route::group(['prefix' => 'api/courses'], function () {
Route::get('/{course}/program', [CourseAPIController::class, 'program'])->middleware('cacheResponse');
Route::get('/{course}/preview/{topic_id}', [CourseAPIController::class, 'preview']);

Route::group(['middleware' => ['auth:api']], function () {
Route::get('/{course}/scorm', [CourseAPIController::class, 'scorm']);
Expand Down
2 changes: 1 addition & 1 deletion testbench.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
env:
- DB_CONNECTION=pgsql
- DB_HOST=127.0.0.1
- DB_HOST=postgres
- DB_PORT=5432
- DB_DATABASE=default
- DB_USERNAME=default
Expand Down
Loading

0 comments on commit 2a534a2

Please sign in to comment.