Something went wrong. Try again.
A project designed to automate the process of fetching result screenshots from the MGU result portal, sending them to students via email as…
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
const express = require('express');const router = express.Router();
const Batch = require('../../models/batch');const verifyToken = require('../../middleware/authentication');
/** * @route GET /api/v4/batch * @desc Get all batches with pagination * @access Authenticated * @params page, limit, search * @return message, data * @error 400, { error } * @status 200, 400 * * @example /api/v4/batch?page=1&limit=10&search=keyword**/
router.get('/', async (req, res) => {
let page = parseInt(req.query.page) || 1; let limit = parseInt(req.query.limit) || 10; let search = req.query.search || null;
let query = {}; if (search) { query = { $text: { $search: search } }; }
let totalBatches = await Batch.countDocuments(query) .catch(err => { res.status(400).json({ status: 400, message: 'Error retrieving batches', error: err }); });
let batches = await Batch.find(query) .skip((page - 1) * limit) .limit(limit) .select('-__v -_id') .catch(err => { res.status(400).json({ status: 400, message: 'Error retrieving batches', error: err }); });
res.status(200).json({ status: 200, message: 'Batches retrieved successfully', data: { batches: batches, meta: { page: page, limit: limit, pages: Math.ceil(totalBatches / limit), total: totalBatches, search: search } } });
});
/** * @route POST /api/v4/batch * @desc Create new batch * @access Authenticated * @params title, description, programme, prn * @return message, data * @error 400, { error } * @status 201, 400 * * @example /api/v4/batch**/
router.post('/', async (req, res) => { const newBatch = new Batch({ title: req.body.title, description: req.body.description, programme: req.body.programme, prn: req.body.prn });
await newBatch.save() .then(batch => { res.status(201).json({ status: 201, message: 'Batch created successfully', // data: batch }); }) .catch(err => { res.status(400).json({ status: 400, message: 'Error creating batch', error: err }); });});
/** * @route PATCH /api/v4/batch/:id * @desc Update a batch by batch_id * @access Authenticated * @params title, description, programme, prn * @return message, data * @error 400, { error } * @status 200, 400 * * @example /api/v4/batch/123456**/
router.patch('/:id', async (req, res) => { const batchId = req.params.id;
await Batch.findOneAndUpdate({ batch_id: batchId }, { title: req.body.title, description: req.body.description, programme: req.body.programme, prn: req.body.prn, updated_at: Date.now() }) .then(batch => { res.status(200).json({ status: 200, message: 'Batch updated successfully', // data: batch }); }) .catch(err => { res.status(400).json({ status: 400, message: 'Error updating batch', error: err }); });});
module.exports = router;