api login

This commit is contained in:
JakkrapartXD 2026-01-09 10:14:13 +00:00
parent bd0daf5858
commit 1a7473362b
8 changed files with 613 additions and 60 deletions

View file

@ -37,22 +37,28 @@ async function main() {
// Seed Roles
console.log('👥 Seeding roles...');
const roles = await Promise.all([
prisma.role.create({
data: {
prisma.role.upsert({
where: { code: 'ADMIN' },
update: {},
create: {
code: 'ADMIN',
name: { th: 'ผู้ดูแลระบบ', en: 'Administrator' },
description: { th: 'มีสิทธิ์เข้าถึงทุกฟังก์ชัน', en: 'Full system access' }
}
}),
prisma.role.create({
data: {
prisma.role.upsert({
where: { code: 'INSTRUCTOR' },
update: {},
create: {
code: 'INSTRUCTOR',
name: { th: 'ผู้สอน', en: 'Instructor' },
description: { th: 'สามารถสร้างและจัดการคอร์สเรียน', en: 'Can create and manage courses' }
}
}),
prisma.role.create({
data: {
prisma.role.upsert({
where: { code: 'STUDENT' },
update: {},
create: {
code: 'STUDENT',
name: { th: 'นักเรียน', en: 'Student' },
description: { th: 'สามารถลงทะเบียนและเรียนคอร์ส', en: 'Can enroll and learn courses' }
@ -64,8 +70,10 @@ async function main() {
console.log('👤 Seeding users...');
const hashedPassword = await bcrypt.hash('admin123', 10);
const admin = await prisma.user.create({
data: {
const admin = await prisma.user.upsert({
where: { username: 'admin' },
update: {},
create: {
username: 'admin',
email: 'admin@elearning.local',
password: hashedPassword,
@ -81,8 +89,10 @@ async function main() {
}
});
const instructor = await prisma.user.create({
data: {
const instructor = await prisma.user.upsert({
where: { username: 'instructor' },
update: {},
create: {
username: 'instructor',
email: 'instructor@elearning.local',
password: hashedPassword,
@ -98,8 +108,10 @@ async function main() {
}
});
const student = await prisma.user.create({
data: {
const student = await prisma.user.upsert({
where: { username: 'student' },
update: {},
create: {
username: 'student',
email: 'student@elearning.local',
password: hashedPassword,
@ -118,8 +130,10 @@ async function main() {
// Seed Categories
console.log('📚 Seeding categories...');
const categories = await Promise.all([
prisma.category.create({
data: {
prisma.category.upsert({
where: { slug: 'programming' },
update: {},
create: {
name: { th: 'การเขียนโปรแกรม', en: 'Programming' },
slug: 'programming',
description: { th: 'เรียนรู้การเขียนโปรแกรมและพัฒนาซอฟต์แวร์', en: 'Learn programming and software development' },
@ -128,8 +142,10 @@ async function main() {
created_by: admin.id
}
}),
prisma.category.create({
data: {
prisma.category.upsert({
where: { slug: 'design' },
update: {},
create: {
name: { th: 'การออกแบบ', en: 'Design' },
slug: 'design',
description: { th: 'เรียนรู้การออกแบบกราฟิกและ UI/UX', en: 'Learn graphic design and UI/UX' },
@ -138,8 +154,10 @@ async function main() {
created_by: admin.id
}
}),
prisma.category.create({
data: {
prisma.category.upsert({
where: { slug: 'business' },
update: {},
create: {
name: { th: 'ธุรกิจ', en: 'Business' },
slug: 'business',
description: { th: 'เรียนรู้การบริหารธุรกิจและการตลาด', en: 'Learn business management and marketing' },
@ -152,7 +170,15 @@ async function main() {
// Seed Sample Course
console.log('🎓 Seeding sample course...');
const course = await prisma.course.create({
// Check if course already exists
let course = await prisma.course.findUnique({
where: { slug: 'javascript-fundamentals' },
include: { chapters: { include: { lessons: true } } }
});
if (!course) {
course = await prisma.course.create({
data: {
title: { th: 'พื้นฐาน JavaScript', en: 'JavaScript Fundamentals' },
slug: 'javascript-fundamentals',
@ -351,6 +377,9 @@ async function main() {
created_by: instructor.id
}
});
} else {
console.log(' ⏭️ Course already exists, skipping...');
}
console.log('✅ Database seeding completed!');
console.log('\n📊 Summary:');