From 937b0d57943030c57350197efab16c181a4439d1 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Mon, 26 Jul 2021 14:55:53 +0300 Subject: [PATCH] added Course and Category --- app/Models/Category.ts | 22 ++++++++ app/Models/Course.ts | 44 +++++++++++++++ database/migrations/1626964029348_roles.ts | 4 +- .../migrations/1627295886598_categories.ts | 20 +++++++ database/migrations/1627300211260_courses.ts | 21 ++++++++ database/seeders/Category.ts | 29 ++++++++++ database/seeders/Course.ts | 53 +++++++++++++++++++ database/seeders/MainSeeder/Index.ts | 2 + 8 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 app/Models/Category.ts create mode 100644 app/Models/Course.ts create mode 100644 database/migrations/1627295886598_categories.ts create mode 100644 database/migrations/1627300211260_courses.ts create mode 100644 database/seeders/Category.ts create mode 100644 database/seeders/Course.ts diff --git a/app/Models/Category.ts b/app/Models/Category.ts new file mode 100644 index 0000000..7f8255a --- /dev/null +++ b/app/Models/Category.ts @@ -0,0 +1,22 @@ +import { DateTime } from 'luxon'; +import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'; + +export default class Category extends BaseModel { + @column({ isPrimary: true }) + public id: number; + + @column() + public title: string; + + @column() + public slug: string; + + @column() + public description: string; + + @column.dateTime({ autoCreate: true }) + public createdAt: DateTime; + + @column.dateTime({ autoCreate: true, autoUpdate: true }) + public updatedAt: DateTime; +} diff --git a/app/Models/Course.ts b/app/Models/Course.ts new file mode 100644 index 0000000..a554208 --- /dev/null +++ b/app/Models/Course.ts @@ -0,0 +1,44 @@ +/* eslint-disable no-param-reassign */ +import { nanoid } from 'nanoid'; +import { DateTime } from 'luxon'; +import { BaseModel, beforeCreate, column, HasOne, hasOne } from '@ioc:Adonis/Lucid/Orm'; +import User from './User'; +import Category from './Category'; + +export default class Course extends BaseModel { + @column({ isPrimary: true }) + public id: string; + + @column() + public title: string; + + @column() + public description: string; + + @column() + public teacher_id: string; + + @column() + public category_id: number; + + @hasOne(() => User) + public teacher: HasOne; + + @hasOne(() => Category) + public category: HasOne; + + @column.dateTime({ autoCreate: true }) + public createdAt: DateTime; + + @column.dateTime({ autoCreate: true, autoUpdate: true }) + public updatedAt: DateTime; + + /** + * Assign id to course by nanoid. + * @param user User + */ + @beforeCreate() + public static assignCourseId(course: Course) { + course.id = nanoid(); + } +} diff --git a/database/migrations/1626964029348_roles.ts b/database/migrations/1626964029348_roles.ts index 0ccec49..12e78df 100644 --- a/database/migrations/1626964029348_roles.ts +++ b/database/migrations/1626964029348_roles.ts @@ -6,8 +6,8 @@ export default class Roles extends BaseSchema { public async up() { this.schema.createTable(this.tableName, table => { table.increments('id'); - table.string('name'); - table.string('slug'); + table.string('name').notNullable(); + table.string('slug').unique().notNullable(); table.timestamp('created_at', { useTz: true }); table.timestamp('updated_at', { useTz: true }); }); diff --git a/database/migrations/1627295886598_categories.ts b/database/migrations/1627295886598_categories.ts new file mode 100644 index 0000000..99a5f91 --- /dev/null +++ b/database/migrations/1627295886598_categories.ts @@ -0,0 +1,20 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema'; + +export default class Categories extends BaseSchema { + protected tableName = 'categories'; + + public async up() { + this.schema.createTable(this.tableName, table => { + table.increments('id'); + table.string('title').notNullable(); + table.string('slug').unique().notNullable(); + table.string('description'); + table.timestamp('created_at', { useTz: true }); + table.timestamp('updated_at', { useTz: true }); + }); + } + + public async down() { + this.schema.dropTable(this.tableName); + } +} diff --git a/database/migrations/1627300211260_courses.ts b/database/migrations/1627300211260_courses.ts new file mode 100644 index 0000000..ce7df65 --- /dev/null +++ b/database/migrations/1627300211260_courses.ts @@ -0,0 +1,21 @@ +import BaseSchema from '@ioc:Adonis/Lucid/Schema'; + +export default class Courses extends BaseSchema { + protected tableName = 'courses'; + + public async up() { + this.schema.createTable(this.tableName, table => { + table.string('id', 21).primary(); + table.string('title').notNullable(); + table.string('description').notNullable(); + table.string('teacher_id', 21).unsigned().references('users.id').onDelete('CASCADE'); + table.integer('category_id').unsigned().references('categories.id').onDelete('CASCADE'); + table.timestamp('created_at', { useTz: true }); + table.timestamp('updated_at', { useTz: true }); + }); + } + + public async down() { + this.schema.dropTable(this.tableName); + } +} diff --git a/database/seeders/Category.ts b/database/seeders/Category.ts new file mode 100644 index 0000000..3287e4e --- /dev/null +++ b/database/seeders/Category.ts @@ -0,0 +1,29 @@ +import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'; +import Category from 'App/Models/Category'; + +export default class CategorySeeder extends BaseSeeder { + private Category: typeof Category; + + public async run() { + this.Category = Category; + + const category1 = new this.Category(); + category1.title = 'JavaScript'; + category1.slug = 'javascript'; + category1.description = 'Category 1 description'; + + const category2 = new this.Category(); + category2.title = 'ReactJS'; + category2.slug = 'reactjs'; + category2.description = 'Category 2 description'; + + const category3 = new this.Category(); + category3.title = 'NodeJS'; + category3.slug = 'nodejs'; + category3.description = 'Category 3 description'; + + await category1.save(); + await category2.save(); + await category3.save(); + } +} diff --git a/database/seeders/Course.ts b/database/seeders/Course.ts new file mode 100644 index 0000000..b0a6602 --- /dev/null +++ b/database/seeders/Course.ts @@ -0,0 +1,53 @@ +import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'; +import Category from 'App/Models/Category'; +import Course from 'App/Models/Course'; +import User from 'App/Models/User'; + +export default class CourseSeeder extends BaseSeeder { + private Course: typeof Course; + + private User: typeof User; + + private Category: typeof Category; + + public async run() { + this.Course = Course; + this.User = User; + this.Category = Category; + + const teacher = await this.User.findByOrFail('login', 'nikolai123'); + + /** + * Course 1 + */ + const category1 = await this.Category.findByOrFail('slug', 'javascript'); + const course1 = new this.Course(); + course1.title = 'Course 1'; + course1.description = 'Course 1 description'; + course1.teacher_id = teacher.id; + course1.category_id = category1.id; + await course1.save(); + + /** + * Course 2 + */ + const category2 = await this.Category.findByOrFail('slug', 'reactjs'); + const course2 = new this.Course(); + course2.title = 'Course 2'; + course2.description = 'Course 2 description'; + course2.teacher_id = teacher.id; + course2.category_id = category2.id; + await course2.save(); + + /** + * Course 3 + */ + const category3 = await this.Category.findByOrFail('slug', 'nodejs'); + const course3 = new this.Course(); + course3.title = 'Course 3'; + course3.description = 'Course 3 description'; + course3.teacher_id = teacher.id; + course3.category_id = category3.id; + await course3.save(); + } +} diff --git a/database/seeders/MainSeeder/Index.ts b/database/seeders/MainSeeder/Index.ts index 87157f4..c8a7e53 100644 --- a/database/seeders/MainSeeder/Index.ts +++ b/database/seeders/MainSeeder/Index.ts @@ -19,5 +19,7 @@ export default class IndexSeeder extends BaseSeeder { public async run() { await this.runSeeder(await import('../Role')); await this.runSeeder(await import('../User')); + await this.runSeeder(await import('../Category')); + await this.runSeeder(await import('../Course')); } }