Skip to content

Commit 91b441e

Browse files
Merge pull request #44 from UnBArqDsw2021-1/feat/31_details_activities_children
Feat/31 details activities children
2 parents de253f3 + 7b6a063 commit 91b441e

File tree

12 files changed

+95
-27
lines changed

12 files changed

+95
-27
lines changed

src/app/controllers/AdmController.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,36 @@ class AdmController extends UserController {
229229
}
230230
}
231231

232+
async registerTeacherClass(req, res) {
233+
try {
234+
const { teacher_id, class_id } = req.body;
235+
236+
const { usertype } = await User.findByPk(teacher_id);
237+
if (usertype !== 1) {
238+
return res.status(403).json({ message: 'O usuário não é um professor.' });
239+
}
240+
241+
const TeacherClass = await ClassProfessional.findOne({
242+
where: {
243+
fk_idClass: class_id,
244+
fk_idProfessional: teacher_id,
245+
},
246+
});
247+
if (TeacherClass !== null) {
248+
return res.status(201).json({ message: 'Professional já está cadastrado na turma.' });
249+
}
250+
251+
await ClassProfessional.create({ fk_idClass: class_id, fk_idProfessional: teacher_id });
252+
253+
return res.status(200).json({ msg: 'Professor adicionado à turma.' });
254+
} catch (err) {
255+
return res.status(500).json({ error: err.message });
256+
}
257+
}
258+
232259
async deleteTeacherClass(req, res) {
233260
try {
234261
const { teacher_id, class_id } = req.query;
235-
console.log(req);
236262

237263
const TeacherClass = await ClassProfessional.findOne({
238264
where: {

src/app/controllers/AuthController.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class AuthController {
3838
if (usertype === 0) {
3939
const relations = await GuardianChild.findAll({
4040
where: {
41-
fk_idGuardian: id
42-
}
41+
fk_idGuardian: id,
42+
},
4343
});
44-
45-
for (const relation of relations){
44+
45+
for (const relation of relations) {
4646
const child = await Child.findByPk(relation.dataValues.fk_idChild);
4747
list.push(child.dataValues);
4848
}
@@ -63,7 +63,7 @@ class AuthController {
6363
expiresIn: authConfig.expiresIn,
6464
}),
6565

66-
extra_info: list
66+
extra_info: list,
6767

6868
});
6969
} catch (err) {

src/app/controllers/ChildController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ChildController {
1515
async listChildrenRelations(req, res) {
1616
try {
1717
const children = await Child.findAll();
18-
const guardian_children = await GuardianChild.findAll();
19-
return res.json({ children, relations: guardian_children });
18+
const guardian_children = await GuardianChild.findAll();
19+
return res.json({ children, relations: guardian_children });
2020
} catch (err) {
2121
return res.status(500).json({ error: err.stack });
2222
}

src/app/controllers/GuardianController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class GuardianController extends UserController {
3838

3939
await Guardian.create({ id, adress }, { transaction: t });
4040

41-
const guardian_children = await GuardianChild.findAll({
41+
const guardian_children = await GuardianChild.findAll({
4242
where: {
4343
guardian_cpf: cpf,
4444
},
4545
});
4646

4747
await t.commit();
4848

49-
for (const element of guardian_children ) {
49+
for (const element of guardian_children) {
5050
await element.update({
5151
fk_idGuardian: id,
5252
});

src/app/controllers/TeacherController.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import dbConfig from '../../config/database';
66
import Project from '../models/Project';
77
import Class from '../models/Class';
88
import ClassProfessional from '../models/ClassProfessional';
9+
import ClassProject from '../models/ClassProject';
10+
import Child from '../models/Child';
911

1012
const sequelize = new Sequelize(dbConfig.database, dbConfig.username,
1113
dbConfig.password, { host: dbConfig.host, dialect: dbConfig.dialect });
@@ -56,8 +58,6 @@ class TeacherController extends UserController {
5658
},
5759
});
5860

59-
console.log(relations);
60-
6161
for (const relation of relations) {
6262
const class_obj = await Class.findByPk(relation.dataValues.fk_idClass);
6363
list.push(class_obj.dataValues);
@@ -68,6 +68,49 @@ class TeacherController extends UserController {
6868
return res.status(500).json({ error: err.stack });
6969
}
7070
}
71+
72+
async getClassInfo(req, res) {
73+
try {
74+
const { class_id } = req.params;
75+
76+
const relation = await ClassProfessional.findOne({
77+
where: {
78+
fk_idClass: class_id,
79+
fk_idProfessional: req.userId,
80+
},
81+
});
82+
if (relation === null) {
83+
return res.status(403).json({ message: 'Esta turma não é sua ou não existe.' });
84+
}
85+
86+
const details = await Class.findByPk(class_id);
87+
88+
const activities = [];
89+
const activities_rels = await ClassProject.findAll({
90+
where: {
91+
fk_idClass: class_id,
92+
},
93+
});
94+
for (const relation of activities_rels) {
95+
const project = await Project.findByPk(relation.dataValues.fk_idProject);
96+
activities.push(project.dataValues);
97+
}
98+
99+
const children = await Child.findAll({
100+
where: {
101+
fk_idClass: class_id,
102+
},
103+
});
104+
105+
return res.json({
106+
details,
107+
activities,
108+
children,
109+
});
110+
} catch (err) {
111+
return res.status(500).json({ error: err.stack });
112+
}
113+
}
71114
}
72115

73116
export default new TeacherController();

src/app/middlewares/middleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class Middleware {
5959
const decoded = await promisify(jwt.verify)(token, authConfig.secret);
6060

6161
if (decoded.usertype !== 0) {
62-
console.log(decoded.usertype);
6362
return res.status(401).json({ error: 'Acesso negado.' });
6463
}
6564
req.userId = decoded.id;

src/app/models/Anotation.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { DataTypes, Model } from "sequelize";
2-
class Anotation extends Model {
3-
static init(sequelize){
1+
import { DataTypes, Model } from 'sequelize';
2+
3+
class Anotation extends Model {
4+
static init(sequelize) {
45
super.init({
56
fk_idChild: DataTypes.INTEGER,
67
title: DataTypes.STRING,
78
description: DataTypes.STRING,
8-
},{
9+
}, {
910
sequelize,
10-
})
11+
});
1112
}
13+
1214
static associate(models) {
1315
this.belongsTo(models.Child, { foreignKey: 'fk_idChild' });
1416
this.belongsTo(models.Professionals, { foreignKey: 'fk_idProfessional' });

src/app/models/Professionals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Professionals extends Model {
1717

1818
static associate(models) {
1919
this.hasMany(models.Project, { foreignKey: 'fk_idProfessional', as: 'Projects' });
20-
this.hasMany(models.Anotation, { foreignKey: 'fk_idProfessional'});
20+
this.hasMany(models.Anotation, { foreignKey: 'fk_idProfessional' });
2121
this.belongsToMany(models.Class, { as: 'Class', through: 'ClassProfessional', foreignKey: 'fk_idProfessional' });
2222
this.belongsTo(models.Ec, { foreignKey: 'fk_idEc', as: 'ec' });
2323
}

src/config/database.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('dotenv').config();
22

33
module.exports = {
4-
dialect:'postgres',
4+
dialect: 'postgres',
55
host: process.env.POSTGRES_HOST,
66
username: process.env.POSTGRES_USER,
77
password: process.env.POSTGRES_PASSWORD,
@@ -12,8 +12,6 @@ module.exports = {
1212
define: {
1313
timestamp: true,
1414
underscored: true,
15-
underscoredAll: true
16-
}
15+
underscoredAll: true,
16+
},
1717
};
18-
19-

src/database/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import dbConfig from '../config/database';
21
import Sequelize from 'sequelize';
2+
import dbConfig from '../config/database';
33
import User from '../app/models/User';
44
import Professionals from '../app/models/Professionals';
55
import Guardian from '../app/models/Guardian';

0 commit comments

Comments
 (0)