feat: added size column to lesson model & return materials to lesson response

This commit is contained in:
Sergey Yarkov 2021-12-21 20:50:32 +03:00
parent 11f0d30092
commit 14a89f77b3
7 changed files with 34 additions and 5 deletions

View File

@ -49,7 +49,7 @@ export default class LessonsController extends BaseController {
* GET /lessons/:id * GET /lessons/:id
*/ */
public async show(ctx: HttpContextContract) { 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) { if (!result.success && result.error) {
throw new Exception(result.message, result.status, result.error.code); throw new Exception(result.message, result.status, result.error.code);

View File

@ -13,6 +13,9 @@ export default class LessonMaterial extends BaseModel {
@column() @column()
public name: string; public name: string;
@column()
public size: number;
@column() @column()
public clientName: string; public clientName: string;

View File

@ -157,7 +157,11 @@ export default class CourseRepository {
* @returns List of lessons or null * @returns List of lessons or null
*/ */
public async getLessons(id: string | number): Promise<Lesson[] | null> { public async getLessons(id: string | number): Promise<Lesson[] | null> {
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; return course && course.lessons;
} }

View File

@ -43,7 +43,7 @@ export default class LessonRepository {
* @returns Lesson or null * @returns Lesson or null
*/ */
public async getById(id: string | number): Promise<Lesson | null> { public async getById(id: string | number): Promise<Lesson | null> {
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; return lesson;
} }
@ -119,6 +119,7 @@ export default class LessonRepository {
const material = new LessonMaterial(); const material = new LessonMaterial();
material.name = file.fileName; material.name = file.fileName;
material.size = file.size;
material.clientName = file.clientName; material.clientName = file.clientName;
material.ext = file.extname; material.ext = file.extname;
material.url = url; material.url = url;

View File

@ -54,7 +54,7 @@ export default class LessonService {
* @param id Lesson id * @param id Lesson id
* @returns Response * @returns Response
*/ */
public async fetchLesson(id: string | number): Promise<IResponse> { public async fetchLesson(id: string | number, ctx: HttpContextContract): Promise<IResponse> {
const lesson = await this.lessonRepository.getById(id); const lesson = await this.lessonRepository.getById(id);
if (!lesson) { 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 { return {
success: true, success: true,
status: HttpStatusEnum.OK, status: HttpStatusEnum.OK,

View File

@ -56,7 +56,7 @@ export const StudentFactory = Factory.define(User, ({ faker }) => makeFakeUser(f
export const LessonContentFactory = Factory.define(LessonContent, ({ faker }) => { export const LessonContentFactory = Factory.define(LessonContent, ({ faker }) => {
return { return {
video_url: `https://www.youtube.com/embed/${faker.datatype.string(10)}`, video_url: `/uploads/videos/${faker.datatype.string(10)}`,
}; };
}).build(); }).build();

View File

@ -8,6 +8,7 @@ export default class LessonMaterials extends BaseSchema {
table.increments('id').primary(); table.increments('id').primary();
table.string('url').notNullable(); table.string('url').notNullable();
table.string('name').notNullable(); table.string('name').notNullable();
table.integer('size').notNullable();
table.string('client_name').notNullable(); table.string('client_name').notNullable();
table.string('ext').notNullable(); table.string('ext').notNullable();
table.string('lesson_id', 21).unsigned().references('lessons.id').onDelete('CASCADE'); table.string('lesson_id', 21).unsigned().references('lessons.id').onDelete('CASCADE');