diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ece3ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules +package-lock.json +build +.env \ No newline at end of file diff --git a/README.md b/README.md index 978a24d..b12a3bf 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,28 @@ -## LabenuSystem: - -Você estuda na Labenu_ há tanto tempo que já parecem anos, não é? Então, hoje, vamos pedir para criar um sistema que represente o básico da nossa organização. - -Ele deve possuir, ao menos, as 3 entidades importantes: - -1. Estudantes - - Representa estudantes da nossa instituição. Eles devem possuir: id, nome, email, data de nascimento e os principais hobbies dele. - -2. Docente - - Representa docentes da nossa instituição. Eles devem possuir: id, nome, email, data de nascimento e todas as especialidades dele. Há 7 especialidades: React, Redux, CSS, Testes, Typescript, Programação Orientada a Objetos e Backend - -3. Turma - - Toda turma é composta das seguintes características: id, nome, data de início, data de término, lista de professores responsáveis, uma lista de alunos e módulo atual em que a turma está. - - O módulo pode assumir os valores de 1 a 7 ou `undefined`, indicando que as aulas dessa turma ainda não começaram. Para esse exercício, vamos considerar que existam dois tipos de turma: integral ou noturna. Há uma restrição para o nome das turmas noturnas: tem que terminar com `-na-night`. - -As funcionalidades básicas são: - -→ Criar estudante; - -→ Criar docente; - -→ Criar turma; - -→ Adicionar estudante na turma; - -→ Adicionar docente na turma; - -→ Pegar a idade de algum estudante a partir do id +## 🏦 LabenuSystem + +API desenvolvida para ser utilizada como um sistema organizacional de estudantes, docentes e turmas da Labenu. + +### 🔗 Link da documentação: +https://documenter.getpostman.com/view/19298136/UyrAGchs + +### ✔ O que funciona: +- Criar turma +- Buscar turmas ativas +- Mudar o módulo da turma +- Criar estudante +- Buscar estudantes por nome +- Mudar estudante de turma +- Criar docente +- Buscar docentes +- Mudar docente de turma + +### ❌ O que falta implementar: +- Exibir todas as pessoas pertencentes a uma mesma turma +- Exibir estudantes que tenham o mesmo hobby +- Exibir todos os docentes que tenham uma mesma especialidade +- Exibir todas as pessoas de um mesmo signo + +### 👩‍💻👨‍💻 Projeto desenvolvido por: +- Ana Sue Sammi +- Diane Silva de Araújo +- Vitor Duarte Passo diff --git a/package.json b/package.json new file mode 100644 index 0000000..346f59b --- /dev/null +++ b/package.json @@ -0,0 +1,39 @@ +{ + "name": "vaughan-labenu-system5", + "version": "1.0.0", + "description": "Você estuda na Labenu_ há tanto tempo que já parecem anos, não é? Então, hoje, vamos pedir para criar um sistema que represente o básico da nossa organização.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "tsc && node ./build/index.js", + "dev": "ts-node-dev ./src/index.ts", + "migrations": "tsnd ./src/data/migrations.ts" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/future4code/Vaughan-labenu-system5.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/future4code/Vaughan-labenu-system5/issues" + }, + "homepage": "https://github.com/future4code/Vaughan-labenu-system5#readme", + "dependencies": { + "@types/knex": "^0.16.1", + "cors": "^2.8.5", + "dotenv": "^16.0.0", + "express": "^4.17.3", + "knex": "^1.0.7", + "mysql": "^2.18.1", + "typescript": "^4.6.3", + "uuid": "^8.3.2" + }, + "devDependencies": { + "@types/cors": "^2.8.12", + "@types/express": "^4.17.13", + "@types/uuid": "^8.3.4", + "ts-node-dev": "^1.1.8" + } +} diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..d28d82d --- /dev/null +++ b/src/app.ts @@ -0,0 +1,16 @@ +import express from 'express' +import cors from 'cors' +import { AddressInfo } from "net"; +export const app = 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.`); + } +}); \ No newline at end of file diff --git a/src/classes/Docente.ts b/src/classes/Docente.ts new file mode 100644 index 0000000..1b3ebb4 --- /dev/null +++ b/src/classes/Docente.ts @@ -0,0 +1,17 @@ +import { Usuarios } from "./Usuarios"; +import { v4 as uuidv4 } from 'uuid'; + +export class Docente extends Usuarios{ + especialidades: string[] + constructor( + nome: string, + email: string, + data_nasc: string, + turma_id: string + ){ + super(nome, email, data_nasc, turma_id) + } + public retornaProfessor(): string{ + return this.nome + } +} \ No newline at end of file diff --git a/src/classes/Estudante.ts b/src/classes/Estudante.ts new file mode 100644 index 0000000..c69e82d --- /dev/null +++ b/src/classes/Estudante.ts @@ -0,0 +1,16 @@ +import { Usuarios } from "./Usuarios"; + +export class Estudante extends Usuarios{ + hobbies: string[] + constructor( + nome: string, + email: string, + data_nasc: string, + turma_id:string + ){ + super(nome, email, data_nasc, turma_id) + } + public retornaData(): string{ + return this.nome + } +} \ No newline at end of file diff --git a/src/classes/Turma.ts b/src/classes/Turma.ts new file mode 100644 index 0000000..cb9758e --- /dev/null +++ b/src/classes/Turma.ts @@ -0,0 +1,17 @@ +import { Request, Response } from 'express'; +import { v4 as uuidv4 } from 'uuid'; + +export class Turma { + id: string + nome: string + modulo: string + constructor(nome:string, modulo:string) { + this.id = uuidv4() + this.nome = nome + this.modulo = modulo + } + public retornaTurma(): string{ + return this.nome + } + +} \ No newline at end of file diff --git a/src/classes/Usuarios.ts b/src/classes/Usuarios.ts new file mode 100644 index 0000000..9242419 --- /dev/null +++ b/src/classes/Usuarios.ts @@ -0,0 +1,21 @@ +import { v4 as uuidv4 } from 'uuid'; + +export class Usuarios { + id: string + nome: string + email: string + data_nasc: string + turma_id: string + constructor( + nome: string, + email: string, + data_nasc: string, + turma_id: string + ){ + this.id = uuidv4() + this.nome = nome + this.email = email + this.data_nasc = data_nasc + this.turma_id = turma_id + } +} \ No newline at end of file diff --git a/src/data/connections.ts b/src/data/connections.ts new file mode 100644 index 0000000..7e9bc15 --- /dev/null +++ b/src/data/connections.ts @@ -0,0 +1,16 @@ +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, + multipleStatements: true + } +}) \ No newline at end of file diff --git a/src/data/docente.json b/src/data/docente.json new file mode 100644 index 0000000..5baee63 --- /dev/null +++ b/src/data/docente.json @@ -0,0 +1,37 @@ +[ + { + "id": "1", + "nome": "Flavio", + "email": "Flavio@gmail.com", + "data_nasc": "1995-03-20", + "turma_id": "1" + }, + { + "id": "2", + "nome": "Iza", + "email": "Iza@gmail.com", + "data_nasc": "1994-06-20", + "turma_id": "2" + }, + { + "id": "3", + "nome": "Paulinha", + "email": "paulinha@gmail.com", + "data_nasc": "1992-08-20", + "turma_id": "3" + }, + { + "id": "4", + "nome": "Roberto", + "email": "Roberto@gmail.com", + "data_nasc": "1999-04-14", + "turma_id": "4" + }, + { + "id": "5", + "nome": "Gabriel", + "email": "Gabriel@gmail.com", + "data_nasc": "1989-11-06", + "turma_id": "4" + } +] \ No newline at end of file diff --git a/src/data/docenteEspecialidade.json b/src/data/docenteEspecialidade.json new file mode 100644 index 0000000..2a1a7b0 --- /dev/null +++ b/src/data/docenteEspecialidade.json @@ -0,0 +1,27 @@ +[ + { + "id": "1", + "docente_id": "1", + "especialidade_id": "1" + }, + { + "id": "2", + "docente_id": "2", + "especialidade_id": "2" + }, + { + "id": "3", + "docente_id": "3", + "especialidade_id": "3" + }, + { + "id": "4", + "docente_id": "4", + "especialidade_id": "4" + }, + { + "id": "5", + "docente_id": "5", + "especialidade_id": "5" + } +] \ No newline at end of file diff --git a/src/data/especialidade.json b/src/data/especialidade.json new file mode 100644 index 0000000..46c52df --- /dev/null +++ b/src/data/especialidade.json @@ -0,0 +1,22 @@ +[ + { + "id": "1", + "nome": "Js" + }, + { + "id": "2", + "nome": "Css" + }, + { + "id": "3", + "nome": "React" + }, + { + "id": "4", + "nome": "Typescript" + }, + { + "id": "5", + "nome": "POO (Programação Orientada a Objetos)" + } +] \ No newline at end of file diff --git a/src/data/estudante.json b/src/data/estudante.json new file mode 100644 index 0000000..232d5e2 --- /dev/null +++ b/src/data/estudante.json @@ -0,0 +1,37 @@ +[ + { + "id": "1", + "nome": "Vitor", + "email": "vitor@gmail.com", + "data_nasc": "2003-06-17", + "turma_id": "1" + }, + { + "id": "2", + "nome": "Ana", + "email": "Ana@gmail.com", + "data_nasc": "1995-03-20", + "turma_id": "2" + }, + { + "id": "3", + "nome": "Diane", + "email": "Diane@gmail.com", + "data_nasc": "1994-04-28", + "turma_id": "3" + }, + { + "id": "4", + "nome": "Gabriela", + "email": "Gabriela@gmail.com", + "data_nasc": "1996-11-04", + "turma_id": "4" + }, + { + "id": "5", + "nome": "Ronald", + "email": "Ronald@gmail.com", + "data_nasc": "1993-04-10", + "turma_id": "4" + } +] \ No newline at end of file diff --git a/src/data/hobbiesEstudante.json b/src/data/hobbiesEstudante.json new file mode 100644 index 0000000..993a7eb --- /dev/null +++ b/src/data/hobbiesEstudante.json @@ -0,0 +1,27 @@ +[ + { + "id": "1", + "estudante_id": "1", + "hobby_id": "1" + }, + { + "id": "2", + "estudante_id": "2", + "hobby_id": "2" + }, + { + "id": "3", + "estudante_id": "3", + "hobby_id": "3" + }, + { + "id": "4", + "estudante_id": "4", + "hobby_id": "4" + }, + { + "id": "5", + "estudante_id": "5", + "hobby_id": "5" + } +] \ No newline at end of file diff --git a/src/data/hobby.json b/src/data/hobby.json new file mode 100644 index 0000000..0ca8468 --- /dev/null +++ b/src/data/hobby.json @@ -0,0 +1,22 @@ +[ + { + "id": "1", + "nome": "assistir séries de tv" + }, + { + "id": "2", + "nome": "jogar videogames" + }, + { + "id": "3", + "nome": "passear de bike" + }, + { + "id": "4", + "nome": "cantar" + }, + { + "id": "5", + "nome": "jogar xadrez" + } +] \ No newline at end of file diff --git a/src/data/migrations.ts b/src/data/migrations.ts new file mode 100644 index 0000000..6cec046 --- /dev/null +++ b/src/data/migrations.ts @@ -0,0 +1,116 @@ +import { connection } from "./connections"; +import turma from "./turma.json" +import estudante from "./estudante.json" +import docente from "./docente.json" +import hobby from "./hobby.json" +import especialidade from "./especialidade.json" +import docenteEspecialidade from "./docenteEspecialidade.json" +import hobbiesEstudante from "./hobbiesEstudante.json" +const printError = (error: any) => { console.log(error.sqlMessage || error.message) } + +const createTable =() => connection.raw(` + CREATE TABLE IF NOT EXISTS turma( + id VARCHAR(255) PRIMARY KEY, + nome VARCHAR(255) UNIQUE NOT NULL, + modulo VARCHAR(255) DEFAULT 0 + ); + + CREATE TABLE IF NOT EXISTS estudante( + id VARCHAR(255) PRIMARY KEY, + nome VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL UNIQUE, + data_nasc DATE NOT NULL, + turma_id VARCHAR(255) NOT NULL, + FOREIGN KEY (turma_id) REFERENCES turma(id) + ); + + CREATE TABLE IF NOT EXISTS docente( + id VARCHAR(255) PRIMARY KEY, + nome VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL UNIQUE, + data_nasc DATE NOT NULL, + turma_id VARCHAR(255) NOT NULL, + FOREIGN KEY (turma_id) REFERENCES turma(id) + ); + + CREATE TABLE IF NOT EXISTS hobby( + id VARCHAR(255) PRIMARY KEY, + nome VARCHAR(255) UNIQUE NOT NULL + ); + + CREATE TABLE IF NOT EXISTS especialidade( + id VARCHAR(255) PRIMARY KEY, + nome VARCHAR(255) UNIQUE NOT NULL + ); + + CREATE TABLE IF NOT EXISTS docente_especialidade( + id VARCHAR(255) PRIMARY KEY, + docente_id VARCHAR(255) NOT NULL, + especialidade_id VARCHAR(255) NOT NULL, + FOREIGN KEY (docente_id) REFERENCES docente(id), + FOREIGN KEY (especialidade_id) REFERENCES especialidade(id) + ); + + CREATE TABLE IF NOT EXISTS estudante_hobby( + id VARCHAR(255) PRIMARY KEY, + estudante_id VARCHAR(255) NOT NULL, + hobby_id VARCHAR(255) NOT NULL, + FOREIGN KEY (estudante_id) REFERENCES docente(id), + FOREIGN KEY (hobby_id) REFERENCES especialidade(id) + ); + +`) +//criando tabelas +.then(() => console.log("Tabelas criadas")) +.catch(printError) + +//inserindo Turma +const insertTurma = () => connection("turma") +.insert(turma) +.then(()=> console.log("Turmas criadas com sucesso")) +.catch(printError) + +//inserindo Estudante +const insertEstudante = () => connection("estudante") +.insert(estudante) +.then(()=> console.log("Estudante criado com sucesso")) +.catch(printError) + +//inserindo docente +const insertDocente = () => connection("docente") +.insert(docente) +.then(()=> console.log("Docente criado com sucesso")) +.catch(printError) + +//inserindo hobby +const insertHobby = () => connection("hobby") +.insert(hobby) +.then(()=> console.log("Hobby criado com sucesso")) +.catch(printError) + +//inserindo especialidade +const insertEspecialidade = () => connection("especialidade") +.insert(especialidade) +.then(()=> console.log("Especialidade criada com sucesso")) +.catch(printError) + +//inserindo especialidade do docente +const insertDocenteEspecialidade = () => connection("docente_especialidade") +.insert(docenteEspecialidade) +.then(()=> console.log("Especialidade do docente criada com sucesso")) +.catch(printError) + +//inserindo hobby do estudante +const insertHobbyEstudante = () => connection("estudante_hobby") +.insert(hobbiesEstudante) +.then(()=> console.log("Hobby do estudante criado com sucesso")) +.catch(printError) + +createTable() +.then(insertTurma) +.then(insertEstudante) +.then(insertDocente) +.then(insertHobby) +.then(insertEspecialidade) +.then(insertDocenteEspecialidade) +.then(insertHobbyEstudante) \ No newline at end of file diff --git a/src/data/turma.json b/src/data/turma.json new file mode 100644 index 0000000..5b86216 --- /dev/null +++ b/src/data/turma.json @@ -0,0 +1,18 @@ +[ + { + "id": "1", + "nome": "Vaughan" + }, + { + "id": "2", + "nome": "Gebru" + }, + { + "id": "3", + "nome": "Alan Coope" + }, + { + "id": "4", + "nome": "zuckerberg" + } +] \ No newline at end of file diff --git a/src/endPoints/buscarDocentes.ts b/src/endPoints/buscarDocentes.ts new file mode 100644 index 0000000..cd64dd7 --- /dev/null +++ b/src/endPoints/buscarDocentes.ts @@ -0,0 +1,11 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const buscarDocentes = async (req: Request, res: Response) => { + try { + const resultado = await connection("docente"); + res.status(200).send(resultado); + } catch (e) { + res.status(500).send({ message: e.sqlMessage || e.message }); + } +}; diff --git a/src/endPoints/buscarEstudantePorNome.ts b/src/endPoints/buscarEstudantePorNome.ts new file mode 100644 index 0000000..360ff0c --- /dev/null +++ b/src/endPoints/buscarEstudantePorNome.ts @@ -0,0 +1,34 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const buscarEstudante = async (req: Request, res: Response) => { + const { nome } = req.query; + let errorCode = 400; + try { + if (!nome) { + errorCode = 422; + throw new Error("Por favor insira um nome"); + } + let resultado = await connection("estudante") + .select("*") + .where("nome", "like", `%${nome}%`); + if (resultado.length < 1) { + errorCode = 422; + throw new Error("Estudante não encontrado"); + } + res.status(200).send(resultado); + } catch (e) { + switch (e.message) { + case "Por favor insira um nome": + res.status(errorCode).send({ message: e.message }); + break; + case "Estudante não encontrado": + res.status(errorCode).send({ message: e.message }); + break; + + default: + res.status(500).send(e.message || e.sqlMessage); + break; + } + } +}; diff --git a/src/endPoints/buscarTurmasAtivas.ts b/src/endPoints/buscarTurmasAtivas.ts new file mode 100644 index 0000000..9e4e802 --- /dev/null +++ b/src/endPoints/buscarTurmasAtivas.ts @@ -0,0 +1,19 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const buscarTurmasAtivas = async (req: Request, res: Response): Promise => { + let errorCode = 400; + try { + let result = await connection("turma") + .select("*") + .where("modulo", "<>", "0"); + + if (result.length < 1) { + errorCode = 422; + throw new Error("Nenhuma turma encontrada"); + } + res.status(200).send(result); + } catch (e) { + res.status(errorCode).send(e.message); + } + } diff --git a/src/endPoints/criarDocente.ts b/src/endPoints/criarDocente.ts new file mode 100644 index 0000000..9616e27 --- /dev/null +++ b/src/endPoints/criarDocente.ts @@ -0,0 +1,52 @@ +import { Request, Response } from 'express'; +import { Docente } from '../Classes/Docente'; +import { connection } from '../data/connections'; + +export const criarDocente = async(req: Request, res: Response) => { + let errorCode = 400; + const {nome, email, data_nasc, turma_id} = req.body + try { + let novoDocente = new Docente(nome, email, data_nasc, turma_id) + if(!nome){ + errorCode = 422; + throw new Error("Preencha seu nome") + } + else if(!email){ + errorCode = 422; + throw new Error("Preencha seu email") + } + else if(!data_nasc){ + errorCode = 422; + throw new Error("Preencha sua data_nasc") + } + else if(!turma_id){ + errorCode = 422; + throw new Error("Preencha sua turma") + } + await connection("docente") + .insert(novoDocente) + + res.status(201).send("Docente criado com sucesso") + } + catch(e: any) { + switch (e.message) { + case "Preencha seu nome": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha seu email": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha sua data_nasc": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha sua turma": + res.status(errorCode).send({ message: e.message }); + break; + + default: + res.status(500).send({message: e.sqlMessage || e.message}); + break; + } + } +} + diff --git a/src/endPoints/criarEstudante.ts b/src/endPoints/criarEstudante.ts new file mode 100644 index 0000000..e743b26 --- /dev/null +++ b/src/endPoints/criarEstudante.ts @@ -0,0 +1,48 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; +import { v4 as uuidv4 } from "uuid"; +import { Estudante } from "../classes/Estudante"; + +export const criarEstudante = async (req: Request, res: Response) => { + let errorCode = 400; + try { + const { nome, email, data_nasc, turma_id } = req.body; + let novoEstudante = new Estudante(nome, email, data_nasc, turma_id) + if (!nome) { + errorCode = 422; + throw new Error("Preencha seu nome"); + } else if (!email) { + errorCode = 422; + throw new Error("Preencha seu email"); + } else if (!data_nasc) { + errorCode = 422; + throw new Error("Preencha sua data_nasc"); + } else if (!turma_id) { + errorCode = 422; + throw new Error("Preencha sua turma"); + } + await connection("estudante").insert(novoEstudante); + + res.status(201).send({message: "Estudante criado com sucesso"}); + + } catch (e:any) { + switch (e.message) { + case "Preencha seu nome": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha seu email": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha sua data_nasc": + res.status(errorCode).send({ message: e.message }); + break; + case "Preencha sua turma": + res.status(errorCode).send({ message: e.message }); + break; + default: + res.status(500).send({message: e.sqlMessage || e.message}); + break; + } + } + } + diff --git a/src/endPoints/criarTurma.ts b/src/endPoints/criarTurma.ts new file mode 100644 index 0000000..bb76749 --- /dev/null +++ b/src/endPoints/criarTurma.ts @@ -0,0 +1,23 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; +import { Turma } from "../classes/Turma"; + +export const criarTurma = async (req: Request, res: Response) => { + let errorCode = 400; + try { + let { nome } = req.body; + let modulo = "0" + let novaTurma = new Turma(nome, modulo) + + if (!nome || nome === undefined) { + errorCode = 422; + throw new Error("Insira o nome da turma"); + } + await connection("turma").insert(novaTurma); + + res.status(201).send({ message: `Turma ${nome} criada com sucesso!` }); + } catch (error) { + res.status(errorCode).send({ message: error.message }); + } + } + diff --git a/src/endPoints/mudarDocenteDeTurma.ts b/src/endPoints/mudarDocenteDeTurma.ts new file mode 100644 index 0000000..61cba10 --- /dev/null +++ b/src/endPoints/mudarDocenteDeTurma.ts @@ -0,0 +1,23 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const mudarTurmaDocente = async (req: Request, res: Response) => { + const { turma_id, docente_id } = req.body; + try { + console.log(turma_id, docente_id); + if (!turma_id || !docente_id) { + throw new Error("Preencha todos os campos"); + } + await connection("docente") + .update({ + turma_id: turma_id + }) + .where({ + id: docente_id + }); + + res.status(200).send({ message: "O docente mudou de turma" }); + } catch (e) { + res.status(500).send({ message: e.sqlMessage || e.message }); + } +}; diff --git a/src/endPoints/mudarEstudanteDeTurma.ts b/src/endPoints/mudarEstudanteDeTurma.ts new file mode 100644 index 0000000..fcd727b --- /dev/null +++ b/src/endPoints/mudarEstudanteDeTurma.ts @@ -0,0 +1,20 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const mudarTurma = async (req: Request, res: Response) => { + const { turma_id, estudante_id } = req.body; + try { + await connection("estudante") + .update({ + turma_id: turma_id + }) + .where("id", estudante_id); + + if (!turma_id || !estudante_id) { + throw new Error("Preencha todos os campos"); + } + res.status(200).send({ message: "O estudante foi remanejado com sucesso" }); + } catch (e) { + res.status(500).send({ message: e.sqlMessage || e.message }); + } +}; diff --git a/src/endPoints/mudarModulo.ts b/src/endPoints/mudarModulo.ts new file mode 100644 index 0000000..6ee942b --- /dev/null +++ b/src/endPoints/mudarModulo.ts @@ -0,0 +1,22 @@ +import { Request, Response } from "express"; +import { connection } from "../data/connections"; + +export const mudarTurmaDeModulo = async (req: Request, res: Response) => { + let errorCode = 400; + try { + const { id, modulo } = req.body; + if (!id && !modulo) { + errorCode = 422; + throw new Error("Preencha todos os campos"); + } + await connection("turma") + .update({ + modulo: modulo + }) + .where("id", "=", id); + + res.status(200).send(`Módulo alterado com sucesso para ${modulo}`); + } catch (e: any) { + res.sendStatus(errorCode).send({ message: e.message }); + } + } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..206e78c --- /dev/null +++ b/src/index.ts @@ -0,0 +1,29 @@ +import { app } from "./app"; +import { buscarDocentes } from "./endPoints/buscarDocentes"; +import { buscarEstudante } from "./endPoints/buscarEstudantePorNome"; +import { buscarTurmasAtivas } from "./endPoints/buscarTurmasAtivas"; +import { criarDocente } from "./endPoints/criarDocente"; +import { criarEstudante } from "./endPoints/criarEstudante"; +import { criarTurma } from "./endPoints/criarTurma"; +import { mudarTurmaDocente } from "./endPoints/mudarDocenteDeTurma"; +import { mudarTurma } from "./endPoints/mudarEstudanteDeTurma"; +import { mudarTurmaDeModulo } from "./endPoints/mudarModulo"; + + +app.post("/turma", criarTurma); + +app.get("/turma", buscarTurmasAtivas); + +app.put("/turma", mudarTurmaDeModulo); + +app.post("/estudante", criarEstudante); + +app.get("/estudante", buscarEstudante); + +app.put("/estudante", mudarTurma); + +app.post("/docente", criarDocente); + +app.get("/docente", buscarDocentes); + +app.put("/docente", mudarTurmaDocente); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..922e922 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", /* Specify ECMAScript target version */ + "module": "commonjs", /* Specify module code generation */ + "sourceMap": true, /* Generates corresponding '.map' file. */ + "outDir": "./build", /* Redirect output structure to the directory. */ + "rootDir": "./src", /* Specify the root directory of input files. */ + "removeComments": true, /* Do not emit comments to output. */ + "noImplicitAny": true , /* Raise error on declarations with an implied 'any' type. */ + "esModuleInterop": true, + "resolveJsonModule": true + } +} \ No newline at end of file