From 14a89f77b3ed85a0cbe81d3ba844ef4fdb657b92 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Tue, 21 Dec 2021 20:50:32 +0300 Subject: [PATCH] feat: added size column to lesson model & return materials to lesson response --- .../Http/Api/v1/LessonsController.ts | 2 +- app/Models/LessonMaterial.ts | 3 +++ app/Repositories/CourseRepository.ts | 6 ++++- app/Repositories/LessonRepository.ts | 3 ++- app/Services/LessonService.ts | 22 ++++++++++++++++++- database/factories/index.ts | 2 +- .../1637080400230_lesson_materials.ts | 1 + 7 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/Controllers/Http/Api/v1/LessonsController.ts b/app/Controllers/Http/Api/v1/LessonsController.ts index 90a9101..ada9a96 100644 --- a/app/Controllers/Http/Api/v1/LessonsController.ts +++ b/app/Controllers/Http/Api/v1/LessonsController.ts @@ -49,7 +49,7 @@ export default class LessonsController extends BaseController { * GET /lessons/:id */ public async show(ctx: HttpContextContract) { - const result = await this.lessonService.fetchLesson(ctx.params.id); + const result = await this.lessonService.fetchLesson(ctx.params.id, ctx); if (!result.success && result.error) { throw new Exception(result.message, result.status, result.error.code); diff --git a/app/Models/LessonMaterial.ts b/app/Models/LessonMaterial.ts index 44c3db5..4958ba5 100644 --- a/app/Models/LessonMaterial.ts +++ b/app/Models/LessonMaterial.ts @@ -13,6 +13,9 @@ export default class LessonMaterial extends BaseModel { @column() public name: string; + @column() + public size: number; + @column() public clientName: string; diff --git a/app/Repositories/CourseRepository.ts b/app/Repositories/CourseRepository.ts index 12abf80..c43fcbd 100644 --- a/app/Repositories/CourseRepository.ts +++ b/app/Repositories/CourseRepository.ts @@ -157,7 +157,11 @@ export default class CourseRepository { * @returns List of lessons or null */ public async getLessons(id: string | number): Promise { - const course = await this.Course.query().select('id').preload('lessons').where('id', id).first(); + const course = await this.Course.query() + .select('id') + .preload('lessons', q => q.preload('color').orderBy('display_order', 'asc')) + .where('id', id) + .first(); return course && course.lessons; } diff --git a/app/Repositories/LessonRepository.ts b/app/Repositories/LessonRepository.ts index e93662b..867d612 100644 --- a/app/Repositories/LessonRepository.ts +++ b/app/Repositories/LessonRepository.ts @@ -43,7 +43,7 @@ export default class LessonRepository { * @returns Lesson or null */ public async getById(id: string | number): Promise { - const lesson = await this.Lesson.query().where('id', id).first(); + const lesson = await this.Lesson.query().where('id', id).preload('color').withCount('materials').first(); return lesson; } @@ -119,6 +119,7 @@ export default class LessonRepository { const material = new LessonMaterial(); material.name = file.fileName; + material.size = file.size; material.clientName = file.clientName; material.ext = file.extname; material.url = url; diff --git a/app/Services/LessonService.ts b/app/Services/LessonService.ts index dcba84c..f161322 100644 --- a/app/Services/LessonService.ts +++ b/app/Services/LessonService.ts @@ -54,7 +54,7 @@ export default class LessonService { * @param id Lesson id * @returns Response */ - public async fetchLesson(id: string | number): Promise { + public async fetchLesson(id: string | number, ctx: HttpContextContract): Promise { const lesson = await this.lessonRepository.getById(id); if (!lesson) { @@ -69,6 +69,26 @@ export default class LessonService { }; } + /** + * Allow user to view lesson content + */ + if (await ctx.bouncer.denies('viewLessonContent', lesson)) { + return { + success: false, + status: HttpStatusEnum.FORBIDDEN, + message: 'The user is not a student of this course.', + data: {}, + error: { + code: 'E_FORBIDDEN', + }, + }; + } + + /** + * Load lesson with materials + */ + await lesson.load(loader => loader.load('content').load('materials')); + return { success: true, status: HttpStatusEnum.OK, diff --git a/database/factories/index.ts b/database/factories/index.ts index 2ea0717..9e53a04 100644 --- a/database/factories/index.ts +++ b/database/factories/index.ts @@ -56,7 +56,7 @@ export const StudentFactory = Factory.define(User, ({ faker }) => makeFakeUser(f export const LessonContentFactory = Factory.define(LessonContent, ({ faker }) => { return { - video_url: `https://www.youtube.com/embed/${faker.datatype.string(10)}`, + video_url: `/uploads/videos/${faker.datatype.string(10)}`, }; }).build(); diff --git a/database/migrations/1637080400230_lesson_materials.ts b/database/migrations/1637080400230_lesson_materials.ts index 56ddb25..a16936d 100644 --- a/database/migrations/1637080400230_lesson_materials.ts +++ b/database/migrations/1637080400230_lesson_materials.ts @@ -8,6 +8,7 @@ export default class LessonMaterials extends BaseSchema { table.increments('id').primary(); table.string('url').notNullable(); table.string('name').notNullable(); + table.integer('size').notNullable(); table.string('client_name').notNullable(); table.string('ext').notNullable(); table.string('lesson_id', 21).unsigned().references('lessons.id').onDelete('CASCADE');