Skip to content

Commit

Permalink
[ADD]: backend da us07
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaryoshida committed Dec 10, 2023
1 parent bc0164d commit 35eb228
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions backend/controllers/LearningPathsEnrolmentController.js
Original file line number Diff line number Diff line change
@@ -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.'});
};
});
};
}
6 changes: 5 additions & 1 deletion backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
});
Expand Down
7 changes: 4 additions & 3 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(() => {
Expand Down
22 changes: 22 additions & 0 deletions backend/models/schemas/LearningPathsEnrolment.js
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion backend/models/schemas/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions backend/views/routes/LearningPathEnrolment.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 35eb228

Please sign in to comment.