Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pratyush1718/counselor get route #67

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions backend/index.ts

This file was deleted.

28 changes: 14 additions & 14 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 51 additions & 4 deletions backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/**
* Initializes mongoose and express.
*/

import dotenv from "dotenv";
import mongoose from "mongoose";

Check warning on line 2 in backend/src/app.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

`mongoose` import should occur after import of `http-errors`
import express, { Express, NextFunction, Request, Response } from "express";
import { isHttpError } from "http-errors";

import announcementRoutes from "../src/routes/announcement";
import discussionRoutes from "../src/routes/discussion";
import replyRoutes from "../src/routes/reply";
import userRoutes from "../src/routes/user";

import { mongoURI } from "./config";

// Load environment variables
dotenv.config();

// Connect to MongoDB
void mongoose
.connect(mongoURI)
Expand All @@ -19,3 +26,43 @@
console.log("An unknown error occurred");
}
});


const app: Express = express();
const port = process.env.PORT ?? 3001;

app.use(express.json());

app.use("/api/announcement", announcementRoutes);
app.use("/api/users", userRoutes);
app.use("/api/discussions", discussionRoutes);
app.use("/api/replies", replyRoutes);


app.get("/", (req: Request, res: Response) => {
res.send("Express + TypeScript Server");
});

// Error handling middleware
app.use((error: unknown, req: Request, res: Response, next: NextFunction) => {

Check failure on line 47 in backend/src/app.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

'next' is defined but never used. Allowed unused args must match /^_/u
// 500 is the "internal server error" error code, this will be our fallback
let statusCode = 500;
let errorMessage = "An error has occurred.";

// Check if the error is an instance of HttpError
if (isHttpError(error)) {
statusCode = error.status;
errorMessage = error.message;
}
// Handle general errors
else if (error instanceof Error) {
errorMessage = error.message;
}

res.status(statusCode).json({ error: errorMessage });
});

// Start the server
app.listen(port, () => {
console.log(`[server]: Server is running at http://localhost:${port}`);

Check failure on line 67 in backend/src/app.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

Invalid type "string | 3001" of template literal expression
});
2 changes: 1 addition & 1 deletion backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function throwIfUndefined(envVar: string | undefined, error: Error) {
return envVar;
}

const port = throwIfUndefined(process.env.APP_PORT, new Error("No Port Found"));
const port = throwIfUndefined(process.env.PORT, new Error("No Port Found"));
const mongoURI = throwIfUndefined(process.env.MONGO_URI, new Error("No Mongo URI Found"));

export { port, mongoURI };
10 changes: 6 additions & 4 deletions backend/src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Request, RequestHandler, Response } from "express";
import mongoose from "mongoose";

Check warning on line 2 in backend/src/controllers/user.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

There should be at least one empty line between import groups

Check failure on line 2 in backend/src/controllers/user.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

'mongoose' is defined but never used. Allowed unused vars must match /^_/u
import User from '../models/user'

// Temporary storage until database is set up
type User = {
Expand Down Expand Up @@ -49,12 +51,12 @@
}
};

export const getAllUsers = (_req: Request, res: Response) => {
export const getAllUsers: RequestHandler = async (req, res, next) => {
try {
const users = await User.find();

Check failure on line 56 in backend/src/controllers/user.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

'users' is already declared in the upper scope on line 12 column 7
res.status(200).json({ users });
} catch (error) {
console.error("Error getting users:", error);
res.status(500).json({ error: "Internal server error" });
next(error);
}
};

Expand All @@ -77,7 +79,7 @@
}
};

export const getPersonalInformation: RequestHandler = async (req, res, next) => {

Check failure on line 82 in backend/src/controllers/user.ts

View workflow job for this annotation

GitHub Actions / Backend lint and style check

Async arrow function 'getPersonalInformation' has no 'await' expression
try {
res.status(200).send("Get personal information route works!");
} catch (error) {
Expand Down Expand Up @@ -127,7 +129,7 @@

export const getDirectoryDisplayInfo: RequestHandler = async (req, res, next) => {
try {
res.status(200).send("Get directory display information route works!");
res.status(200).send("get directory display information route works!");
} catch (error) {
next(error);
}
Expand Down
Loading