From 11f0d3009221f56dba424d9d00930867f4dc54c7 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sat, 18 Dec 2021 21:07:48 +0300 Subject: [PATCH] feat: added new fields to course model --- app/Models/Course.ts | 3 +++ app/Repositories/CourseRepository.ts | 7 ++++++- app/Services/MeService.ts | 5 ++++- app/Validators/Contacts/CreateContactsValidator.ts | 8 ++++---- app/Validators/Course/CreateCourseValidator.ts | 1 + app/Validators/Course/UpdateCourseValidator.ts | 1 + database/factories/index.ts | 1 + database/migrations/1627300211260_courses.ts | 1 + 8 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/Models/Course.ts b/app/Models/Course.ts index e8f0a9b..ff271e6 100644 --- a/app/Models/Course.ts +++ b/app/Models/Course.ts @@ -34,6 +34,9 @@ export default class Course extends BaseModel { @column() public description: string; + @column() + public education_description: string | null; + @column({ serializeAs: null }) public teacher_id: string; diff --git a/app/Repositories/CourseRepository.ts b/app/Repositories/CourseRepository.ts index 8010eeb..12abf80 100644 --- a/app/Repositories/CourseRepository.ts +++ b/app/Repositories/CourseRepository.ts @@ -81,9 +81,12 @@ export default class CourseRepository { const course = await this.Course.query() .preload('teacher') .preload('color') - .preload('category') + .preload('category', q => q.preload('color')) .preload('lessons', q => q.preload('color').orderBy('display_order', 'asc').withCount('materials')) .preload('students', q => q.preload('roles').preload('contacts')) + .withCount('likes') + .withCount('lessons') + .withCount('students') .where('id', id) .first(); @@ -194,6 +197,7 @@ export default class CourseRepository { course.title = data.title; course.description = data.description; + course.education_description = data.education_description; course.status = data.status; course.teacher_id = data.teacher_id; course.category_id = data.category_id; @@ -255,6 +259,7 @@ export default class CourseRepository { course.merge({ title: data.title, description: data.description, + education_description: data.education_description, teacher_id: data.teacher_id, category_id: data.category_id, }); diff --git a/app/Services/MeService.ts b/app/Services/MeService.ts index f10032e..5fb3854 100644 --- a/app/Services/MeService.ts +++ b/app/Services/MeService.ts @@ -37,7 +37,10 @@ export default class MeService { const user = await auth.use(this.authGuard).authenticate(); await user.load(loader => { - loader.load('roles').load('contacts'); + loader + .load('roles') + .load('contacts') + .load('likes', q => q.select('id')); }); /** diff --git a/app/Validators/Contacts/CreateContactsValidator.ts b/app/Validators/Contacts/CreateContactsValidator.ts index 999a5bb..e41d565 100644 --- a/app/Validators/Contacts/CreateContactsValidator.ts +++ b/app/Validators/Contacts/CreateContactsValidator.ts @@ -5,15 +5,15 @@ export default class CreateContactsValidator { constructor(protected ctx: HttpContextContract) {} public schema = schema.create({ - phone_number: schema.string.optional({}, [ + phone_number: schema.string.nullableAndOptional({}, [ rules.mobile({ locales: ['ru-RU', 'en-US'], strict: true, }), ]), - vk_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), - twitter_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), - telegram_id: schema.string.optional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), + vk_id: schema.string.nullableAndOptional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), + twitter_id: schema.string.nullableAndOptional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), + telegram_id: schema.string.nullableAndOptional({}, [rules.regex(/^[a-zA-Z0-9_]+$/)]), }); public messages = {}; diff --git a/app/Validators/Course/CreateCourseValidator.ts b/app/Validators/Course/CreateCourseValidator.ts index 23d8ce1..337a186 100644 --- a/app/Validators/Course/CreateCourseValidator.ts +++ b/app/Validators/Course/CreateCourseValidator.ts @@ -8,6 +8,7 @@ export default class CreateCourseValidator { public schema = schema.create({ title: schema.string(), description: schema.string(), + education_description: schema.string.nullable(), image: schema.file.optional({ size: '10mb', extnames: ['jpg', 'jpeg', 'png'], diff --git a/app/Validators/Course/UpdateCourseValidator.ts b/app/Validators/Course/UpdateCourseValidator.ts index a5096c1..c8c8a8d 100644 --- a/app/Validators/Course/UpdateCourseValidator.ts +++ b/app/Validators/Course/UpdateCourseValidator.ts @@ -7,6 +7,7 @@ export default class UpdateCourseValidator { public schema = schema.create({ title: schema.string.optional(), description: schema.string.optional(), + education_description: schema.string.nullable(), image: schema.file.optional({ size: '10mb', extnames: ['jpg', 'jpeg', 'png'], diff --git a/database/factories/index.ts b/database/factories/index.ts index b1c2c90..2ea0717 100644 --- a/database/factories/index.ts +++ b/database/factories/index.ts @@ -87,6 +87,7 @@ export const CourseFactory = Factory.define(Course, ({ faker }) => { return { title: faker.lorem.sentence(8), description: faker.lorem.sentence(20), + education_description: faker.lorem.sentence(15), status: CourseStatusEnum.PUBLISHED, }; }) diff --git a/database/migrations/1627300211260_courses.ts b/database/migrations/1627300211260_courses.ts index 9657c28..0040390 100644 --- a/database/migrations/1627300211260_courses.ts +++ b/database/migrations/1627300211260_courses.ts @@ -14,6 +14,7 @@ export default class Courses extends BaseSchema { table.json('image'); table.string('title').notNullable(); table.string('description').notNullable(); + table.string('education_description', 250); table.string('teacher_id', 21).unsigned().references('users.id').notNullable().onDelete('CASCADE'); table.string('category_id', 21).unsigned().references('categories.id').notNullable().onDelete('CASCADE'); table.integer('color_id').unsigned().references('colors.id');