From 0a973d3cb85c7b040c64191f5a83fc8764765769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Oliveira=20Patr=C3=ADcio?= Date: Fri, 15 Oct 2021 08:53:12 -0300 Subject: [PATCH 1/4] =?UTF-8?q?feat(#31)=20Cria=20rota=20para=20ver=20info?= =?UTF-8?q?rma=C3=A7=C3=B5es=20da=20turma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routes.js b/src/routes.js index ca92d1e..4e2eb6e 100644 --- a/src/routes.js +++ b/src/routes.js @@ -48,6 +48,7 @@ routes.put('/teacher/update-anotation', AnotationController.update); routes.delete('/teacher/delete-anotation/:id', AnotationController.delete); routes.get('/teacher/list-anotations', AnotationController.listMine); routes.get('/teacher/list-my-classes', TeacherController.listMyClasses); +routes.get('/teacher/get-class-info/:class_id', TeacherController.getClassInfo) // Guardian routes routes.use('/guardian', Middleware.verifyGuardian); From e5a5d7e3f7099ac3eef1284d032de3d128e6da41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Oliveira=20Patr=C3=ADcio?= Date: Fri, 15 Oct 2021 08:53:33 -0300 Subject: [PATCH 2/4] =?UTF-8?q?feat(#31)Cria=20fun=C3=A7=C3=A3o=20para=20v?= =?UTF-8?q?er=20informa=C3=A7=C3=B5es=20da=20turma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/controllers/TeacherController.js | 47 +++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/app/controllers/TeacherController.js b/src/app/controllers/TeacherController.js index 14ba82e..72ca122 100644 --- a/src/app/controllers/TeacherController.js +++ b/src/app/controllers/TeacherController.js @@ -6,6 +6,8 @@ import dbConfig from '../../config/database'; import Project from '../models/Project'; import Class from '../models/Class'; import ClassProfessional from '../models/ClassProfessional'; +import ClassProject from '../models/ClassProject'; +import Child from '../models/Child'; const sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, { host: dbConfig.host, dialect: dbConfig.dialect }); @@ -56,8 +58,6 @@ class TeacherController extends UserController { } }); - console.log(relations); - for (const relation of relations){ const class_obj = await Class.findByPk(relation.dataValues.fk_idClass); list.push(class_obj.dataValues); @@ -68,6 +68,49 @@ class TeacherController extends UserController { return res.status(500).json({ error: err.stack }); } } + + async getClassInfo(req, res){ + try{ + const { class_id } = req.params; + + const relation = await ClassProfessional.findOne({ + where: { + fk_idClass: class_id, + fk_idProfessional: req.userId, + }, + }); + if (relation === null) { + return res.status(403).json({ message: 'Esta turma não é sua ou não existe.' }); + } + + const details = await Class.findByPk(class_id); + + const activities = [] + const activities_rels = await ClassProject.findAll({ + where: { + fk_idClass: class_id, + } + }); + for (const relation of activities_rels){ + const project = await Project.findByPk(relation.dataValues.fk_idProject); + activities.push(project.dataValues); + } + + const children = await Child.findAll({ + where: { + fk_idClass: class_id, + } + }); + + return res.json({ + details, + activities, + children, + }); + } catch (err) { + return res.status(500).json({ error: err.stack }); + } + } } export default new TeacherController(); From a8c94ba76530209cbc671c98e2b21ba24220582b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Oliveira=20Patr=C3=ADcio?= Date: Fri, 15 Oct 2021 08:53:48 -0300 Subject: [PATCH 3/4] Lint --- src/app/controllers/AdmController.js | 19 +++++++++--------- src/app/controllers/AuthController.js | 13 ++++++------ src/app/controllers/ChildController.js | 4 ++-- src/app/controllers/GuardianController.js | 4 ++-- src/app/controllers/TeacherController.js | 24 +++++++++++------------ src/app/middlewares/middleware.js | 1 - src/app/models/Anotation.js | 12 +++++++----- src/app/models/Professionals.js | 2 +- src/config/database.js | 8 +++----- src/database/index.js | 2 +- src/routes.js | 2 +- src/server.js | 2 -- 12 files changed, 44 insertions(+), 49 deletions(-) diff --git a/src/app/controllers/AdmController.js b/src/app/controllers/AdmController.js index e6c58ba..2c2a81e 100644 --- a/src/app/controllers/AdmController.js +++ b/src/app/controllers/AdmController.js @@ -227,11 +227,11 @@ class AdmController extends UserController { } async registerTeacherClass(req, res) { - try{ + try { const { teacher_id, class_id } = req.body; const { usertype } = await User.findByPk(teacher_id); - if (usertype !== 1){ + if (usertype !== 1) { return res.status(403).json({ message: 'O usuário não é um professor.' }); } @@ -239,30 +239,29 @@ class AdmController extends UserController { where: { fk_idClass: class_id, fk_idProfessional: teacher_id, - } + }, }); if (TeacherClass !== null) { return res.status(201).json({ message: 'Professional já está cadastrado na turma.' }); } - await ClassProfessional.create({ fk_idClass: class_id, fk_idProfessional: teacher_id, }); + await ClassProfessional.create({ fk_idClass: class_id, fk_idProfessional: teacher_id }); return res.status(200).json({ msg: 'Professor adicionado à turma.' }); - }catch(err) { + } catch (err) { return res.status(500).json({ error: err.message }); } } async deleteTeacherClass(req, res) { - try{ + try { const { teacher_id, class_id } = req.query; - console.log(req); const TeacherClass = await ClassProfessional.findOne({ where: { fk_idClass: class_id, fk_idProfessional: teacher_id, - } + }, }); if (TeacherClass === null) { return res.status(404).json({ message: 'Relação não encontrada.' }); @@ -271,11 +270,11 @@ class AdmController extends UserController { await TeacherClass.destroy(); return res.status(200).json({ msg: 'Professor removido da turma.' }); - }catch(err) { + } catch (err) { return res.status(500).json({ error: err.message }); } } - + async updateClass(req, res) { try { const { id, updates } = req.body; diff --git a/src/app/controllers/AuthController.js b/src/app/controllers/AuthController.js index ab435d6..3faeba4 100644 --- a/src/app/controllers/AuthController.js +++ b/src/app/controllers/AuthController.js @@ -33,16 +33,16 @@ class AuthController { } const { id, name, usertype } = user; - var list = []; + let list = []; if (usertype === 0) { const relations = await GuardianChild.findAll({ where: { - fk_idGuardian: id - } + fk_idGuardian: id, + }, }); - - for (const relation of relations){ + + for (const relation of relations) { const child = await Child.findByPk(relation.dataValues.fk_idChild); list.push(child.dataValues); } @@ -50,7 +50,6 @@ class AuthController { list = undefined; } - return res.json({ user: { id, @@ -63,7 +62,7 @@ class AuthController { expiresIn: authConfig.expiresIn, }), - extra_info: list + extra_info: list, }); } catch (err) { diff --git a/src/app/controllers/ChildController.js b/src/app/controllers/ChildController.js index 6e0cc48..8a3c42d 100644 --- a/src/app/controllers/ChildController.js +++ b/src/app/controllers/ChildController.js @@ -15,8 +15,8 @@ class ChildController { async listChildrenRelations(req, res) { try { const children = await Child.findAll(); - const guardian_children = await GuardianChild.findAll(); - return res.json({ children, relations: guardian_children }); + const guardian_children = await GuardianChild.findAll(); + return res.json({ children, relations: guardian_children }); } catch (err) { return res.status(500).json({ error: err.stack }); } diff --git a/src/app/controllers/GuardianController.js b/src/app/controllers/GuardianController.js index 75d9900..b2678b0 100644 --- a/src/app/controllers/GuardianController.js +++ b/src/app/controllers/GuardianController.js @@ -38,7 +38,7 @@ class GuardianController extends UserController { await Guardian.create({ id, adress }, { transaction: t }); - const guardian_children = await GuardianChild.findAll({ + const guardian_children = await GuardianChild.findAll({ where: { guardian_cpf: cpf, }, @@ -46,7 +46,7 @@ class GuardianController extends UserController { await t.commit(); - for (const element of guardian_children ) { + for (const element of guardian_children) { await element.update({ fk_idGuardian: id, }); diff --git a/src/app/controllers/TeacherController.js b/src/app/controllers/TeacherController.js index 72ca122..09ff1a3 100644 --- a/src/app/controllers/TeacherController.js +++ b/src/app/controllers/TeacherController.js @@ -54,25 +54,25 @@ class TeacherController extends UserController { const relations = await ClassProfessional.findAll({ where: { - fk_idProfessional: req.userId - } + fk_idProfessional: req.userId, + }, }); - for (const relation of relations){ + for (const relation of relations) { const class_obj = await Class.findByPk(relation.dataValues.fk_idClass); list.push(class_obj.dataValues); } - return res.json({list}); + return res.json({ list }); } catch (err) { return res.status(500).json({ error: err.stack }); } } - async getClassInfo(req, res){ - try{ + async getClassInfo(req, res) { + try { const { class_id } = req.params; - + const relation = await ClassProfessional.findOne({ where: { fk_idClass: class_id, @@ -85,13 +85,13 @@ class TeacherController extends UserController { const details = await Class.findByPk(class_id); - const activities = [] + const activities = []; const activities_rels = await ClassProject.findAll({ where: { fk_idClass: class_id, - } + }, }); - for (const relation of activities_rels){ + for (const relation of activities_rels) { const project = await Project.findByPk(relation.dataValues.fk_idProject); activities.push(project.dataValues); } @@ -99,9 +99,9 @@ class TeacherController extends UserController { const children = await Child.findAll({ where: { fk_idClass: class_id, - } + }, }); - + return res.json({ details, activities, diff --git a/src/app/middlewares/middleware.js b/src/app/middlewares/middleware.js index f8c5e22..87b6b9f 100644 --- a/src/app/middlewares/middleware.js +++ b/src/app/middlewares/middleware.js @@ -59,7 +59,6 @@ class Middleware { const decoded = await promisify(jwt.verify)(token, authConfig.secret); if (decoded.usertype !== 0) { - console.log(decoded.usertype); return res.status(401).json({ error: 'Acesso negado.' }); } req.userId = decoded.id; diff --git a/src/app/models/Anotation.js b/src/app/models/Anotation.js index 0a6ba73..64a2444 100644 --- a/src/app/models/Anotation.js +++ b/src/app/models/Anotation.js @@ -1,14 +1,16 @@ -import { DataTypes, Model } from "sequelize"; -class Anotation extends Model { - static init(sequelize){ +import { DataTypes, Model } from 'sequelize'; + +class Anotation extends Model { + static init(sequelize) { super.init({ fk_idChild: DataTypes.INTEGER, title: DataTypes.STRING, description: DataTypes.STRING, - },{ + }, { sequelize, - }) + }); } + static associate(models) { this.belongsTo(models.Child, { foreignKey: 'fk_idChild' }); this.belongsTo(models.Professionals, { foreignKey: 'fk_idProfessional' }); diff --git a/src/app/models/Professionals.js b/src/app/models/Professionals.js index 4f52b07..5dbee87 100644 --- a/src/app/models/Professionals.js +++ b/src/app/models/Professionals.js @@ -17,7 +17,7 @@ class Professionals extends Model { static associate(models) { this.hasMany(models.Project, { foreignKey: 'fk_idProfessional', as: 'Projects' }); - this.hasMany(models.Anotation, { foreignKey: 'fk_idProfessional'}); + this.hasMany(models.Anotation, { foreignKey: 'fk_idProfessional' }); this.belongsToMany(models.Class, { as: 'Class', through: 'ClassProfessional', foreignKey: 'fk_idProfessional' }); this.belongsTo(models.Ec, { foreignKey: 'fk_idEc', as: 'ec' }); } diff --git a/src/config/database.js b/src/config/database.js index 8ed9b5f..ab663eb 100644 --- a/src/config/database.js +++ b/src/config/database.js @@ -1,7 +1,7 @@ require('dotenv').config(); module.exports = { - dialect:'postgres', + dialect: 'postgres', host: process.env.POSTGRES_HOST, username: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, @@ -12,8 +12,6 @@ module.exports = { define: { timestamp: true, underscored: true, - underscoredAll: true - } + underscoredAll: true, + }, }; - - diff --git a/src/database/index.js b/src/database/index.js index 4265c83..1f150a5 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -1,5 +1,5 @@ -import dbConfig from '../config/database'; import Sequelize from 'sequelize'; +import dbConfig from '../config/database'; import User from '../app/models/User'; import Professionals from '../app/models/Professionals'; import Guardian from '../app/models/Guardian'; diff --git a/src/routes.js b/src/routes.js index 4e2eb6e..7307d38 100644 --- a/src/routes.js +++ b/src/routes.js @@ -48,7 +48,7 @@ routes.put('/teacher/update-anotation', AnotationController.update); routes.delete('/teacher/delete-anotation/:id', AnotationController.delete); routes.get('/teacher/list-anotations', AnotationController.listMine); routes.get('/teacher/list-my-classes', TeacherController.listMyClasses); -routes.get('/teacher/get-class-info/:class_id', TeacherController.getClassInfo) +routes.get('/teacher/get-class-info/:class_id', TeacherController.getClassInfo); // Guardian routes routes.use('/guardian', Middleware.verifyGuardian); diff --git a/src/server.js b/src/server.js index d76595f..7e0f053 100644 --- a/src/server.js +++ b/src/server.js @@ -2,5 +2,3 @@ import app from './app'; app.listen(process.env.PORT || 3333); - - From 9a4aae7da924c93705c98ed7f72c921a7960a97e Mon Sep 17 00:00:00 2001 From: eliseukadesh67 Date: Fri, 15 Oct 2021 12:39:24 -0300 Subject: [PATCH 4/4] Remove console.log --- src/app/controllers/TeacherController.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/app/controllers/TeacherController.js b/src/app/controllers/TeacherController.js index 1c34629..804fd95 100644 --- a/src/app/controllers/TeacherController.js +++ b/src/app/controllers/TeacherController.js @@ -58,8 +58,6 @@ class TeacherController extends UserController { }, }); - console.log(relations); - for (const relation of relations) { const class_obj = await Class.findByPk(relation.dataValues.fk_idClass); list.push(class_obj.dataValues);