mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
init admin backend
This commit is contained in:
parent
16ec234ed8
commit
967a2ac6d7
4 changed files with 177 additions and 3 deletions
|
|
@ -20,6 +20,8 @@ const users = require('./models/users.js');
|
|||
const userModel = new users(db, foldersModel);
|
||||
const images = require('./models/images.js');
|
||||
const imageModel = new images(db);
|
||||
const Admin = require('./models/admin.js');
|
||||
const adminModel = new Admin(db);
|
||||
|
||||
// instantiate the controllers
|
||||
const usersController = require('./controllers/users.js');
|
||||
|
|
@ -32,6 +34,8 @@ const quizController = require('./controllers/quiz.js');
|
|||
const quizControllerInstance = new quizController(quizModel, foldersModel);
|
||||
const imagesController = require('./controllers/images.js');
|
||||
const imagesControllerInstance = new imagesController(imageModel);
|
||||
const AdminController = require('./controllers/admin.js');
|
||||
const AdminControllerInstance = new AdminController(adminModel);
|
||||
|
||||
// export the controllers
|
||||
module.exports.users = usersControllerInstance;
|
||||
|
|
@ -39,6 +43,7 @@ module.exports.rooms = roomsControllerInstance;
|
|||
module.exports.folders = foldersControllerInstance;
|
||||
module.exports.quizzes = quizControllerInstance;
|
||||
module.exports.images = imagesControllerInstance;
|
||||
module.exports.admin = AdminControllerInstance;
|
||||
|
||||
//import routers (instantiate controllers as side effect)
|
||||
const userRouter = require('./routers/users.js');
|
||||
|
|
@ -48,6 +53,7 @@ const quizRouter = require('./routers/quiz.js');
|
|||
const imagesRouter = require('./routers/images.js')
|
||||
const AuthManager = require('./auth/auth-manager.js')
|
||||
const authRouter = require('./routers/auth.js')
|
||||
const adminRouter = require('./routers/admin.js')
|
||||
|
||||
// Setup environment
|
||||
dotenv.config();
|
||||
|
|
@ -100,6 +106,7 @@ app.use('/api/folder', folderRouter);
|
|||
app.use('/api/quiz', quizRouter);
|
||||
app.use('/api/image', imagesRouter);
|
||||
app.use('/api/auth', authRouter);
|
||||
app.use('/api/admin', adminRouter);
|
||||
|
||||
// Add Auths methods
|
||||
const session = require('express-session');
|
||||
|
|
@ -113,11 +120,9 @@ app.use(session({
|
|||
let _authManager = new AuthManager(app,null,userModel);
|
||||
app.use(errorHandler);
|
||||
|
||||
// Start server
|
||||
async function start() {
|
||||
const port = process.env.PORT || 4400;
|
||||
|
||||
// Check DB connection
|
||||
await db.connect();
|
||||
db.getConnection();
|
||||
console.log(`Connexion MongoDB établie`);
|
||||
|
|
@ -127,7 +132,6 @@ async function start() {
|
|||
});
|
||||
}
|
||||
|
||||
// Graceful shutdown on SIGINT (Ctrl+C)
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down...');
|
||||
await db.closeConnection();
|
||||
|
|
|
|||
44
server/controllers/admin.js
Normal file
44
server/controllers/admin.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
const AppError = require('../middleware/AppError.js');
|
||||
const { MISSING_REQUIRED_PARAMETER, IMAGE_NOT_FOUND } = require('../constants/errorCodes');
|
||||
|
||||
class AdminController {
|
||||
|
||||
constructor(model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
get = async (req, res, next) => {
|
||||
try {
|
||||
const users = await this.model.getUsers();
|
||||
|
||||
return res.status(200).json({
|
||||
users: users
|
||||
});
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
delete = async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
if (!id) {
|
||||
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
||||
}
|
||||
|
||||
const user = await this.model.deleteUser(id);
|
||||
|
||||
if (!user) {
|
||||
throw new AppError(IMAGE_NOT_FOUND);
|
||||
}
|
||||
|
||||
return res.status(200).json({ user: user });
|
||||
} catch (error) {
|
||||
return next(error);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
module.exports = AdminController;
|
||||
114
server/models/admin.js
Normal file
114
server/models/admin.js
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
const { ObjectId } = require('mongodb');
|
||||
|
||||
class Admin {
|
||||
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
async getUsers() {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const usrColl = conn.collection('users');
|
||||
|
||||
const result = await usrColl.find({}).toArray();
|
||||
|
||||
if (!result) return null;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async deleteUser(id) {
|
||||
let deleted = false;
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const usrColl = conn.collection('users');
|
||||
|
||||
const result = await usrColl.deleteOne({ _id: ObjectId.createFromHexString(id) });
|
||||
|
||||
if (result) deleted = true;
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
async getQuizzes() {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const quizColl = conn.collection('files');
|
||||
|
||||
const result = await quizColl.find({}).toArray();
|
||||
|
||||
if (!result) return null;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async deleteQuiz(id) {
|
||||
let deleted = false;
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const quizColl = conn.collection('files');
|
||||
|
||||
const result = await quizColl.deleteOne({ _id: ObjectId.createFromHexString(id) });
|
||||
|
||||
if (result) deleted = true;
|
||||
|
||||
return deleted;
|
||||
}
|
||||
|
||||
async getImages(page, limit) {
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
|
||||
const imagesCollection = conn.collection('images');
|
||||
|
||||
|
||||
const total = await imagesCollection.countDocuments();
|
||||
if (!total || total === 0) return { images: [], total };
|
||||
|
||||
const result = await imagesCollection.find({})
|
||||
.sort({ created_at: 1 })
|
||||
.skip((page - 1) * limit)
|
||||
.limit(limit)
|
||||
.toArray();
|
||||
|
||||
const objImages = result.map(image => ({
|
||||
id: image._id,
|
||||
user: image.userId,
|
||||
file_name: image.file_name,
|
||||
file_content: image.file_content.toString('base64'),
|
||||
mime_type: image.mime_type
|
||||
}));
|
||||
|
||||
let respObj = {
|
||||
images: objImages,
|
||||
total: total
|
||||
}
|
||||
|
||||
return respObj;
|
||||
}
|
||||
|
||||
async deleteImage(uid, imgId) {
|
||||
let resp = false;
|
||||
await this.db.connect()
|
||||
const conn = this.db.getConnection();
|
||||
const quizColl = conn.collection('files');
|
||||
const rgxImg = new RegExp(`/api/image/get/${imgId}`);
|
||||
|
||||
const result = await quizColl.find({ userId: uid, content: { $regex: rgxImg }}).toArray();
|
||||
if(!result || result.length < 1){
|
||||
const imgsColl = conn.collection('images');
|
||||
const isDeleted = await imgsColl.deleteOne({ _id: ObjectId.createFromHexString(imgId) });
|
||||
if(isDeleted){
|
||||
resp = true;
|
||||
}
|
||||
}
|
||||
return { deleted: resp };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Admin;
|
||||
12
server/routers/admin.js
Normal file
12
server/routers/admin.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const admin = require('../app.js').admin;
|
||||
const asyncHandler = require('./routerUtils.js');
|
||||
|
||||
const jwt = require('../middleware/jwtToken.js');
|
||||
|
||||
|
||||
router.get("/get", jwt.authenticate, asyncHandler(admin.get));
|
||||
router.delete("/delete", jwt.authenticate, asyncHandler(admin.delete));
|
||||
|
||||
module.exports = router;
|
||||
Loading…
Reference in a new issue