Skip to content

Commit

Permalink
CHE-17 Created alumni model, alumni type file, added routes and getAl…
Browse files Browse the repository at this point in the history
…lAlumniData controller and basic fetch test on Directory page
  • Loading branch information
brok3turtl3 committed Mar 16, 2024
1 parent e8fd24b commit 6c0ff9c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
16 changes: 15 additions & 1 deletion client/src/pages/DirectoryPage/DirectoryPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import React from "react";
import React, { useEffect } from "react";
import { useAppSelector } from "../../app/hooks";
import axios from "axios";

const DirectoryPage = (): JSX.Element => {
const user = useAppSelector((state) => state.user.userData);

useEffect(() => {
const getAlumniData = async () => {
try {
const response = await axios.get("/api/alumni");
console.log(response);
} catch (error) {
console.log("Something just caught fire in Directory useEffect");
}
};

getAlumniData();
}, []);

return (
<div className="min-h-screen bg-gray-100 flex flex-col items-center justify-center">
<h1 className="text-4xl font-extrabold mb-4">Directory Page</h1>
Expand Down
18 changes: 18 additions & 0 deletions server/controllers/alumniControllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Alumni from "../models/alumniModel";
import { Request, Response, NextFunction } from "express";
import { IAlumni } from "../types/alumni";

const getAllAlumniData = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const alumni: IAlumni[] = await Alumni.find({});
return res.status(200).json(alumni);
} catch (error) {
console.log("Awesome error handling");
}
};

export { getAllAlumniData };
2 changes: 2 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path";
import express, { Request, Response, Application, NextFunction } from "express";
import userRoutes from "./routes/userRoutes";
import profileRoutes from "./routes/profileRoutes";
import alumniRoutes from "./routes/alumniRoutes";
import connectDB from "./config/db";
import dotenv from "dotenv";
import { notFound, errorHandler } from "./controllers/errorControllers";
Expand All @@ -16,6 +17,7 @@ connectDB();

app.use("/api/users", userRoutes);
app.use("/api/profiles", profileRoutes);
app.use("/api/alumni", alumniRoutes);

console.log(`ENV BEFORE CHECK: ${process.env.NODE_ENV}`);

Expand Down
31 changes: 31 additions & 0 deletions server/models/alumniModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose, { Schema } from "mongoose";
import { IAlumni } from "../types/alumni";

const alumniSchema = new Schema<IAlumni>(
{
company: { type: String, trim: true },
name: { type: String, trim: true, required: true },
email: { type: String, trim: true, required: true },
linkedIn: { type: String, trim: true },
campus: { type: String, trim: true },
cohort: {
type: Schema.Types.Mixed, //TODO Better way to do this? Spreadhsete has strings and numbers...
required: true,
validate: {
validator: function (v: string | number) {
return typeof v === "string" || typeof v === "number";
},
message: (props: { value: string | number }) =>
`${props.value} is not a valid number or string!`,
},
},
jobTitle: { type: String, trim: true },
industry: { type: String, trim: true, default: "" },
cities: [{ type: String, trim: true }],
},
{ timestamps: true }
);

const Alumni = mongoose.model<IAlumni>("Alumni", alumniSchema);

export default Alumni;
9 changes: 9 additions & 0 deletions server/routes/alumniRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from "express";

import { getAllAlumniData } from "../controllers/alumniControllers";

const router = express.Router();

router.get("/", getAllAlumniData);

export default router;
13 changes: 13 additions & 0 deletions server/types/alumni.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Schema } from "mongoose";

export interface IAlumni extends Document {
company: string;
name: string;
email: string;
linkedIn: string;
campus: string;
cohort: Schema.Types.Mixed;
jobTitle: string;
industry: string;
cities: string[];
}

0 comments on commit 6c0ff9c

Please sign in to comment.