diff --git a/request.rest b/request.rest deleted file mode 100644 index c3e4b9f..0000000 --- a/request.rest +++ /dev/null @@ -1,34 +0,0 @@ -POST http://localhost:3003/create-class -Content-Type: application/json - -{ - "nome":"Ada", - "data_inicio": "2019-09-01", - "data_final": "2020-04-01", - "tipo": "integral" -} - -### - -PUT http://localhost:3003/create-teacher -Content-Type: application/json - -{ - "nome":"Darvas", - "email": "darvas@labenu.com.br", - "data_nasc": "1990-05-20", - "turma_id": 1 -} - -### - -PUT http://localhost:3003/create-student -Content-Type: application/json - -{ - - "nome":"Allisson", - "email": "allisson@gmail.com", - "data_nasc": "1990-02-17", - "turma_id": 1 -} \ No newline at end of file diff --git a/src/app.ts b/src/app.ts deleted file mode 100644 index 9f82ffb..0000000 --- a/src/app.ts +++ /dev/null @@ -1,20 +0,0 @@ -import express, {Express} from 'express' -import cors from 'cors' -import { AddressInfo } from "net"; - - -const app: Express = express(); - -app.use(express.json()); -app.use(cors()); - -const server = app.listen(process.env.PORT || 3003, () => { - if (server) { - const address = server.address() as AddressInfo; - console.log(`Server is running in http://localhost: ${address.port}`); - } else { - console.error(`Failure upon starting server.`); - } -}); - -export default app; \ No newline at end of file diff --git a/src/connection.ts b/src/connection.ts deleted file mode 100644 index 7a133ce..0000000 --- a/src/connection.ts +++ /dev/null @@ -1,18 +0,0 @@ -import knex from "knex"; -import dotenv from "dotenv"; - -dotenv.config(); - -const connection = knex({ - client: "mysql", - connection: { - host: process.env.DB_HOST, - port: Number(process.env.DB_PORT || "3306"), - user: process.env.DB_USER, - password: process.env.DB_PASSWORD, - database: process.env.DB_SCHEMA, - multipleStatements: true - }, -}); - -export default connection \ No newline at end of file diff --git a/src/data/insertStudent.ts b/src/data/insertStudent.ts deleted file mode 100644 index 475ab04..0000000 --- a/src/data/insertStudent.ts +++ /dev/null @@ -1,15 +0,0 @@ -import connection from "../connection"; - -export default async function insertTeacher( - nome: string, - email: string, - data_nasc: Date, - turma_id: number -) { - await connection.insert({ - nome, - email, - data_nasc, - turma_id - }).into('students') -} \ No newline at end of file diff --git a/src/data/insertTeacher.ts b/src/data/insertTeacher.ts deleted file mode 100644 index 2809716..0000000 --- a/src/data/insertTeacher.ts +++ /dev/null @@ -1,15 +0,0 @@ -import connection from "../connection"; - -export default async function insertStudent( - nome: string, - email: string, - data_nasc: Date, - turma_id: number -) { - await connection.insert({ - nome, - email, - data_nasc, - turma_id - }).into('teacher') -} \ No newline at end of file diff --git a/src/data/selectAgeById.ts b/src/data/selectAgeById.ts deleted file mode 100644 index b1e47f5..0000000 --- a/src/data/selectAgeById.ts +++ /dev/null @@ -1,6 +0,0 @@ -import connection from "../connection"; - -export default async function selectAgeById( - id:number) { - const result = await connection('students') -} \ No newline at end of file diff --git a/src/endpoints/addStudent.ts b/src/endpoints/addStudent.ts deleted file mode 100644 index c068c93..0000000 --- a/src/endpoints/addStudent.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Request, Response } from "express" - - -export default async function addStudents( - req: Request, - res: Response -): Promise { - - try { - - } catch (error) { - res.status(500).end() - } -} \ No newline at end of file diff --git a/src/endpoints/addTeacher.ts b/src/endpoints/addTeacher.ts deleted file mode 100644 index 15277df..0000000 --- a/src/endpoints/addTeacher.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Request, Response } from "express" -import connection from "../connection" - -export default async function addTeacher( - req: Request, - res: Response -): Promise { - - try { - let { id, nome, email, data_nasc, turma_id } = req.body - - await connection("teacher") - .insert({id, nome, email, data_nasc, turma_id}) - res.status(201).send({message: 'Class created succesfully'}) - - - } catch (error) { - res.status(500).send({message: error.message || error.sqlMessage}) - } -} \ No newline at end of file diff --git a/src/endpoints/createClass.ts b/src/endpoints/createClass.ts deleted file mode 100644 index 64533c7..0000000 --- a/src/endpoints/createClass.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { Request, Response } from "express" -import connection from "../connection" -import { Class, TYPE_CLASS } from "../types" - - -export default async function createClass( - req: Request, - res: Response -): Promise { - - try { - let errorCode = 400; - - const input: Class = { - nome: req.body.nome, - data_inicio:req.body.data_inicio, - data_final:req.body.data_final, - modulo: 0, - tipo: req.body.tipo - } - - if(!input.nome || !input.data_inicio || - !input.data_final || !input.tipo) { - errorCode = 422; - throw new Error("Campos obrigatorios!") - } - - if(input.tipo !== TYPE_CLASS.INTEGRAL && input.tipo !== TYPE_CLASS.NOTURNA) { - errorCode = 422; - throw new Error("Campos obrigatorios!") - - } - if(input.tipo === TYPE_CLASS.NOTURNA) { - input.nome = input.nome += "-na-night"; - } - await connection.raw(` - INSERT INTO class ( nome, data_inicio, data_final, modulo) - VALUES( - "${req.body.nome}", - "${req.body.data_inicio}", - "${req.body.data_final}", - ${input.modulo} - ); - `); - res.status(201).send({ message: 'Class created successfully' }) - - } catch (error) { - res.status(500).send({message: error.message || error.sqlMessage}) - } -} \ No newline at end of file diff --git a/src/endpoints/createStudent.ts b/src/endpoints/createStudent.ts deleted file mode 100644 index edf3edc..0000000 --- a/src/endpoints/createStudent.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Request, Response } from "express" -import insertStudent from "../data/insertStudent" - - -export default async function createStudent( - req: Request, - res: Response -): Promise { - - try { - - if( - !req.body.nome || - !req.body.email || - !req.body.data_nasc || - !req.body.turma_id - ) - res.status(400).send("Campos obrigatorios") - - await insertStudent( - req.body.nome, - req.body.email, - req.body.data_nasc, - req.body.turma_id - ) - } catch (error) { - res.status(500).send({message: error.message || error.sqlMessage}) - } -} \ No newline at end of file diff --git a/src/endpoints/createTeacher.ts b/src/endpoints/createTeacher.ts deleted file mode 100644 index db5650d..0000000 --- a/src/endpoints/createTeacher.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Request, Response } from "express" -import insertTeacher from "../data/insertTeacher" - - -export default async function createTeacher( - req: Request, - res: Response -): Promise { - - try { - if( - !req.body.nome || - !req.body.email || - !req.body.data_nasc || - !req.body.turma_id - ) - res.status(400).send("Campos obrigatorios") - - - await insertTeacher( - req.body.nome, - req.body.email, - req.body.data_nasc, - req.body.turma_id - ) - res.status(200).send("Professor criado com sucesso!") - - - } catch (error) { - res.status(500).send({message: error.message || error.sqlMessage}) - } -} \ No newline at end of file diff --git a/src/endpoints/getAge.ts b/src/endpoints/getAge.ts deleted file mode 100644 index a4167ac..0000000 --- a/src/endpoints/getAge.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Request, Response } from "express" - - -export default async function getAge( - req: Request, - res: Response -): Promise { - - try { - - } catch (error) { - res.status(500).end() - } -} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index b1058b4..6e428fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,32 @@ -import app from "./app" -import addStudent from "./endpoints/addStudent" -import addTeacher from "./endpoints/addTeacher" -import createClass from "./endpoints/createClass" -import createStudent from "./endpoints/createStudent" -import createTeacher from "./endpoints/createTeacher" -import getAge from "./endpoints/getAge" - -app.put("/", addStudent); -app.put("/", addTeacher); -app.put("/create-student", createStudent); -app.put("/create-teacher", createTeacher); -app.post("/create-class", createClass); -app.get("/get-age/:id", getAge); +import express, {Express} from 'express' +import cors from 'cors' +import { AddressInfo } from "net"; +import knex from "knex"; +import dotenv from "dotenv"; + +dotenv.config(); + +export const connection = knex({ + client: "mysql", + connection: { + host: process.env.DB_HOST, + port: 3306, + user: process.env.DB_USER, + password: process.env.DB_PASS, + database: process.env.DB_NAME + } +}); + +const app: Express = express(); + +app.use(express.json()); +app.use(cors()); + +const server = app.listen(process.env.PORT || 3003, () => { + if (server) { + const address = server.address() as AddressInfo; + console.log(`Server is running in http://localhost: ${address.port}`); + } else { + console.error(`Failure upon starting server.`); + } +}); diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index 92d5a88..0000000 --- a/src/types.ts +++ /dev/null @@ -1,46 +0,0 @@ -export enum TYPE_CLASS { - INTEGRAL = "integral", - NOTURNA = "noturna" -} - -export type students = { - id: number, - nome: string, - email: string, - data_nasc: Date, - turma_id: number -}; - -export type hobby = { - id: number, - nome: string -} - -export type students_hobby = { - estudantes_id: number, - passatempo_id: number -} - -export type Class = { - nome: string, - data_inicio: Date, - data_final: Date, - modulo: number, - tipo: TYPE_CLASS -}; - -export type Teacher = { - id: number, - nome: string, - email: string, - data_nasc: Date, - turma_id: number -}; - -export type teacher_expertise = { - docente_id: number, - especialidade_id: number -}; - -export let classes: Class [] = [] - diff --git a/tables/class.sql b/tables/class.sql deleted file mode 100644 index cf0e7f4..0000000 --- a/tables/class.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE class ( -id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -nome VARCHAR(50) NOT NULL, -data_inicio DATE NOT NULL, -data_final DATE NOT NULL, -modulo INT NOT NULL -); - - - - - - - - - diff --git a/tables/students.sql b/tables/students.sql deleted file mode 100644 index c17e117..0000000 --- a/tables/students.sql +++ /dev/null @@ -1,21 +0,0 @@ -CREATE TABLE students ( -id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -nome VARCHAR(50) NOT NULL, -email VARCHAR(50) NOT NULL UNIQUE, -data_nasc DATE NOT NULL, -turma_id INT NOT NULL, -FOREIGN KEY (turma_id) REFERENCES class(id) -); - -CREATE TABLE hobby ( -id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -nome VARCHAR(50) NOT NULL -); - -CREATE TABLE students_hobby ( -estudantes_id INT NOT NULL AUTO_INCREMENT, -passatempo_id INT NOT NULL, -PRIMARY KEY(estudantes_id, passatempo_id), -FOREIGN KEY(estudantes_id) REFERENCES students(id), -FOREIGN KEY(passatempo_id) REFERENCES hobby(id) -); diff --git a/tables/tables.sql b/tables/tables.sql new file mode 100644 index 0000000..0712ff0 --- /dev/null +++ b/tables/tables.sql @@ -0,0 +1,55 @@ +CREATE TABLE class ( +id INT NOT NULL PRIMARY KEY, +nome VARCHAR(50) NOT NULL, +data_inicio DATE NOT NULL, +data_final DATE NOT NULL, +modulo INT NOT NULL +); + +CREATE TABLE teacher ( +id INT NOT NULL PRIMARY KEY, +nome VARCHAR(50) NOT NULL, +email VARCHAR(50) NOT NULL UNIQUE, +data_nasc DATE NOT NULL, +turma_id INT NOT NULL, +FOREIGN KEY (turma_id) REFERENCES class(id) +); + +CREATE TABLE expertise ( +id INT NOT NULL PRIMARY KEY, +nome VARCHAR(50) NOT NULL +); + +CREATE TABLE teacher_expertise ( +docente_id_especialidade_id INT NOT NULL PRIMARY KEY, +docente_id INT NOT NULL, +especialidade_id INT NOT NULL, +FOREIGN KEY(docente_id) REFERENCES teacher(id), +FOREIGN KEY(especialidade_id) REFERENCES expertise(id) +); + +CREATE TABLE students ( +id INT NOT NULL PRIMARY KEY, +nome VARCHAR(50) NOT NULL, +email VARCHAR(50) NOT NULL UNIQUE, +data_nasc DATE NOT NULL, +turma_id INT NOT NULL, +FOREIGN KEY (turma_id) REFERENCES class(id) +); + +CREATE TABLE hobby ( +id INT NOT NULL PRIMARY KEY, +nome VARCHAR(50) NOT NULL +); + +CREATE TABLE students_hobby ( +estudantes_id_passatempo_id INT NOT NULL PRIMARY KEY, +estudantes_id INT NOT NULL, +passatempo_id INT NOT NULL, +FOREIGN KEY(estudantes_id) REFERENCES students(id), +FOREIGN KEY(passatempo_id) REFERENCES hobby(id) +); + + + + diff --git a/tables/teacher.sql b/tables/teacher.sql deleted file mode 100644 index 43be497..0000000 --- a/tables/teacher.sql +++ /dev/null @@ -1,21 +0,0 @@ -CREATE TABLE teacher ( -id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -nome VARCHAR(50) NOT NULL, -email VARCHAR(50) NOT NULL UNIQUE, -data_nasc DATE NOT NULL, -turma_id INT NOT NULL, -FOREIGN KEY (turma_id) REFERENCES class(id) -); - -CREATE TABLE expertise ( -id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, -nome VARCHAR(50) NOT NULL -); - -CREATE TABLE teacher_expertise ( -docente_id INT NOT NULL AUTO_INCREMENT, -especialidade_id INT NOT NULL, -PRIMARY KEY (docente_id, especialidade_id), -FOREIGN KEY(docente_id) REFERENCES teacher(id), -FOREIGN KEY(especialidade_id) REFERENCES expertise(id) -); \ No newline at end of file