From a4adf27831d457aee18d21b7ca5657639c02e849 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Wed, 24 Nov 2021 23:21:16 +0300 Subject: [PATCH] feat: assign random color to lesson & deleted some colors in enum type --- app/Datatypes/Enums/ColorEnum.ts | 9 --------- app/Models/Lesson.ts | 10 ++++++++++ app/Repositories/LessonRepository.ts | 3 +++ database/factories/index.ts | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/Datatypes/Enums/ColorEnum.ts b/app/Datatypes/Enums/ColorEnum.ts index a71f6e7..4ee5616 100644 --- a/app/Datatypes/Enums/ColorEnum.ts +++ b/app/Datatypes/Enums/ColorEnum.ts @@ -11,15 +11,6 @@ enum ColorEnum { 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', diff --git a/app/Models/Lesson.ts b/app/Models/Lesson.ts index a7f6b22..30e79b2 100644 --- a/app/Models/Lesson.ts +++ b/app/Models/Lesson.ts @@ -2,6 +2,7 @@ import { DateTime } from 'luxon'; import { nanoid } from 'nanoid'; import { BaseModel, beforeCreate, BelongsTo, belongsTo, column, HasOne, hasOne } from '@ioc:Adonis/Lucid/Orm'; +import ColorHelper from 'App/Helpers/ColorHelper'; import Course from './Course'; // eslint-disable-next-line import/no-cycle import LessonContent from './LessonContent'; @@ -58,4 +59,13 @@ export default class Lesson extends BaseModel { public static assignLessonId(lesson: Lesson) { lesson.id = nanoid(); } + + @beforeCreate() + public static async assignRandomColor(course: Course) { + const color = await ColorHelper.generateRandomColor(); + + if (color) { + course.color_id = color.id; + } + } } diff --git a/app/Repositories/LessonRepository.ts b/app/Repositories/LessonRepository.ts index 9eff7fc..437144e 100644 --- a/app/Repositories/LessonRepository.ts +++ b/app/Repositories/LessonRepository.ts @@ -72,10 +72,13 @@ export default class LessonRepository { * @returns Lesson */ public async create(course: Course, data: CreateLessonValidator['schema']['props']): Promise { + await course.loadCount('lessons'); + const lesson = await this.Lesson.create({ title: data.title, description: data.description, duration: data.duration.toFormat('HH:mm:ss'), + display_order: Number.parseInt(course.$extras.lessons_count, 10) + 1, }); /** diff --git a/database/factories/index.ts b/database/factories/index.ts index 1678e0c..b1c2c90 100644 --- a/database/factories/index.ts +++ b/database/factories/index.ts @@ -70,7 +70,7 @@ export const LessonFactory = Factory.define(Lesson, ({ faker }) => { minute: faker.datatype.number({ min: 30, max: 59 }), second: 0, }).toFormat('HH:mm:ss'), - color_id: faker.datatype.number({ min: 1, max: 25 }), + color_id: faker.datatype.number({ min: 1, max: 15 }), }; }) .relation('content', () => LessonContentFactory)