-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
803c2c5
commit e95c741
Showing
9 changed files
with
258 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import express from 'express'; | ||
import { body, ValidationError, validationResult } from 'express-validator'; | ||
import { createFestivaluser, getFestivalUserById } from '../models/FestivalUser'; | ||
import { GetUserByIdRequest, GetUserByIdResponse, RegisterUserResponse } from '../interfaces/endpoints'; | ||
import { FestivalUser } from '../interfaces/models'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/:id', async (req: express.Request & { params: GetUserByIdRequest }, res: express.Response) => { | ||
const userId: string = req.params.id; | ||
|
||
let resObj: GetUserByIdResponse = { | ||
status: 404, | ||
data: null, | ||
message: 'Not Found.', | ||
}; | ||
|
||
const user = await getFestivalUserById(userId); | ||
if (user) { | ||
resObj = { | ||
status: 200, | ||
data: user as any, // todo set type | ||
message: 'Success', | ||
}; | ||
return res.json(resObj); | ||
} | ||
|
||
return res.json(resObj); | ||
}); | ||
|
||
router.post( | ||
'/', | ||
body('festivalId').isString().isLength({ min: 2 }), | ||
body('userId').isString().isLength({ min: 2 }), | ||
async (req: express.Request, res: express.Response) => { | ||
// const { firstName, lastName, gender, birthdate, email, phoneNumber } = req.body; | ||
|
||
let resObj: RegisterUserResponse = { | ||
status: 404, | ||
data: null, | ||
message: 'Not implemented yet.', | ||
}; | ||
|
||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
resObj = { | ||
status: 400, | ||
data: null, | ||
message: 'Bad Request', | ||
errors: errors.array().map((error: ValidationError) => ({ | ||
code: 400, | ||
message: `${error.param}: ${error.msg}`, | ||
})), | ||
}; | ||
return res.status(400).json(resObj); | ||
} | ||
|
||
return res.json({ | ||
status: 201, | ||
data: (await createFestivaluser({ | ||
userId: req.body.userId, | ||
festivalId: req.body.festivalId, | ||
})) as FestivalUser, | ||
message: 'Created', | ||
}); | ||
}, | ||
); | ||
|
||
export default router; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import express from 'express'; | ||
import { body, ValidationError, validationResult } from 'express-validator'; | ||
import { Festival } from '../interfaces/models'; | ||
import { | ||
CreateFestivalResponse, | ||
GetAllFestivalsResponse, | ||
GetFestivalByIdRequest, | ||
GetFestivalByIdResponse, | ||
UpdateFestivalResponse, | ||
} from '../interfaces/endpoints'; | ||
import { | ||
createFestival, getAllFestivals, getFestivalById, updateFestivalByID, | ||
} from '../models/Festival'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', async (req: express.Request, res: express.Response) => { | ||
const festivals = await getAllFestivals(); | ||
const resObj: GetAllFestivalsResponse = { | ||
status: 200, | ||
data: festivals, | ||
message: 'Success', | ||
}; | ||
return res.json(resObj); | ||
}); | ||
|
||
router.get('/:id', async (req: express.Request & { params: GetFestivalByIdRequest }, res: express.Response) => { | ||
const festivalId: string = req.params.id; | ||
|
||
let resObj: GetFestivalByIdResponse = { | ||
status: 404, | ||
data: null, | ||
message: 'Not implemented yet.', | ||
}; | ||
|
||
const festival = await getFestivalById(festivalId); | ||
if (festival) { | ||
resObj = { | ||
status: 200, | ||
data: festival, | ||
message: 'Success', | ||
}; | ||
return res.json(resObj); | ||
} | ||
|
||
return res.json(resObj); | ||
}); | ||
|
||
router.post( | ||
'/', | ||
body('name').isString().isLength({ min: 2 }), | ||
body('description').isString().isLength({ min: 2 }), | ||
body('location').isString(), | ||
async (req: express.Request, res: express.Response) => { | ||
// const { firstName, lastName, gender, birthdate, email, phoneNumber } = req.body; | ||
|
||
let resObj: CreateFestivalResponse = { | ||
status: 404, | ||
data: null, | ||
message: 'Not Found.', | ||
}; | ||
|
||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
resObj = { | ||
status: 400, | ||
data: null, | ||
message: 'Bad Request', | ||
errors: errors.array().map((error: ValidationError) => ({ | ||
code: 400, | ||
message: `${error.param}: ${error.msg}`, | ||
})), | ||
}; | ||
return res.status(400).json(resObj); | ||
} | ||
|
||
return res.json({ | ||
status: 201, | ||
data: (await createFestival({ | ||
name: req.body.name, | ||
description: req.body.description, | ||
location: req.body.location, | ||
})) as Festival, | ||
message: 'Created', | ||
}); | ||
}, | ||
); | ||
|
||
router.put( | ||
'/:id', | ||
body('name').isString().isLength({ min: 2 }), | ||
body('description').isString().isLength({ min: 2 }), | ||
body('location').isString(), | ||
async (req: express.Request, res: express.Response) => { | ||
const festivalId: string = req.params.id; | ||
|
||
let resObj: UpdateFestivalResponse = { | ||
status: 404, | ||
data: null, | ||
message: 'Not Found.', | ||
}; | ||
|
||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
resObj = { | ||
status: 400, | ||
data: null, | ||
message: 'Bad Request', | ||
errors: errors.array().map((error: ValidationError) => ({ | ||
code: 400, | ||
message: `${error.param}: ${error.msg} => ${error.value}`, | ||
})), | ||
}; | ||
return res.status(400).json(resObj); | ||
} | ||
|
||
return res.json({ | ||
status: 201, | ||
data: (await updateFestivalByID(festivalId, { | ||
name: req.body.name, | ||
description: req.body.description, | ||
location: req.body.location, | ||
})) as Festival, | ||
message: 'Updated', | ||
}); | ||
}, | ||
); | ||
|
||
export default router; |
Oops, something went wrong.