Skip to content

Commit

Permalink
speaker service
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Jul 15, 2024
1 parent f28d172 commit 4045580
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import s3Router from "./services/s3/s3-router";
import statsRouter from "./services/stats/stats-router";
import sponsorRouter from "./services/sponsor/sponsor-router";
import subscriptionRouter from "./services/subscription/subscription-router";
import speakersRouter from "./services/speakers/speakers-router";

const app = express();

Expand All @@ -45,6 +46,7 @@ app.use("/s3", databaseMiddleware, s3Router);
app.use("/stats", databaseMiddleware, statsRouter);
app.use("/sponsor", databaseMiddleware, sponsorRouter);
app.use("/subscription", databaseMiddleware, subscriptionRouter);
app.use("/speakers", databaseMiddleware, speakersRouter);

app.get("/status", (_, res) => {
return res.status(StatusCodes.OK).send("API is alive!");
Expand Down
5 changes: 5 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import {
NotificationsSchema,
NotificationsValidator,
} from "./services/notifications/notifications-schema";
import {
SpeakerSchema,
SpeakerValidator,
} from "./services/speakers/speakers-schema";

mongoose.set("toObject", { versionKey: false });

Expand Down Expand Up @@ -89,4 +93,5 @@ export const Database = {
NotificationsSchema,
NotificationsValidator
),
SPEAKERS: initializeModel("speakers", SpeakerSchema, SpeakerValidator),
};
104 changes: 104 additions & 0 deletions src/services/speakers/speakers-router.ts
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;
48 changes: 48 additions & 0 deletions src/services/speakers/speakers-schema.ts
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.

0 comments on commit 4045580

Please sign in to comment.