From 35eb22814447fa12ebcca5330db466daac57fe7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Yoshida?= Date: Sun, 10 Dec 2023 15:50:07 -0300 Subject: [PATCH] [ADD]: backend da us07 --- backend/config/config.js | 2 +- .../LearningPathsEnrolmentController.js | 21 ++++++++++++++++++ backend/controllers/UserControllers.js | 6 ++++- backend/index.js | 7 +++--- .../models/schemas/LearningPathsEnrolment.js | 22 +++++++++++++++++++ backend/models/schemas/Users.js | 2 +- backend/package.json | 2 ++ backend/views/routes/LearningPathEnrolment.js | 8 +++++++ 8 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 backend/controllers/LearningPathsEnrolmentController.js create mode 100644 backend/models/schemas/LearningPathsEnrolment.js create mode 100644 backend/views/routes/LearningPathEnrolment.js diff --git a/backend/config/config.js b/backend/config/config.js index 8e6a837e..cc6e3533 100644 --- a/backend/config/config.js +++ b/backend/config/config.js @@ -2,7 +2,7 @@ require("dotenv").config(); const config = { username: process.env.DB_USERNAME || "root", - password: process.env.DB_PASSWORD || null, + password: process.env.DB_PASSWORD || "password", database: process.env.DB_DATABASE || "database", host: process.env.DB_HOST || "127.0.0.1", dialect: "mysql", diff --git a/backend/controllers/LearningPathsEnrolmentController.js b/backend/controllers/LearningPathsEnrolmentController.js new file mode 100644 index 00000000..0cfb5e69 --- /dev/null +++ b/backend/controllers/LearningPathsEnrolmentController.js @@ -0,0 +1,21 @@ +const { LearningPathsEnrolment } = require('../models/schemas'); + +exports.studentEnrolment = async (req, res) => { + const { learning_path_id, student_id } = req.body; + const existingEnrolment = await LearningPathsEnrolment.findOne({where: {learning_path_id: learning_path_id, student_id: student_id} }); + + if (existingEnrolment) { + return res.status(400).json({error: 'O aluno já está matriculado nesta trilha.'}); + } else { + await LearningPathsEnrolment.create({ + learning_path_id: learning_path_id, + student_id: student_id, + }).then(() => { + res.status(201).json("Solicitação bem sucedida") + }).catch((err) => { + if(err){ + res.status(400).json({error: err.message || 'Erro desconhecido durante a matrícula.'}); + }; + }); + }; +} \ No newline at end of file diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index 6a7ca717..d02ff935 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -4,7 +4,7 @@ const { createToken, validateToken } = require('./middlewares/Auth'); exports.userRegister = async (req, res) => { try { - const { email, password } = req.body; + const { superuser, name, registry, school_year, email, password } = req.body; const existingUser = await Users.findOne({ where: { email: email } }); if (existingUser) { @@ -13,6 +13,10 @@ exports.userRegister = async (req, res) => { const hash = await bcrypt.hash(password, 15); await Users.create({ + superuser: superuser, + name: name, + registry: registry, + school_year: school_year, email: email, password: hash, }); diff --git a/backend/index.js b/backend/index.js index e271e046..6a549189 100644 --- a/backend/index.js +++ b/backend/index.js @@ -3,10 +3,10 @@ const cors = require('cors'); const database = require('./models/schemas'); const userRoute = require('./views/routes/Users'); const electiveRoute = require('./views/routes/Electives') -const learningPathRoute = require('./views/routes/LearningPaths') +const learningPathRoute = require('./views/routes/LearningPaths'); +const learningPathsEnrolmentRoute = require('./views/routes/LearningPathEnrolment'); const logoutRoutes = require('./views/routes/Users'); const sequelize = require('sequelize'); -const deleteLearningPathsRoute = require('./views/routes/LearningPaths'); const extractStudentsRoutes = require('./views/routes/Extract'); require("dotenv").config(); @@ -20,8 +20,9 @@ app.use('/elective', electiveRoute); app.use('/learningpath', learningPathRoute); app.use('/api', logoutRoutes); app.use('/send-file', extractStudentsRoutes); +app.use('/learningpathenrolment', learningPathsEnrolmentRoute); -let test = process.env.DB_USERNAME +let test = process.env.DB_USERNAME; database.sequelize.sync().then(() => { diff --git a/backend/models/schemas/LearningPathsEnrolment.js b/backend/models/schemas/LearningPathsEnrolment.js new file mode 100644 index 00000000..52b473ba --- /dev/null +++ b/backend/models/schemas/LearningPathsEnrolment.js @@ -0,0 +1,22 @@ +module.exports = (sequelize, DataTypes) => { + const LearningPathsEnrolment = sequelize.define("LearningPathsEnrolment", { + id: { + type: DataTypes.INTEGER, + field: "id", + primaryKey: true, + autoIncrement: true + }, + learning_path_id: { + type: DataTypes.INTEGER, + field: "learning_path_id", + allowNull: false, + }, + student_id: { + type: DataTypes.INTEGER, + field: "student_id", + allowNull: false, + }, + }) + + return LearningPathsEnrolment; +} \ No newline at end of file diff --git a/backend/models/schemas/Users.js b/backend/models/schemas/Users.js index 4e5ae9dd..fef97ab1 100644 --- a/backend/models/schemas/Users.js +++ b/backend/models/schemas/Users.js @@ -2,7 +2,7 @@ module.exports = (sequelize, DataTypes) => { const Users = sequelize.define("Users", { id: { type: DataTypes.INTEGER, - field: "co_user", + field: "co_users", primaryKey: true, autoIncrement: true }, diff --git a/backend/package.json b/backend/package.json index 46557e0f..b2454ee7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,8 +16,10 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "dotenv": "^16.3.1", + "exceljs": "^4.4.0", "express": "^4.18.2", "jsonwebtoken": "^9.0.2", + "multer": "^1.4.5-lts.1", "mysql2": "^3.6.3", "nodemon": "^3.0.1", "sequelize": "^6.35.0", diff --git a/backend/views/routes/LearningPathEnrolment.js b/backend/views/routes/LearningPathEnrolment.js new file mode 100644 index 00000000..a5a905b5 --- /dev/null +++ b/backend/views/routes/LearningPathEnrolment.js @@ -0,0 +1,8 @@ +const express = require('express'); +const router = express.Router(); +const learningPathsEnrolmentController = require('../../controllers/LearningPathsEnrolmentController'); + + +router.post('/studentenrolment', learningPathsEnrolmentController.studentEnrolment); + +module.exports = router; \ No newline at end of file