1
0
Fork 0

Socket getter for database

This commit is contained in:
Ethanell 2020-11-24 08:47:13 +01:00
parent 84c932d3f9
commit 9de9cf7b37
5 changed files with 94 additions and 1 deletions

39
sockets/get/agendaGet.js Normal file
View file

@ -0,0 +1,39 @@
const models = require("../../models");
module.exports = socket => {
return async (data) => {
let startDate = data.startDate ? data.startDate : new Date(), endDate = data.endDate ? data.endDate : new Date();
let options = {where: {startDate: startDate, endDate: endDate}, include: []};
if (data.semesters) {
for (let semester of data.semesters) {
let s = await models.Semester.findByPk(semester);
if (!s) {
socket.emit("agendaGet", {error: {message: "semester_not_found"}});
return
} else
options.include.push({model: models.Semester, where: {id: s.id}, require: true});
}
}
if (data.groups) {
for (let group of data.groups) {
let g = await models.Group.findByPk(group);
if (!g) {
socket.emit("agendaGet", {error: {message: "group_not_found"}});
return
} else
options.include.push({model: models.Group, where: {id: g.id}, require: true});
}
}
if (data.teachers) {
for (let teacher of data.teachers) {
let t = await models.User.findByPk(teacher);
if (!t) {
socket.emit("agendaGet", {error: {message: "teacher_not_found"}});
return
} else
options.include.push({model: models.User, where: {id: t.email}, require: true});
}
}
socket.emit("agendaGet", await models.Event.findAll(options));
}
};

21
sockets/get/groupGet.js Normal file
View file

@ -0,0 +1,21 @@
const models = require("../../models");
module.exports = socket => {
return async (data) => {
let options = {where: {}, include: [{model: models.Semester, require: true}]};
if (data.number)
options.where.number = data.number;
if (data.semester) {
let s = await models.Semester.findByPk(data.semester);
if (!s) {
socket.emit("groupGet", {error: {message: "semester_not_found"}});
return
}
options.include[0].where = {id: s.id};
}
if (data.users)
options.include.push({model: models.User, require: true});
socket.emit("groupGet", await models.Group.findAll(options));
}
};

View file

@ -0,0 +1,15 @@
const models = require("../../models");
module.exports = socket => {
return async (data) => {
let options = {where: {}, include: []};
if (data.name)
options.where.name = data.name;
if (data.year)
options.where.year = data.year;
if (data.groups)
options.include.push({model: models.Group, require: true});
socket.emit("semesterGet", await models.Semester.findAll(options));
}
};

17
sockets/get/userGet.js Normal file
View file

@ -0,0 +1,17 @@
const models = require("../../models");
module.exports = socket => {
return async (data) => {
let options = {where: {}};
if (data.email)
options.where.email = data.email;
if (data.firstName && data.lastName) {
options.where.firstName = data.firstName;
options.where.lastName = data.lastName;
}
if (data.permissions)
options.where.permissions = data.permissions;
socket.emit("userGet", await models.User.findAll(options));
}
};

View file

@ -10,6 +10,7 @@ module.exports = socket => {
} else {
socket.on("profileEdit", require("./profile/edit")(socket));
socket.on("logout", require("./logout")(socket));
socket.on("agendaGet", require("./get/agendaGet")(socket));
}
socket.emit("connected");
}
};