-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexercise.ts
More file actions
126 lines (110 loc) · 2.83 KB
/
exercise.ts
File metadata and controls
126 lines (110 loc) · 2.83 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import Database from "better-sqlite3";
// Ejercicio: Many-to-Many (N:M)
// Objetivo: 'student' ↔ 'course' con tabla intermedia 'enrollment'
// Instrucciones: completar los TODO con SQL para crear tablas e insertar datos.
export type Student = {
id?: number;
studentId: string;
firstName: string;
lastName: string;
program: string;
};
export type Course = {
id?: number;
courseCode: string;
courseName: string;
credits: number;
};
export type Enrollment = {
id?: number;
studentId: number; // FK a student.id
courseId: number; // FK a course.id
letterGrade?:
| "A+"
| "A"
| "A-"
| "B+"
| "B"
| "B-"
| "C+"
| "C"
| "C-"
| "D+"
| "D"
| "F";
};
// Instancias para persistir
export const students: Student[] = [
{
id: 1,
studentId: "STU001",
firstName: "Ana",
lastName: "García",
program: "CS",
},
{
id: 2,
studentId: "STU002",
firstName: "Carlos",
lastName: "Ruiz",
program: "CS",
},
];
export const courses: Course[] = [
{ id: 1, courseCode: "CS101", courseName: "Intro Programming", credits: 3 },
{ id: 2, courseCode: "CS201", courseName: "Data Structures", credits: 4 },
];
export const enrollments: Enrollment[] = [
{ id: 1, studentId: 1, courseId: 1, letterGrade: "A-" },
{ id: 2, studentId: 1, courseId: 2, letterGrade: "B+" },
{ id: 3, studentId: 2, courseId: 1, letterGrade: "A" },
];
export class ManyToManyExercise {
private db: Database.Database;
constructor() {
this.db = new Database("ejercicio-many-to-many.sqlite");
}
async run(): Promise<void> {
try {
await this.createSchema();
await this.insertData();
console.log("✅ N:M: datos listos. Agregá consultas si querés.");
} finally {
this.db.close();
}
}
async createSchema(): Promise<void> {
console.log(
"🔧 TODO: Crear 'student', 'course' y 'enrollment' (junction con UNIQUE)"
);
// TODO: Crear tablas y constraint UNIQUE(student_id, course_id) en enrollment
}
async insertData(): Promise<void> {
console.log("📝 TODO: Insertar 'students', 'courses' y 'enrollments'");
// TODO: Insertar primero entities, luego enrollment
}
// Consultas (a implementar como parte del ejercicio)
async getAllEnrollments(): Promise<
Array<Enrollment>
> {
// TODO: SELECT JOIN student/enrollment/course devolviendo campos mapeados
return [];
}
async getEnrollmentsByCourse(
courseCode: string
): Promise<
Array<{ firstName: string; lastName: string; letterGrade?: string }>
> {
// TODO: SELECT JOIN filtrando por course_code
return [];
}
async filterStudentsByProgram(
program: string
): Promise<Array<Student>> {
// TODO: SELECT filtrando por program
return [];
}
}
if (require.main === module) {
new ManyToManyExercise().run().catch(console.error);
}