From aae79b582b02369eff19a857deae717c90010cfb Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Fri, 19 Nov 2021 23:57:00 +0300 Subject: [PATCH] feat: added color table --- app/Datatypes/Enums/ColorEnum.ts | 32 ++++++++++++++++++++ app/Models/Color.ts | 13 ++++++++ app/Models/Lesson.ts | 9 ++++++ app/Repositories/CourseRepository.ts | 2 +- app/Repositories/UserRepository.ts | 3 -- database/factories/index.ts | 5 +-- database/migrations/1617351321869_colors.ts | 25 +++++++++++++++ database/migrations/1627300211260_courses.ts | 2 +- database/migrations/1628016919501_lessons.ts | 1 + database/seeders/Color.ts | 18 +++++++++++ database/seeders/MainSeeder/Index.ts | 1 + 11 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 app/Datatypes/Enums/ColorEnum.ts create mode 100644 app/Models/Color.ts create mode 100644 database/migrations/1617351321869_colors.ts create mode 100644 database/seeders/Color.ts diff --git a/app/Datatypes/Enums/ColorEnum.ts b/app/Datatypes/Enums/ColorEnum.ts new file mode 100644 index 0000000..a71f6e7 --- /dev/null +++ b/app/Datatypes/Enums/ColorEnum.ts @@ -0,0 +1,32 @@ +enum ColorEnum { + Beige = '#F5F5DC', + IndianRed = '#CD5C5C', + FireBrick = '#B22222', + Pink = '#FFC0CB', + Tomato = '#FF6347', + Orange = '#FFA500', + Gold = '#FFD700', + Moccasin = '#FFE4B5', + Khaki = '#F0E68C', + Purple = '#800080', + Indigo = '#4B0082', + Teal = '#008080', + LightGray = '#D3D3D3', + LightSlateGray = '#778899', + LightGreen = '#90EE90', + LightSeaGreen = '#20B2AA', + LightCoral = '#FFA07A', + LightCyan = '#E0FFFF', + LightSteelBlue = '#B0C4DE', + LightBlue = '#ADD8E6', + LightSkyBlue = '#87CEFA', + MediumVioletRed = '#C71585', + MediumOrchid = '#BA55D3', + MediumPurple = '#9370DB', + MediumSlateBlue = '#7B68EE', + MediumSeaGreen = '#3CB371', + MediumAquamarine = '#66CDAA', + MediumTurquoise = '#48D1CC', +} + +export default ColorEnum; diff --git a/app/Models/Color.ts b/app/Models/Color.ts new file mode 100644 index 0000000..38586ce --- /dev/null +++ b/app/Models/Color.ts @@ -0,0 +1,13 @@ +import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'; +import ColorEnum from 'App/Datatypes/Enums/ColorEnum'; + +export default class Color extends BaseModel { + @column({ isPrimary: true }) + public id: number; + + @column() + public name: string; + + @column() + public hex: ColorEnum; +} diff --git a/app/Models/Lesson.ts b/app/Models/Lesson.ts index 75eca39..bf21314 100644 --- a/app/Models/Lesson.ts +++ b/app/Models/Lesson.ts @@ -5,6 +5,7 @@ import { BaseModel, beforeCreate, BelongsTo, belongsTo, column, HasOne, hasOne } import Course from './Course'; // eslint-disable-next-line import/no-cycle import LessonContent from './LessonContent'; +import Color from './Color'; export default class Lesson extends BaseModel { @column({ isPrimary: true }) @@ -27,6 +28,14 @@ export default class Lesson extends BaseModel { }) public course: BelongsTo; + @column({ serializeAs: null }) + public color_id: number; + + @belongsTo(() => Color, { + foreignKey: 'color_id', + }) + public color: BelongsTo; + @hasOne(() => LessonContent, { foreignKey: 'lesson_id', }) diff --git a/app/Repositories/CourseRepository.ts b/app/Repositories/CourseRepository.ts index f5edffb..dff103d 100644 --- a/app/Repositories/CourseRepository.ts +++ b/app/Repositories/CourseRepository.ts @@ -76,7 +76,7 @@ export default class CourseRepository { const course = await this.Course.query() .preload('teacher') .preload('category') - .preload('lessons') + .preload('lessons', q => q.preload('color')) .preload('students') .where('id', id) .first(); diff --git a/app/Repositories/UserRepository.ts b/app/Repositories/UserRepository.ts index b312df1..2ff2e3f 100644 --- a/app/Repositories/UserRepository.ts +++ b/app/Repositories/UserRepository.ts @@ -15,11 +15,8 @@ import UpdateUserValidator from 'App/Validators/User/UpdateUserValidator'; export default class UserRepository { private User: typeof User; - private Role: typeof Role; - constructor() { this.User = User; - this.Role = Role; } /** diff --git a/database/factories/index.ts b/database/factories/index.ts index 34862c1..e0f4c04 100644 --- a/database/factories/index.ts +++ b/database/factories/index.ts @@ -61,13 +61,14 @@ export const LessonContentFactory = Factory.define(LessonContent, ({ faker }) => export const LessonFactory = Factory.define(Lesson, ({ faker }) => { return { - title: `Lesson #${faker.datatype.number(200)}`, - description: faker.lorem.sentence(5), + title: faker.lorem.sentence(6), + description: faker.lorem.sentence(7), duration: DateTime.fromObject({ hour: faker.datatype.number(2), minute: faker.datatype.number({ min: 30, max: 59 }), second: 0, }).toFormat('HH:mm:ss'), + color_id: faker.datatype.number({ min: 1, max: 25 }), }; }) .relation('content', () => LessonContentFactory) diff --git a/database/migrations/1617351321869_colors.ts b/database/migrations/1617351321869_colors.ts new file mode 100644 index 0000000..6d315f1 --- /dev/null +++ b/database/migrations/1617351321869_colors.ts @@ -0,0 +1,25 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema'; +import ColorEnum from 'App/Datatypes/Enums/ColorEnum'; + +export default class Colors extends BaseSchema { + protected tableName = 'colors'; + + public async up() { + this.schema.createTable(this.tableName, table => { + table.increments('id').primary(); + table + .enu('name', Object.keys(ColorEnum), { + useNative: true, + enumName: 'color_name_enum', + existingType: false, + schemaName: 'public', + }) + .notNullable(); + table.string('hex').notNullable(); + }); + } + + public async down() { + this.schema.dropTable(this.tableName); + } +} diff --git a/database/migrations/1627300211260_courses.ts b/database/migrations/1627300211260_courses.ts index 0ba9629..0e889e4 100644 --- a/database/migrations/1627300211260_courses.ts +++ b/database/migrations/1627300211260_courses.ts @@ -19,7 +19,7 @@ export default class Courses extends BaseSchema { table .enu('status', Object.values(CourseStatusEnum), { useNative: true, - enumName: 'course_status', + enumName: 'course_status_enum', existingType: false, schemaName: 'public', }) diff --git a/database/migrations/1628016919501_lessons.ts b/database/migrations/1628016919501_lessons.ts index 870d974..7545313 100644 --- a/database/migrations/1628016919501_lessons.ts +++ b/database/migrations/1628016919501_lessons.ts @@ -8,6 +8,7 @@ export default class Lessons extends BaseSchema { table.string('id', 21).primary(); table.string('course_id', 21).unsigned().references('courses.id').onDelete('CASCADE'); table.string('title').notNullable(); + table.integer('color_id').unsigned().references('colors.id'); table.string('description'); table.time('duration').notNullable(); table.timestamp('created_at', { useTz: true }); diff --git a/database/seeders/Color.ts b/database/seeders/Color.ts new file mode 100644 index 0000000..ca3217c --- /dev/null +++ b/database/seeders/Color.ts @@ -0,0 +1,18 @@ +import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'; +import ColorEnum from 'App/Datatypes/Enums/ColorEnum'; +import Color from 'App/Models/Color'; + +export default class RoleSeeder extends BaseSeeder { + private Color: typeof Color; + + public async run() { + this.Color = Color; + + await this.Color.createMany( + (Object.keys(ColorEnum) as Array).map(k => ({ + name: k, + hex: ColorEnum[k], + })) + ); + } +} diff --git a/database/seeders/MainSeeder/Index.ts b/database/seeders/MainSeeder/Index.ts index 53e1e56..d511c64 100644 --- a/database/seeders/MainSeeder/Index.ts +++ b/database/seeders/MainSeeder/Index.ts @@ -18,6 +18,7 @@ export default class IndexSeeder extends BaseSeeder { public async run() { await this.runSeeder(await import('../Role')); + await this.runSeeder(await import('../Color')); await this.runSeeder(await import('../User')); await this.runSeeder(await import('../Course')); }