Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -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)
);
21 changes: 12 additions & 9 deletions src/data/createClass.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
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
})


Comment on lines +9 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O id precisa ser único, por enquanto podem até usar o Date.now().toString() ou o lib uuid https://www.npmjs.com/package/uuid


}
29 changes: 15 additions & 14 deletions src/data/createStudent.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { connection } from "../connection"

export const createStudent = async (
id: number,
name: string,
email: string,
birth_date: string,
hobbies: string
): Promise<void> => {
await connection.raw(`
INSERT INTO student_labenu
(id, name, email, birth_date, hobbies)
VALUES ('${id}', '${name}', '${email}', '${birth_date}', '${hobbies}')
`

)
}

name: string,
email: string,
birth_date: Date,

): Promise<void> => {
await connection('student_labenu').insert({
id: 1,
name,
email,
birth_date,
class_id: '1'

})
}
8 changes: 4 additions & 4 deletions src/data/createTeacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export const createTeacher = async (

): Promise<void> => {
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')
`

)
);
}
67 changes: 67 additions & 0 deletions src/data/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {connection} from "../connection"
import {Request, Response} from "express"



export const createTables = async(req: Request, res: Response): Promise<void> => {
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,
Comment on lines +7 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faltou o await antes do connection.raw await connection.raw()

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)
}

}

13 changes: 7 additions & 6 deletions src/endpoints/addClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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,
Comment on lines +8 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

È importante fazer as validações antes de tentar inserir os dados no Banco:

dica: pode usar o thow new Error("nome do erro")

end_date,
module
);


Expand Down
12 changes: 5 additions & 7 deletions src/endpoints/addStudent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
9 changes: 5 additions & 4 deletions src/endpoints/addTeachers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,


);
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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);


Expand Down
1 change: 1 addition & 0 deletions src/resquest.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POST http://localhost:3003/createTable