forked from tomasvanpez2/True-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudents.config.js
More file actions
86 lines (74 loc) · 2.7 KB
/
Copy pathstudents.config.js
File metadata and controls
86 lines (74 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');
const path = require('path');
const studentsFilePath = path.join(__dirname, 'students.json');
// Función para cargar estudiantes desde el archivo JSON
function loadStudents() {
if (fs.existsSync(studentsFilePath)) {
try {
const data = fs.readFileSync(studentsFilePath, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Error al cargar students.json:', error);
return [];
}
} return [];
}
// Función para guardar estudiantes en el archivo JSON
function saveStudents(students) {
try {
fs.writeFileSync(studentsFilePath, JSON.stringify(students, null, 2), 'utf8');
} catch (error) {
console.error('Error al guardar en students.json:', error);
}
}
module.exports = {
// Obtener todos los estudiantes de un curso
getStudentsByCourse: function (course) {
const students = loadStudents();
return students.filter(s => s.course === course);
},
// Agregar un nuevo estudiante
addStudent: function (studentData) {
const { name, course } = studentData;
if (!name || !course) {
return { success: false, message: 'Datos incompletos' };
}
const students = loadStudents();
const newStudent = {
id: uuidv4(),
name,
course
};
students.push(newStudent);
saveStudents(students);
return { success: true, student: newStudent };
},
// Actualizar un estudiante existente
updateStudent: function (studentId, updatedData) {
const students = loadStudents();
const studentIndex = students.findIndex(s => s.id === studentId);
if (studentIndex === -1) {
return { success: false, message: 'Estudiante no encontrado' };
}
students[studentIndex] = { ...students[studentIndex], ...updatedData };
saveStudents(students);
return { success: true, student: students[studentIndex] };
},
// Eliminar un estudiante
deleteStudent: function (studentId) {
let students = loadStudents();
const initialLength = students.length;
students = students.filter(s => s.id !== studentId);
if (students.length === initialLength) {
return { success: false, message: 'Estudiante no encontrado' };
}
saveStudents(students);
return { success: true, message: 'Estudiante eliminado correctamente' };
},
// Obtener un estudiante por su ID
getStudentById: function (studentId) {
const students = loadStudents();
return students.find(s => s.id === studentId) || null;
}
};