-
Notifications
You must be signed in to change notification settings - Fork 0
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
f28d172
commit 4045580
Showing
5 changed files
with
159 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Router } from "express"; | ||
import { StatusCodes } from "http-status-codes"; | ||
import { SpeakerValidator } from "./speakers-schema"; | ||
import { Database } from "../../database"; | ||
import RoleChecker from "../../middleware/role-checker"; | ||
import { Role } from "../auth/auth-models"; | ||
|
||
const speakersRouter = Router(); | ||
|
||
// Get all speakers | ||
speakersRouter.get("/", RoleChecker([], true), async (req, res, next) => { | ||
try { | ||
const speakers = await Database.SPEAKERS.find(); | ||
return res.status(StatusCodes.OK).json(speakers); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
// Get a specific speaker | ||
speakersRouter.get( | ||
"/:SPEAKERID", | ||
RoleChecker([], true), | ||
async (req, res, next) => { | ||
const speakerId = req.params.SPEAKERID; | ||
|
||
try { | ||
const speaker = await Database.SPEAKERS.findOne({ speakerId }); | ||
|
||
if (!speaker) { | ||
return res | ||
.status(StatusCodes.NOT_FOUND) | ||
.json({ error: "DoesNotExist" }); | ||
} | ||
|
||
return res.status(StatusCodes.OK).json(speaker); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
// Create a new speaker | ||
speakersRouter.post( | ||
"/", | ||
RoleChecker([Role.Enum.STAFF]), | ||
async (req, res, next) => { | ||
try { | ||
const validatedData = SpeakerValidator.parse(req.body); | ||
const speaker = new Database.SPEAKERS(validatedData); | ||
await speaker.save(); | ||
return res.status(StatusCodes.CREATED).json(speaker); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
// Update a speaker | ||
speakersRouter.put( | ||
"/:SPEAKERID", | ||
RoleChecker([Role.Enum.STAFF], true), | ||
async (req, res, next) => { | ||
const speakerId = req.params.SPEAKERID; | ||
|
||
try { | ||
const validatedData = SpeakerValidator.parse(req.body); | ||
const speaker = await Database.SPEAKERS.findOneAndUpdate( | ||
{ speakerId }, | ||
{ $set: validatedData }, | ||
{ new: true, runValidators: true } | ||
); | ||
|
||
if (!speaker) { | ||
return res | ||
.status(StatusCodes.NOT_FOUND) | ||
.json({ error: "DoesNotExist" }); | ||
} | ||
|
||
return res.status(StatusCodes.OK).json(speaker); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
// Delete a speaker | ||
speakersRouter.delete( | ||
"/:SPEAKERID", | ||
RoleChecker([Role.Enum.STAFF], true), | ||
async (req, res, next) => { | ||
const speakerId = req.params.SPEAKERID; | ||
|
||
try { | ||
await Database.SPEAKERS.findOneAndDelete({ speakerId }); | ||
|
||
return res.sendStatus(StatusCodes.NO_CONTENT); | ||
} catch (error) { | ||
next(error); | ||
} | ||
} | ||
); | ||
|
||
export default speakersRouter; |
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,48 @@ | ||
import { Schema } from "mongoose"; | ||
import { z } from "zod"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
// Zod schema for speaker | ||
export const SpeakerValidator = z.object({ | ||
speakerId: z.coerce.string().default(() => uuidv4()), | ||
name: z.string(), | ||
title: z.string(), | ||
bio: z.string(), | ||
eventTitle: z.string(), | ||
eventDescription: z.string(), | ||
imgUrl: z.string(), | ||
}); | ||
|
||
// Mongoose schema for speaker | ||
export const SpeakerSchema = new Schema({ | ||
speakerId: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
default: () => uuidv4(), | ||
}, | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
bio: { | ||
type: String, | ||
required: true, | ||
}, | ||
eventTitle: { | ||
type: String, | ||
required: true, | ||
}, | ||
eventDescription: { | ||
type: String, | ||
required: true, | ||
}, | ||
imgUrl: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); |
Empty file.