Skip to content

Commit

Permalink
Merge pull request #44 from UnBArqDsw2021-1/feat/31_details_activitie…
Browse files Browse the repository at this point in the history
…s_children

Feat/31 details activities children
  • Loading branch information
eliseukadesh67 authored Oct 15, 2021
2 parents de253f3 + 7b6a063 commit 91b441e
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 27 deletions.
28 changes: 27 additions & 1 deletion src/app/controllers/AdmController.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,36 @@ class AdmController extends UserController {
}
}

async registerTeacherClass(req, res) {
try {
const { teacher_id, class_id } = req.body;

const { usertype } = await User.findByPk(teacher_id);
if (usertype !== 1) {
return res.status(403).json({ message: 'O usuário não é um professor.' });
}

const TeacherClass = await ClassProfessional.findOne({
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 });

return res.status(200).json({ msg: 'Professor adicionado à turma.' });
} catch (err) {
return res.status(500).json({ error: err.message });
}
}

async deleteTeacherClass(req, res) {
try {
const { teacher_id, class_id } = req.query;
console.log(req);

const TeacherClass = await ClassProfessional.findOne({
where: {
Expand Down
10 changes: 5 additions & 5 deletions src/app/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class AuthController {
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);
}
Expand All @@ -63,7 +63,7 @@ class AuthController {
expiresIn: authConfig.expiresIn,
}),

extra_info: list
extra_info: list,

});
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/controllers/ChildController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/controllers/GuardianController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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,
},
});

await t.commit();

for (const element of guardian_children ) {
for (const element of guardian_children) {
await element.update({
fk_idGuardian: id,
});
Expand Down
47 changes: 45 additions & 2 deletions src/app/controllers/TeacherController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
Expand All @@ -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();
1 change: 0 additions & 1 deletion src/app/middlewares/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions src/app/models/Anotation.js
Original file line number Diff line number Diff line change
@@ -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' });
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/Professionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down
8 changes: 3 additions & 5 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,8 +12,6 @@ module.exports = {
define: {
timestamp: true,
underscored: true,
underscoredAll: true
}
underscoredAll: true,
},
};


2 changes: 1 addition & 1 deletion src/database/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ routes.get('/adm/list-professionals', TeacherController.list);
routes.get('/adm/list-guardians', GuardianController.list);
routes.get('/adm/list-classes', AdmController.listClasses);
routes.get('/adm/list-anotations', AnotationController.listAll);
routes.post('/adm/register-class-teacher', AdmController.registerTeacherClass);
routes.delete('/adm/delete-class-teacher', AdmController.deleteTeacherClass);
// routes.get('/adm/list-activities', ActivityController.list);

Expand All @@ -47,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);
Expand Down
2 changes: 0 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
import app from './app';

app.listen(process.env.PORT || 3333);


0 comments on commit 91b441e

Please sign in to comment.