mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-12 02:39:09 +03:00
added Course and Category
This commit is contained in:
parent
df1d1716d5
commit
937b0d5794
22
app/Models/Category.ts
Normal file
22
app/Models/Category.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
44
app/Models/Course.ts
Normal file
44
app/Models/Course.ts
Normal file
@ -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<typeof User>;
|
||||||
|
|
||||||
|
@hasOne(() => Category)
|
||||||
|
public category: HasOne<typeof Category>;
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,8 +6,8 @@ export default class Roles extends BaseSchema {
|
|||||||
public async up() {
|
public async up() {
|
||||||
this.schema.createTable(this.tableName, table => {
|
this.schema.createTable(this.tableName, table => {
|
||||||
table.increments('id');
|
table.increments('id');
|
||||||
table.string('name');
|
table.string('name').notNullable();
|
||||||
table.string('slug');
|
table.string('slug').unique().notNullable();
|
||||||
table.timestamp('created_at', { useTz: true });
|
table.timestamp('created_at', { useTz: true });
|
||||||
table.timestamp('updated_at', { useTz: true });
|
table.timestamp('updated_at', { useTz: true });
|
||||||
});
|
});
|
||||||
|
|||||||
20
database/migrations/1627295886598_categories.ts
Normal file
20
database/migrations/1627295886598_categories.ts
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
database/migrations/1627300211260_courses.ts
Normal file
21
database/migrations/1627300211260_courses.ts
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
29
database/seeders/Category.ts
Normal file
29
database/seeders/Category.ts
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
53
database/seeders/Course.ts
Normal file
53
database/seeders/Course.ts
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -19,5 +19,7 @@ export default class IndexSeeder extends BaseSeeder {
|
|||||||
public async run() {
|
public async run() {
|
||||||
await this.runSeeder(await import('../Role'));
|
await this.runSeeder(await import('../Role'));
|
||||||
await this.runSeeder(await import('../User'));
|
await this.runSeeder(await import('../User'));
|
||||||
|
await this.runSeeder(await import('../Category'));
|
||||||
|
await this.runSeeder(await import('../Course'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user