Skip to content

Instantly share code, notes, and snippets.

@urbanisierung
Created December 10, 2020 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbanisierung/0afb2d71eb2fd1154ffdf238f995c60c to your computer and use it in GitHub Desktop.
Save urbanisierung/0afb2d71eb2fd1154ffdf238f995c60c to your computer and use it in GitHub Desktop.
const express = require('express')
import { NextFunction, Request, Response } from 'express'
import { MoodController } from '../../controller/mood.controller'
import { StorageController } from '../../controller/storage.controller'
import { Error, ErrorType } from '../../utils/Error'
export class MoodRouter {
public router = express.Router({ mergeParams: true })
constructor(store: StorageController) {
this.router.post(
'/:moodRequestId/:moodValue',
async (req: Request, res: Response, next: NextFunction) => {
const moodRequestId: string = req.params.moodRequestId
const moodValue: number = Number(req.params.moodValue)
try {
if (!moodRequestId || !moodValue) {
throw new Error(ErrorType.BadRequest, `Mandatory parameter missing`)
}
if (moodValue < 1 || moodValue > 5) {
throw new Error(
ErrorType.BadRequest,
`Mood ${moodValue} is not in range [1,5]`,
)
}
const moodController = new MoodController(store)
await moodController.setMood(moodRequestId, moodValue)
res.send()
} catch (error) {
next(error)
}
},
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment