Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint de criar turmas feito #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions src/data/Class/insert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import connection from "../../connection";
import { Student } from "d:/1.Cursos/0.Programação/2.Origamid/1. ORIGAMID - Web Design Completo/bikcraft-web/types/student";


export const insertStudent = async (student: Student): Promise<void> => {
try {
await connection
.insert({
id: Date.now(), //ou nada e fazer/mudar tab com id INT PRIMARY KEY AUTO_INCREMENT, que vai incrementando no sql de 2 em 2 int automaticamente.
name: student.name,
email: student.email,
birthdate: student.birthdate,
})
.into("Student");
} catch (error) {
throw new Error(error.message || error.sqlMessage);
}
};
59 changes: 59 additions & 0 deletions src/endpoints/Class/createClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Request, Response } from "express";
import connection from "../../connection";
import { checkEmail } from "../../functions/checkEmail";
import { checkDate, formatDate } from "../../functions/validDate";

enum TIPO_TURMA {
INTEGRAL = "integral",
NOTURNO = "noturno"
}

export type Class = {
id: number;
nome: string;
data_inicio: string;
data_fim: string;
modulo: number,
tipo: TIPO_TURMA
};

const createClass = async (req: Request, res: Response) => {
let errorCode: number = 400;
try {
const input: Class = {
id: req.body.id,
nome: req.body.nome,
data_inicio: req.body.data_inicio,
data_fim: req.body.data_fim,
modulo: req.body.modulo,
tipo: req.body.tipo
}
if(!input.id || !input.nome || !input.data_inicio || !input.data_fim || !input.modulo || !input.tipo){
errorCode = 422;
throw new Error("preencha os campos corretamente")
}
if(input.tipo !== TIPO_TURMA.INTEGRAL && input.tipo !== TIPO_TURMA.NOTURNO){
errorCode = 422;
throw new Error('os valores possíveis são "integral" e "noturno"')
}
if(input.tipo === TIPO_TURMA.NOTURNO){
input.nome = input.nome+='-na-night'
}
await connection.raw(`
INSERT INTO TURMA (id, nome, data_inicio, data_fim, modulo)
VALUES(
${input.id},
'${input.nome}',
'${input.data_inicio}',
'${input.data_fim}',
${input.modulo},
)
`)
res.status(201).send({message:"Turma criada com sucesso"})

} catch (error) {
res.status(errorCode).send({ message: error.message || error.sqlMessage });
}
}

export default createClass;
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import app from "./app";
import createClass from "./endpoints/Class/createClass";
import createStudent from "./endpoints/Student/createStudent";
import createTeacher from "./endpoints/Teacher/createTeacher";

Expand All @@ -7,4 +8,7 @@ import createTeacher from "./endpoints/Teacher/createTeacher";
app.post("/student/create", createStudent);

// Cadastrar professor
app.post("/teacher/create", createTeacher);
app.post("/teacher/create", createTeacher);

// Cadastrar turma
app.post("/class/create", createClass);
13 changes: 13 additions & 0 deletions src/types/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum TIPO_TURMA {
INTEGRAL = "integral",
NOTURNO = "noturno"
}

export type Class = {
id: number;
nome: string;
data_inicio: string;
data_fim: string;
modulo: number,
tipo: TIPO_TURMA
};