11import express from 'express' ;
22import type { Request , Response } from 'express' ;
3- import { requireAuth } from '../middleware/jwt.ts' ;
43import AuctionsService from '../services/auctions.ts' ;
54import {
65 createAuctionSchema ,
76 createBidSchema ,
87 updateAuctionSchema ,
98} from 'treasure-trove-shared' ;
109import BidsService from '../services/bids.ts' ;
10+ import { userFullAuth } from 'src/middleware/userAuth.ts' ;
1111
1212const auctionsRouter = express . Router ( ) ;
1313
1414// GET information about all auctions.
15- auctionsRouter . get ( '/' , requireAuth , async ( _req : Request , res : Response ) => {
15+ auctionsRouter . get ( '/' , userFullAuth , async ( _req : Request , res : Response ) => {
1616 try {
1717 const auctions = await AuctionsService . getAllAuctions ( ) ;
1818 res . status ( 200 ) . json ( auctions ) ;
@@ -23,18 +23,22 @@ auctionsRouter.get('/', requireAuth, async (_req: Request, res: Response) => {
2323} ) ;
2424
2525// GET information about the auction with the given ID.
26- auctionsRouter . get ( '/:id' , requireAuth , async ( req : Request , res : Response ) => {
27- try {
28- const auctionInfo = await AuctionsService . getAuctionById ( req . params . id ) ;
29- return res . status ( 200 ) . json ( auctionInfo ) ;
30- } catch ( error ) {
31- console . error ( 'Error fetching auction info:' , error ) ;
32- return res . status ( 404 ) . json ( { error : 'Auction not found' } ) ;
33- }
34- } ) ;
26+ auctionsRouter . get (
27+ '/:id' ,
28+ userFullAuth ,
29+ async ( req : Request , res : Response ) => {
30+ try {
31+ const auctionInfo = await AuctionsService . getAuctionById ( req . params . id ) ;
32+ return res . status ( 200 ) . json ( auctionInfo ) ;
33+ } catch ( error ) {
34+ console . error ( 'Error fetching auction info:' , error ) ;
35+ return res . status ( 404 ) . json ( { error : 'Auction not found' } ) ;
36+ }
37+ } ,
38+ ) ;
3539
3640// POST endpoint to create a new auction.
37- auctionsRouter . post ( '/' , requireAuth , async ( req : Request , res : Response ) => {
41+ auctionsRouter . post ( '/' , userFullAuth , async ( req : Request , res : Response ) => {
3842 try {
3943 const validatedBody = createAuctionSchema . validateSync ( req . body ) ;
4044 const auction = await AuctionsService . createAuction ( validatedBody ) ;
@@ -49,25 +53,29 @@ auctionsRouter.post('/', requireAuth, async (req: Request, res: Response) => {
4953} ) ;
5054
5155// PUT endpoint to update information about an existing auction.
52- auctionsRouter . put ( '/:id' , requireAuth , async ( req : Request , res : Response ) => {
53- try {
54- const validatedBody = updateAuctionSchema . validateSync ( req . body ) ;
55- const auctionInfo = await AuctionsService . updateAuction (
56- req . params . id ,
57- validatedBody ,
58- ) ;
59- return res . status ( 200 ) . json ( auctionInfo ) ;
60- } catch ( error ) {
61- console . error ( 'Error updating auction:' , error ) ;
62- // Use 400 when there is a bad request for some reason.
63- return res . status ( 400 ) . json ( { error : 'failed to update auction ' } ) ;
64- }
65- } ) ;
56+ auctionsRouter . put (
57+ '/:id' ,
58+ userFullAuth ,
59+ async ( req : Request , res : Response ) => {
60+ try {
61+ const validatedBody = updateAuctionSchema . validateSync ( req . body ) ;
62+ const auctionInfo = await AuctionsService . updateAuction (
63+ req . params . id ,
64+ validatedBody ,
65+ ) ;
66+ return res . status ( 200 ) . json ( auctionInfo ) ;
67+ } catch ( error ) {
68+ console . error ( 'Error updating auction:' , error ) ;
69+ // Use 400 when there is a bad request for some reason.
70+ return res . status ( 400 ) . json ( { error : 'failed to update auction ' } ) ;
71+ }
72+ } ,
73+ ) ;
6674
6775// GET all the bids associated with a given auction.
6876auctionsRouter . get (
6977 '/:id/bids' ,
70- requireAuth ,
78+ userFullAuth ,
7179 async ( req : Request , res : Response ) => {
7280 try {
7381 const bidsInfo = await BidsService . getAuctionBids ( req . params . id ) ;
@@ -82,7 +90,7 @@ auctionsRouter.get(
8290// POST endpoint to create a new bid for the given auction.
8391auctionsRouter . post (
8492 '/:id/bids' ,
85- requireAuth ,
93+ userFullAuth ,
8694 async ( req : Request , res : Response ) => {
8795 try {
8896 const validatedBody = createBidSchema . validateSync ( req . body ) ;
@@ -101,7 +109,7 @@ auctionsRouter.post(
101109// DELETE an auction.
102110auctionsRouter . delete (
103111 '/:id' ,
104- requireAuth ,
112+ userFullAuth ,
105113 async ( req : Request , res : Response ) => {
106114 try {
107115 await AuctionsService . deleteAuction ( req . params . id ) ;
0 commit comments