Skip to content

Instantly share code, notes, and snippets.

@ravirajawasthi
Created May 29, 2020 13:35
Show Gist options
  • Save ravirajawasthi/f997a05589c3daccc29a79d93d08c2ee to your computer and use it in GitHub Desktop.
Save ravirajawasthi/f997a05589c3daccc29a79d93d08c2ee to your computer and use it in GitHub Desktop.
router handler that is being tested
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