-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCatEndpoints.ts
42 lines (38 loc) · 1.33 KB
/
CatEndpoints.ts
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
import { NextFunction, Request, Response } from 'express'
import * as HttpStatus from 'http-status-codes'
import { FeatureToggles } from '../middlewares/feature-toggles/features'
export class CatEndpoints {
public getCatDetails = async (req: Request, res: Response, next: NextFunction) => {
try {
const catId = req.params.catId
const cat = req.services.catService.getCat(catId)
if (cat) {
res.json(cat)
} else {
res.sendStatus(HttpStatus.NOT_FOUND)
}
} catch (err) {
// something could fail unexpectedly...
// at some point the middleware chain should handle errors
next(err)
}
}
public getAllCats = async (req: Request, res: Response, next: NextFunction) => {
try {
res.json(req.services.catService.getAllCats())
} catch (err) {
next(err)
}
}
public getCatsStatistics = async (req: Request, res: Response, next: NextFunction) => {
try {
if (req.fflip.has(FeatureToggles.WITH_CAT_STATISTICS)) {
res.json(req.services.catService.getCatsStatistics())
} else {
res.sendStatus(HttpStatus.NOT_FOUND)
}
} catch (err) {
next(err)
}
}
}