Skip to content

Commit ee48e49

Browse files
Copilotcubapthehabes
authored
Refactor if/else statements to use guard clauses for improved readability (#206)
* Initial plan * Refactor route files to use guard clauses instead of if/else Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Refactor app.js maintenance mode check to use guard clause Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Update package-lock.json * Invert patch override check in POST routes Simplify POST handlers for patch routes by inverting the rest.checkPatchOverrideSupport condition and moving the controller invocation out of the conditional in routes/patchSet.js, routes/patchUnset.js, and routes/patchUpdate.js. Behavior is preserved: if patch-override is not supported respond with 405, otherwise call the corresponding controller. This reduces nesting and improves readability. * no diff --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> Co-authored-by: Bryan Haberberger <bryan.j.haberberger@slu.edu> Co-authored-by: Patrick Cuba <cubap@slu.edu>
1 parent b48e6f9 commit ee48e49

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ app.use(express.static(path.join(__dirname, 'public')))
7272
app.all('*_', (req, res, next) => {
7373
if(process.env.DOWN === "true"){
7474
res.status(503).json({"message":"RERUM v1 is down for updates or maintenance at this time. We apologize for the inconvenience. Try again later."})
75+
return
7576
}
76-
else{
77-
next() //pass on to the next app.use
78-
}
77+
next() //pass on to the next app.use
7978
})
8079

8180
app.use('/', indexRouter)

routes/patchSet.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import rest from '../rest.js'
88
router.route('/')
99
.patch(auth.checkJwt, rest.verifyJsonContentType, controller.patchSet)
1010
.post(auth.checkJwt, rest.verifyJsonContentType, (req, res, next) => {
11-
if (rest.checkPatchOverrideSupport(req, res)) {
12-
controller.patchSet(req, res, next)
13-
}
14-
else {
11+
if (!rest.checkPatchOverrideSupport(req, res)) {
1512
res.statusMessage = 'Improper request method for updating, please use PATCH to add new keys to this object.'
1613
res.status(405)
1714
next(res)
15+
return
1816
}
17+
controller.patchSet(req, res, next)
1918
})
2019
.all((req, res, next) => {
2120
res.statusMessage = 'Improper request method for updating, please use PATCH to add new keys to this object.'

routes/patchUnset.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import rest from '../rest.js'
88
router.route('/')
99
.patch(auth.checkJwt, rest.verifyJsonContentType, controller.patchUnset)
1010
.post(auth.checkJwt, rest.verifyJsonContentType, (req, res, next) => {
11-
if (rest.checkPatchOverrideSupport(req, res)) {
12-
controller.patchUnset(req, res, next)
13-
}
14-
else {
11+
if (!rest.checkPatchOverrideSupport(req, res)) {
1512
res.statusMessage = 'Improper request method for updating, please use PATCH to remove keys from this object.'
1613
res.status(405)
1714
next(res)
15+
return
1816
}
17+
controller.patchUnset(req, res, next)
1918
})
2019
.all((req, res, next) => {
2120
res.statusMessage = 'Improper request method for updating, please use PATCH to remove keys from this object.'

routes/patchUpdate.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import auth from '../auth/index.js'
99
router.route('/')
1010
.patch(auth.checkJwt, rest.verifyJsonContentType, controller.patchUpdate)
1111
.post(auth.checkJwt, rest.verifyJsonContentType, (req, res, next) => {
12-
if (rest.checkPatchOverrideSupport(req, res)) {
13-
controller.patchUpdate(req, res, next)
14-
}
15-
else {
12+
if (!rest.checkPatchOverrideSupport(req, res)) {
1613
res.statusMessage = 'Improper request method for updating, please use PATCH to alter the existing keys this object.'
1714
res.status(405)
1815
next(res)
16+
return
1917
}
18+
controller.patchUpdate(req, res, next)
2019
})
2120
.all((req, res, next) => {
2221
res.statusMessage = 'Improper request method for updating, please use PATCH to alter existing keys on this object.'

0 commit comments

Comments
 (0)