-
-
Save ravirajawasthi/f997a05589c3daccc29a79d93d08c2ee to your computer and use it in GitHub Desktop.
router handler that is being tested
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request, Response } from 'express'; | |
import { Ticket } from '../models/tickets'; | |
import { body } from 'express-validator'; | |
import { requireAuth, validateRequest } from '@byte_b/common'; | |
const router = express.Router(); | |
router.post( | |
'/api/tickets', | |
requireAuth, | |
[ | |
body('title').not().isEmpty().withMessage('Title is required'), | |
body('price').notEmpty().withMessage('Invalid Price'), | |
body('price') | |
.isFloat({ gt: 0 }) | |
.withMessage('Price must be greater than 0'), | |
], | |
validateRequest, | |
async (req: Request, res: Response) => { | |
const { title, price } = req.body; | |
const userId = req.currentUser!.id; //requireAuth middleware guarantees that req.currentUser exists | |
const newTicket = Ticket.build({ title, price, userId }); | |
await newTicket.save(); | |
res.status(201).send(newTicket); | |
} | |
); | |
export { router as createTicketRouter }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment