feat: send materials count to frontend

This commit is contained in:
Sergey Yarkov 2021-12-07 19:11:15 +03:00
parent 56493f97b4
commit d403b7b87a
9 changed files with 44 additions and 27 deletions

View File

@ -1,10 +1,21 @@
/* eslint-disable import/no-cycle */
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { BaseModel, beforeCreate, BelongsTo, belongsTo, column, HasOne, hasOne } from '@ioc:Adonis/Lucid/Orm'; import {
BaseModel,
beforeCreate,
BelongsTo,
belongsTo,
column,
hasMany,
HasMany,
HasOne,
hasOne,
} from '@ioc:Adonis/Lucid/Orm';
import ColorHelper from 'App/Helpers/ColorHelper'; import ColorHelper from 'App/Helpers/ColorHelper';
import Course from './Course'; import Course from './Course';
// eslint-disable-next-line import/no-cycle import LessonMaterial from './LessonMaterial';
import LessonContent from './LessonContent'; import LessonContent from './LessonContent';
import Color from './Color'; import Color from './Color';
@ -45,6 +56,9 @@ export default class Lesson extends BaseModel {
}) })
public content: HasOne<typeof LessonContent>; public content: HasOne<typeof LessonContent>;
@hasMany(() => LessonMaterial, { foreignKey: 'lesson_id' })
public materials: HasMany<typeof LessonMaterial>;
@column.dateTime({ autoCreate: true }) @column.dateTime({ autoCreate: true })
public createdAt: DateTime; public createdAt: DateTime;
@ -68,4 +82,13 @@ export default class Lesson extends BaseModel {
course.color_id = color.id; course.color_id = color.id;
} }
} }
/**
* Serialize the `$extras` object
*/
public serializeExtras() {
return {
materials_count: this.$extras.materials_count,
};
}
} }

View File

