From bdf64df493f9b7d41fb944926f3bc090a0fa7762 Mon Sep 17 00:00:00 2001 From: luzum Date: Fri, 30 Jul 2021 17:07:18 -0300 Subject: [PATCH] Tudo meio estranho --- queries.sql | 56 ++++++++++++++++++++++++++++++ src/data/createClass.ts | 21 ++++++----- src/data/createStudent.ts | 29 ++++++++-------- src/data/createTeacher.ts | 8 ++--- src/data/migrations.ts | 67 ++++++++++++++++++++++++++++++++++++ src/endpoints/addClass.ts | 13 +++---- src/endpoints/addStudent.ts | 12 +++---- src/endpoints/addTeachers.ts | 9 ++--- src/index.ts | 3 ++ src/resquest.rest | 1 + 10 files changed, 175 insertions(+), 44 deletions(-) create mode 100644 queries.sql create mode 100644 src/data/migrations.ts diff --git a/queries.sql b/queries.sql new file mode 100644 index 0000000..ff2a8a5 --- /dev/null +++ b/queries.sql @@ -0,0 +1,56 @@ + + + + + + CREATE TABLE class_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + start_date DATE NOT NULL, + end_date DATE NOT NULL, + module ENUM ('0','1','2','3','4','5','6','7') DEFAULT '0' + + ); + CREATE TABLE student_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + birth_date DATE NOT NULL, + class_id INT NOT NULL, + FOREIGN KEY (class_id) REFERENCES class_labenu(id) + ); + + CREATE TABLE teacher_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + birth_date DATE NOT NULL, + class_id INT NOT NULL, + FOREIGN KEY (class_id) REFERENCES class_labenu(id) + ); + + CREATE TABLE student_hobbies ( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL + ); + + CREATE TABLE student_hobbies_labenu( + id INT PRIMARY KEY NOt NULL, + student_id INT NOT NULL, + hobby_id INT NOT NULL, + FOREIGN KEY(student_id) REFERENCES student_labenu(id), + FOREIGN KEY(hobby_id) REFERENCES student_hobbies(id) + ); + + CREATE TABLE teacher_specialty( + id INT PRIMARY KEY NOT NULL, + specialty ENUM('React', 'Redux', 'CSS', 'Testes', 'Typescript','Programação Orientada a Objetos', 'Backend') + ); + + CREATE TABLE teacher_speacialty_labenu ( + id INT PRIMARY KEY NOT NULL, + teacher_id INT NOT NULL, + specialty_id INT NOT NULL, + FOREIGN KEY (teacher_id) REFERENCES teacher_labenu(id), + FOREIGN KEY (specialty_id) REFERENCES teacher_specialty(id) + ); diff --git a/src/data/createClass.ts b/src/data/createClass.ts index 8efd007..d01e00f 100644 --- a/src/data/createClass.ts +++ b/src/data/createClass.ts @@ -1,16 +1,19 @@ import { connection } from "../connection" export const createClass = async ( - id: number, name: string, start_date: string, - end_date: string + end_date: string, + module: number ): Promise => { - await connection.raw(` - INSERT INTO class_labenu - (id, name, start_date, end_date) - VALUES ('${id}', '${name}', '${start_date}', '${end_date}') - ` - - ) + await connection ('class_labenu').insert ({ + id: 1, + name, + start_date, + end_date, + module + }) + + + } \ No newline at end of file diff --git a/src/data/createStudent.ts b/src/data/createStudent.ts index a6c80f4..7d9f3df 100644 --- a/src/data/createStudent.ts +++ b/src/data/createStudent.ts @@ -1,17 +1,18 @@ import { connection } from "../connection" export const createStudent = async ( - id: number, - name: string, - email: string, - birth_date: string, - hobbies: string - ): Promise => { - await connection.raw(` - INSERT INTO student_labenu - (id, name, email, birth_date, hobbies) - VALUES ('${id}', '${name}', '${email}', '${birth_date}', '${hobbies}') - ` - - ) - } \ No newline at end of file + + name: string, + email: string, + birth_date: Date, + +): Promise => { + await connection('student_labenu').insert({ + id: 1, + name, + email, + birth_date, + class_id: '1' + + }) +} \ No newline at end of file diff --git a/src/data/createTeacher.ts b/src/data/createTeacher.ts index 8d21249..52d7ebb 100644 --- a/src/data/createTeacher.ts +++ b/src/data/createTeacher.ts @@ -8,10 +8,10 @@ export const createTeacher = async ( ): Promise => { await connection.raw(` - INSERT INTO teachers_labenu - (id, name, email, birth_date) - VALUES ('${id}', '${name}', '${email}', '${birth_date}') + INSERT INTO teacher_labenu + (id, name, email, birth_date, class_id) + VALUES ('${id}', '${name}', '${email}', '${birth_date}', '1') ` - ) + ); } \ No newline at end of file diff --git a/src/data/migrations.ts b/src/data/migrations.ts new file mode 100644 index 0000000..7fa13e1 --- /dev/null +++ b/src/data/migrations.ts @@ -0,0 +1,67 @@ +import {connection} from "../connection" +import {Request, Response} from "express" + + + +export const createTables = async(req: Request, res: Response): Promise => { + try {connection.raw(` + CREATE TABLE class_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + start_date DATE NOT NULL, + end_date DATE NOT NULL, + module ENUM ('0','1','2','3','4','5','6','7') DEFAULT '0' + + ); + CREATE TABLE student_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + birth_date DATE NOT NULL, + class_id INT NOT NULL, + FOREIGN KEY (class_id) REFERENCES class_labenu(id) + ); + + CREATE TABLE teacher_labenu( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + birth_date DATE NOT NULL, + class_id INT NOT NULL, + FOREIGN KEY (class_id) REFERENCES class_labenu(id) + ); + + CREATE TABLE student_hobbies ( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(255) NOT NULL + ); + + CREATE TABLE student_hobbies_labenu( + id INT PRIMARY KEY NOt NULL, + student_id INT NOT NULL, + hobby_id INT NOT NULL, + FOREIGN KEY(student_id) REFERENCES student_labenu(id), + FOREIGN KEY(hobby_id) REFERENCES student_hobbies(id) + ); + + CREATE TABLE teacher_specialty( + id INT PRIMARY KEY NOT NULL, + specialty ENUM('React', 'Redux', 'CSS', 'Testes', 'Typescript','Programação Orientada a Objetos', 'Backend') + ); + + CREATE TABLE teacher_speacialty_labenu ( + id INT PRIMARY KEY NOT NULL, + teacher_id INT NOT NULL, + specialty_id INT NOT NULL, + FOREIGN KEY (teacher_id) REFERENCES teacher_labenu(id), + FOREIGN KEY (specialty_id) REFERENCES teacher_specialty(id) + ) +`) + res.status(200).send('Criou as tabelas') + + } catch (error) { + res.send(error.message || error.sqlMessage) + } + +} + diff --git a/src/endpoints/addClass.ts b/src/endpoints/addClass.ts index 05af72a..702bfb9 100644 --- a/src/endpoints/addClass.ts +++ b/src/endpoints/addClass.ts @@ -3,14 +3,15 @@ import { createClass } from '../data/createClass'; -export const addClass = async (req:Request, res:Response) => { +export const addClass = async (req:Request, res:Response): Promise => { try { + + const {name, start_date, end_date, module} = req.body await createClass( - req.body.id, - req.body.name, - req.body.start_date, - req.body.end_date - + name, + start_date, + end_date, + module ); diff --git a/src/endpoints/addStudent.ts b/src/endpoints/addStudent.ts index 571d444..1bd02d4 100644 --- a/src/endpoints/addStudent.ts +++ b/src/endpoints/addStudent.ts @@ -6,14 +6,12 @@ import {Request, Response} from "express"; export const addStudent = async (req:Request, res:Response) => { try { + const {name, email, birth_date} = req.body await createStudent( - req.body.id, - req.body.name, - req.body.email, - req.body.birth_date, - req.body.hobbies - - ); + name, + email, + birth_date, + ); res.status(200).send("Estudante criado com sucesso!") diff --git a/src/endpoints/addTeachers.ts b/src/endpoints/addTeachers.ts index 43b4d3d..1a67ac6 100644 --- a/src/endpoints/addTeachers.ts +++ b/src/endpoints/addTeachers.ts @@ -4,11 +4,12 @@ import { createTeacher } from './../data/createTeacher'; export const addTeachers = async (req:Request, res:Response) => { try { + const {id, name, email, birth_date} = req.body await createTeacher( - req.body.id, - req.body.name, - req.body.email, - req.body.birth_date, + id, + name, + email, + birth_date, ); diff --git a/src/index.ts b/src/index.ts index 22359af..6efaf76 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,12 @@ import {app} from "./app" +import { createTables } from "./data/migrations"; import { addClass } from "./endpoints/addClass"; import { addStudent } from "./endpoints/addStudent" import { addTeachers } from "./endpoints/addTeachers"; +app.post('/createTable', createTables); + app.post("/createStudent", addStudent); diff --git a/src/resquest.rest b/src/resquest.rest index e69de29..3d76178 100644 --- a/src/resquest.rest +++ b/src/resquest.rest @@ -0,0 +1 @@ +POST http://localhost:3003/createTable \ No newline at end of file