generated from csc301-2023-fall/deliverable-documents
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcampusController.js
92 lines (77 loc) · 2.22 KB
/
campusController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* This module implements the controller for requests for campus service
* operations.
*
* @module api/controllers/campusController
*
* @requires api/services/campusService
* @requires api/entities/ServiceErrors
*/
const campusService = require('../services/campusService');
const {CampusServiceError, STATUS_CODES} = require('../entities/ServiceErrors');
/**
* Retrieves a campus by ID.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Object} - An object containing a canmpus.
*/
async function getCampus(req, res) {
const campus_id = req.params.campus_id;
// Check paramaters are valid
if (!campus_id) {
throw new CampusServiceError(
`Invalid paramaters provided for request`,
STATUS_CODES.INVALID
);
}
const campus = await campusService.getCampus(campus_id);
res.status(STATUS_CODES.SUCCESS);
return {
campus: campus
};
}
/**
* Retrieves all campuses.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Object} - An object containing an array of campuses with camp and room ids.
*/
async function getAllCampuses(req, res) {
const allcampuses = await campusService.getAllCampuses();
res.status(STATUS_CODES.SUCCESS);
return {
campuses: allcampuses.map((campus) => {
return {
...campus,
camp_ids: campus.getCampIds(),
room_ids: campus.getRoomIds()
};
})
};
}
/**
* Creates a new campus.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @returns {Object} - An object containing a status message.
*/
async function createCampus(req, res) {
const name = req.body.name;
// Check paramaters are valid
if (!name) {
throw new CampusServiceError(
`Invalid paramaters provided for request`,
STATUS_CODES.INVALID
);
}
const status = await campusService.createCampus(name);
res.status(STATUS_CODES.SUCCESS);
return {
status: status ? 'Success' : 'failure'
};
}
module.exports = {
getCampus,
getAllCampuses,
createCampus
}