@ -1,8 +1,7 @@
/* eslint-disable import/no-cycle */ /* eslint-disable import/no-cycle */
import { BaseModel, BelongsTo, belongsTo, column, HasMany, hasMany } from '@ioc:Adonis/Lucid/Orm'; import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import Lesson from './Lesson'; import Lesson from './Lesson';
import LessonMaterial from './LessonMaterial';
export default class LessonContent extends BaseModel { export default class LessonContent extends BaseModel {
@column({ isPrimary: true }) @column({ isPrimary: true })
@ -14,9 +13,6 @@ export default class LessonContent extends BaseModel {
@column() @column()
public video_url: string; public video_url: string;
@hasMany(() => LessonMaterial, { foreignKey: 'lesson_content_id' })
public materials: HasMany<typeof LessonMaterial>;
@belongsTo(() => Lesson, { foreignKey: 'lesson_id' }) @belongsTo(() => Lesson, { foreignKey: 'lesson_id' })
public lesson: BelongsTo<typeof Lesson>; public lesson: BelongsTo<typeof Lesson>;

View File

@ -1,7 +1,7 @@
/* eslint-disable import/no-cycle */
import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm'; import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
// eslint-disable-next-line import/no-cycle import Lesson from './Lesson';
import LessonContent from './LessonContent';
export default class LessonMaterial extends BaseModel { export default class LessonMaterial extends BaseModel {
@column({ isPrimary: true }) @column({ isPrimary: true })
@ -20,12 +20,12 @@ export default class LessonMaterial extends BaseModel {
public ext: string; public ext: string;
@column() @column()
public lesson_content_id: number; public lesson_id: string;
@belongsTo(() => LessonContent, { @belongsTo(() => Lesson, {
foreignKey: 'lesson_content_id', foreignKey: 'lesson_id',
}) })
public content: BelongsTo<typeof LessonContent>; public lesson: BelongsTo<typeof Lesson>;
@column.dateTime({ autoCreate: true }) @column.dateTime({ autoCreate: true })
public createdAt: DateTime; public createdAt: DateTime;

View File

@ -62,10 +62,10 @@ export default class User extends BaseModel {
}) })
public likes: ManyToMany<typeof Course>; public likes: ManyToMany<typeof Course>;
@column.dateTime({ autoCreate: true, serializeAs: null }) @column.dateTime({ autoCreate: true })
public createdAt: DateTime; public createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true, serializeAs: null }) @column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime; public updatedAt: DateTime;
/** /**

View File

@ -82,7 +82,7 @@ export default class CourseRepository {
.preload('teacher') .preload('teacher')
.preload('color') .preload('color')
.preload('category') .preload('category')
.preload('lessons', q => q.preload('color').orderBy('display_order', 'asc')) .preload('lessons', q => q.preload('color').orderBy('display_order', 'asc').withCount('materials'))
.preload('students') .preload('students')
.where('id', id) .where('id', id)
.first(); .first();

View File

@ -89,7 +89,7 @@ export default class LessonRepository {
/** /**
* Create content and materials fields in database * Create content and materials fields in database
*/ */
const content = await lesson.related('content').create({ video_url: data.video_url }); await lesson.related('content').create({ video_url: data.video_url });
if (data.materials) { if (data.materials) {
await Promise.all( await Promise.all(
@ -100,7 +100,7 @@ export default class LessonRepository {
await file.moveToDisk('materials'); await file.moveToDisk('materials');
if (file.state === 'moved') { if (file.state === 'moved') {
const url = await Drive.getUrl(`materials/${file.fileName}`); const url = await Drive.getUrl(`materials/${file.fileName}`);
await content.related('materials').create({ await lesson.related('materials').create({
name: file.fileName, name: file.fileName,
clientName: file.clientName, clientName: file.clientName,
ext: file.extname, ext: file.extname,
@ -114,7 +114,7 @@ export default class LessonRepository {
/** /**
* Load data * Load data
*/ */
await lesson.load('content', q => q.preload('materials')); await lesson.load(loader => loader.load('content').load('materials'));
return lesson; return lesson;
} }

View File

@ -101,12 +101,9 @@ export default class LessonService {
/** /**
* Allow user to view lesson material * Allow user to view lesson material
*/ */
await material.load('content', q => q.preload('lesson')); await material.load('lesson');
const { if (await ctx.bouncer.denies('viewLessonContent', material.lesson)) {
content: { lesson },
} = material;
if (await ctx.bouncer.denies('viewLessonContent', lesson)) {
return { return {
success: false, success: false,
status: HttpStatusEnum.FORBIDDEN, status: HttpStatusEnum.FORBIDDEN,
@ -254,9 +251,9 @@ export default class LessonService {
} }
/** /**
* Load lesson content with materials * Load lesson with materials
*/ */
await lesson.load('content', q => q.preload('materials')); await lesson.load(loader => loader.load('content').load('materials'));
return { return {
success: true, success: true,

View File

@ -10,6 +10,7 @@ export default class UpdateContactsValidator {
locales: ['ru-RU', 'en-US'], locales: ['ru-RU', 'en-US'],
strict: true, strict: true,
}), }),
// TODO validate correctly
rules.unique({ table: 'contacts', column: 'phone_number' }), rules.unique({ table: 'contacts', column: 'phone_number' }),
]), ]),
vk_id: schema.string.optional({}, [rules.regex(/^([a-zA-Z0-9_]){1,64}$/)]), vk_id: schema.string.optional({}, [rules.regex(/^([a-zA-Z0-9_]){1,64}$/)]),

View File

@ -10,7 +10,7 @@ export default class LessonMaterials extends BaseSchema {
table.string('name').notNullable(); table.string('name').notNullable();
table.string('client_name').notNullable(); table.string('client_name').notNullable();
table.string('ext').notNullable(); table.string('ext').notNullable();
table.integer('lesson_content_id').unsigned().references('lesson_contents.id').onDelete('CASCADE'); table.string('lesson_id', 21).unsigned().references('lessons.id').onDelete('CASCADE');
table.timestamp('created_at', { useTz: true }); table.timestamp('created_at', { useTz: true });
table.timestamp('updated_at', { useTz: true }); table.timestamp('updated_at', { useTz: true });
}); });