mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
feat: added color table
This commit is contained in:
parent
1a9bddd0d3
commit
aae79b582b
32
app/Datatypes/Enums/ColorEnum.ts
Normal file
32
app/Datatypes/Enums/ColorEnum.ts
Normal file
@ -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;
|
||||
13
app/Models/Color.ts
Normal file
13
app/Models/Color.ts
Normal file
@ -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;
|
||||
}
|
||||
@ -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<typeof Course>;
|
||||
|
||||
@column({ serializeAs: null })
|
||||
public color_id: number;
|
||||
|
||||
@belongsTo(() => Color, {
|
||||
foreignKey: 'color_id',
|
||||
})
|
||||
public color: BelongsTo<typeof Color>;
|
||||
|
||||
@hasOne(() => LessonContent, {
|
||||
foreignKey: 'lesson_id',
|
||||
})
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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)
|
||||
|
||||
25
database/migrations/1617351321869_colors.ts
Normal file
25
database/migrations/1617351321869_colors.ts
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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',
|
||||
})
|
||||
|
||||
@ -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 });
|
||||
|
||||
18
database/seeders/Color.ts
Normal file
18
database/seeders/Color.ts
Normal file
@ -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<keyof typeof ColorEnum>).map(k => ({
|
||||
name: k,
|
||||
hex: ColorEnum[k],
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -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'));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user