init Backend
This commit is contained in:
parent
08a4e0d8fa
commit
924000b084
29 changed files with 10080 additions and 13 deletions
218
Backend/prisma/seed.js
Normal file
218
Backend/prisma/seed.js
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
const { PrismaClient } = require('@prisma/client');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('🌱 Starting database seeding...');
|
||||
|
||||
// Clear existing data (in development only)
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('🗑️ Clearing existing data...');
|
||||
await prisma.quizAttempt.deleteMany();
|
||||
await prisma.quizQuestion.deleteMany();
|
||||
await prisma.quiz.deleteMany();
|
||||
await prisma.lessonProgress.deleteMany();
|
||||
await prisma.attachment.deleteMany();
|
||||
await prisma.lessonPrerequisite.deleteMany();
|
||||
await prisma.lesson.deleteMany();
|
||||
await prisma.chapter.deleteMany();
|
||||
await prisma.enrollment.deleteMany();
|
||||
await prisma.courseInstructor.deleteMany();
|
||||
await prisma.course.deleteMany();
|
||||
await prisma.category.deleteMany();
|
||||
await prisma.certificate.deleteMany();
|
||||
await prisma.profile.deleteMany();
|
||||
await prisma.user.deleteMany();
|
||||
await prisma.role.deleteMany();
|
||||
}
|
||||
|
||||
// Seed Roles
|
||||
console.log('👥 Seeding roles...');
|
||||
const roles = await Promise.all([
|
||||
prisma.role.create({
|
||||
data: {
|
||||
code: 'ADMIN',
|
||||
name: { th: 'ผู้ดูแลระบบ', en: 'Administrator' },
|
||||
description: { th: 'มีสิทธิ์เข้าถึงทุกฟังก์ชัน', en: 'Full system access' }
|
||||
}
|
||||
}),
|
||||
prisma.role.create({
|
||||
data: {
|
||||
code: 'INSTRUCTOR',
|
||||
name: { th: 'ผู้สอน', en: 'Instructor' },
|
||||
description: { th: 'สามารถสร้างและจัดการคอร์สเรียน', en: 'Can create and manage courses' }
|
||||
}
|
||||
}),
|
||||
prisma.role.create({
|
||||
data: {
|
||||
code: 'STUDENT',
|
||||
name: { th: 'นักเรียน', en: 'Student' },
|
||||
description: { th: 'สามารถลงทะเบียนและเรียนคอร์ส', en: 'Can enroll and learn courses' }
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
// Seed Users
|
||||
console.log('👤 Seeding users...');
|
||||
const hashedPassword = await bcrypt.hash('admin123', 10);
|
||||
|
||||
const admin = await prisma.user.create({
|
||||
data: {
|
||||
username: 'admin',
|
||||
email: 'admin@elearning.local',
|
||||
password: hashedPassword,
|
||||
role_id: roles[0].id,
|
||||
profile: {
|
||||
create: {
|
||||
first_name: 'Admin',
|
||||
last_name: 'User',
|
||||
bio: { th: 'ผู้ดูแลระบบ', en: 'System Administrator' }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const instructor = await prisma.user.create({
|
||||
data: {
|
||||
username: 'instructor',
|
||||
email: 'instructor@elearning.local',
|
||||
password: hashedPassword,
|
||||
role_id: roles[1].id,
|
||||
profile: {
|
||||
create: {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
bio: { th: 'ผู้สอนมืออาชีพ', en: 'Professional Instructor' }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const student = await prisma.user.create({
|
||||
data: {
|
||||
username: 'student',
|
||||
email: 'student@elearning.local',
|
||||
password: hashedPassword,
|
||||
role_id: roles[2].id,
|
||||
profile: {
|
||||
create: {
|
||||
first_name: 'Jane',
|
||||
last_name: 'Smith',
|
||||
bio: { th: 'นักเรียนที่กระตือรือร้น', en: 'Eager learner' }
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Seed Categories
|
||||
console.log('📚 Seeding categories...');
|
||||
const categories = await Promise.all([
|
||||
prisma.category.create({
|
||||
data: {
|
||||
code: 'PROGRAMMING',
|
||||
name: { th: 'การเขียนโปรแกรม', en: 'Programming' },
|
||||
description: { th: 'เรียนรู้การเขียนโปรแกรมและพัฒนาซอฟต์แวร์', en: 'Learn programming and software development' }
|
||||
}
|
||||
}),
|
||||
prisma.category.create({
|
||||
data: {
|
||||
code: 'DESIGN',
|
||||
name: { th: 'การออกแบบ', en: 'Design' },
|
||||
description: { th: 'เรียนรู้การออกแบบกราฟิกและ UI/UX', en: 'Learn graphic design and UI/UX' }
|
||||
}
|
||||
}),
|
||||
prisma.category.create({
|
||||
data: {
|
||||
code: 'BUSINESS',
|
||||
name: { th: 'ธุรกิจ', en: 'Business' },
|
||||
description: { th: 'เรียนรู้การบริหารธุรกิจและการตลาด', en: 'Learn business management and marketing' }
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
// Seed Sample Course
|
||||
console.log('🎓 Seeding sample course...');
|
||||
const course = await prisma.course.create({
|
||||
data: {
|
||||
title: { th: 'พื้นฐาน JavaScript', en: 'JavaScript Fundamentals' },
|
||||
description: {
|
||||
th: 'เรียนรู้พื้นฐาน JavaScript ตั้งแต่เริ่มต้น',
|
||||
en: 'Learn JavaScript fundamentals from scratch'
|
||||
},
|
||||
price: 0,
|
||||
is_free: true,
|
||||
have_certificate: true,
|
||||
status: 'APPROVED',
|
||||
category_id: categories[0].id,
|
||||
created_by: instructor.id,
|
||||
instructors: {
|
||||
create: {
|
||||
user_id: instructor.id,
|
||||
is_primary: true
|
||||
}
|
||||
},
|
||||
chapters: {
|
||||
create: [
|
||||
{
|
||||
title: { th: 'บทที่ 1: เริ่มต้น', en: 'Chapter 1: Getting Started' },
|
||||
description: { th: 'แนะนำ JavaScript', en: 'Introduction to JavaScript' },
|
||||
order: 1,
|
||||
lessons: {
|
||||
create: [
|
||||
{
|
||||
title: { th: 'JavaScript คืออะไร', en: 'What is JavaScript' },
|
||||
description: { th: 'เรียนรู้ว่า JavaScript คืออะไร', en: 'Learn what JavaScript is' },
|
||||
type: 'VIDEO',
|
||||
order: 1,
|
||||
is_preview: true
|
||||
},
|
||||
{
|
||||
title: { th: 'ตัวแปรและชนิดข้อมูล', en: 'Variables and Data Types' },
|
||||
description: { th: 'เรียนรู้เกี่ยวกับตัวแปร', en: 'Learn about variables' },
|
||||
type: 'VIDEO',
|
||||
order: 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
title: { th: 'บทที่ 2: ฟังก์ชัน', en: 'Chapter 2: Functions' },
|
||||
description: { th: 'เรียนรู้เกี่ยวกับฟังก์ชัน', en: 'Learn about functions' },
|
||||
order: 2,
|
||||
lessons: {
|
||||
create: [
|
||||
{
|
||||
title: { th: 'การสร้างฟังก์ชัน', en: 'Creating Functions' },
|
||||
description: { th: 'เรียนรู้วิธีสร้างฟังก์ชัน', en: 'Learn how to create functions' },
|
||||
type: 'VIDEO',
|
||||
order: 1
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
console.log('✅ Database seeding completed!');
|
||||
console.log('\n📊 Summary:');
|
||||
console.log(`- Roles: ${roles.length}`);
|
||||
console.log(`- Users: 3 (admin, instructor, student)`);
|
||||
console.log(`- Categories: ${categories.length}`);
|
||||
console.log(`- Courses: 1`);
|
||||
console.log('\n🔑 Test Credentials:');
|
||||
console.log('Admin: admin / admin123');
|
||||
console.log('Instructor: instructor / admin123');
|
||||
console.log('Student: student / admin123');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error('❌ Error seeding database:', e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue