move to src
This commit is contained in:
1 parent
acc910cbc0
commit
1c47c8ce2c
127 files changed
+71
-19
No files matched your search
+77
@@ -0,0 +1,77 @@
|
||||
import { createAnnouncementValidation } from "../validation/joi.js";
|
||||
import BaseController from "./baseController.js";
|
||||
|
||||
const SERVICE_NAME = "announcementController";
|
||||
|
||||
/**
|
||||
* Controller for managing announcements in the system.
|
||||
* This class handles the creation of new announcements.
|
||||
*
|
||||
* @class AnnouncementController
|
||||
*/
|
||||
|
||||
class AnnouncementController extends BaseController {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
constructor(commonDependencies) {
|
||||
super(commonDependencies);
|
||||
this.createAnnouncement = this.createAnnouncement.bind(this);
|
||||
this.getAnnouncement = this.getAnnouncement.bind(this);
|
||||
}
|
||||
|
||||
get serviceName() {
|
||||
return AnnouncementController.SERVICE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the creation of a new announcement.
|
||||
*
|
||||
* @async
|
||||
* @param {Object} req - The request object, containing the announcement data in the body.
|
||||
* @param {Object} res - The response object used to send the result back to the client.
|
||||
* @param {Function} next - The next middleware function in the stack for error handling.
|
||||
*
|
||||
* @returns {Promise<void>} A promise that resolves once the response is sent.
|
||||
*/
|
||||
createAnnouncement = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
await createAnnouncementValidation.validateAsync(req.body);
|
||||
const { title, message } = req.body;
|
||||
const announcementData = {
|
||||
title: title.trim(),
|
||||
message: message.trim(),
|
||||
userId: req.user._id,
|
||||
};
|
||||
|
||||
const newAnnouncement = await this.db.createAnnouncement(announcementData);
|
||||
return res.success({
|
||||
msg: this.stringService.createAnnouncement,
|
||||
data: newAnnouncement,
|
||||
});
|
||||
},
|
||||
SERVICE_NAME,
|
||||
"createAnnouncement"
|
||||
);
|
||||
|
||||
/**
|
||||
* Handles retrieving announcements with pagination.
|
||||
*
|
||||
* @async
|
||||
* @param {Object} res - The response object used to send the result back to the client.
|
||||
* - `data`: The list of announcements to be sent back to the client.
|
||||
* - `msg`: A message about the success of the request.
|
||||
* @param {Function} next - The next middleware function in the stack for error handling.
|
||||
*/
|
||||
getAnnouncement = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
const allAnnouncements = await this.db.getAnnouncements();
|
||||
return res.success({
|
||||
msg: this.stringService.getAnnouncement,
|
||||
data: allAnnouncements,
|
||||
});
|
||||
},
|
||||
SERVICE_NAME,
|
||||
"getAnnouncement"
|
||||
);
|
||||
}
|
||||
|
||||
export default AnnouncementController;
|
||||
Reference in new issue
Block a user