Skip to content

Commit

Permalink
Merge pull request #11 from yale-swe/anna-dev
Browse files Browse the repository at this point in the history
Added routes to server.js; debugged data model imports and exports
  • Loading branch information
annayzhang1337 authored Oct 28, 2023
2 parents 49e053b + 56fcbde commit 0d1c56d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions server/controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import User from "../models/User.js";
import UserModel from "../models/User.js";
import { handleServerError, handleSuccess } from "../utils/handlers.js";

export const register = async (req, res) => {
Expand All @@ -8,7 +8,7 @@ export const register = async (req, res) => {
const salt = await bcrypt.genSalt(10);
const passwordHash = await bcrypt.hash(password, salt);

const newUser = new User({
const newUser = new UserModel({
userName,
firstName,
lastName,
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const MessageModel = require('../models/Message.js');
import MessageModel from "../models/Message.js";
import { handleServerError, handleSuccess } from '../utils/handlers.js';

// Post message
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/reply.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ReplyModel = require('../models/Reply.js');
import ReplyModel from "../models/Reply.js";
import { handleServerError, handleSuccess } from '../utils/handlers.js';

// Reply to a message
Expand Down
24 changes: 12 additions & 12 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import User from "../models/User.js";
import UserModel from "../models/User.js";
import { handleBadRequest } from "../utils/handlers.js";

export const getUserById = async (req, res) => {
try {
const user = await User.findById(req.params.userId);
const user = await UserModel.findById(req.params.userId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -19,7 +19,7 @@ export const getUserByEmailOrUsername = async (req, res) => {
try {
const { emailOrUsername } = req.params;

const user = await User.findOne({
const user = await UserModel.findOne({
$or: [{ email: emailOrUsername }, { userName: emailOrUsername }],
});

Expand All @@ -36,7 +36,7 @@ export const getUserByEmailOrUsername = async (req, res) => {
export const getUserFriends = async (req, res) => {
try {
const userId = req.params.userId;
const user = await User.findById(userId);
const user = await UserModel.findById(userId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -57,8 +57,8 @@ export const addUserFriend = async (req, res) => {
return handleBadRequest(res, "You can't friend yourself!");
}

const user = await User.findById(userId);
const friend = await User.findById(friendId);
const user = await UserModel.findById(userId);
const friend = await UserModel.findById(friendId);

if (!user) {
return handleNotFound(res, "User not found");
Expand Down Expand Up @@ -88,8 +88,8 @@ export const removeUserFriend = async (req, res) => {
const { friendId } = req.body;

try {
const user = await User.findById(userId);
const friend = await User.findById(friendId);
const user = await UserModel.findById(userId);
const friend = await UserModel.findById(friendId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -115,7 +115,7 @@ export const getUserMessages = async (req, res) => {
try {
const { userId } = req.params;

const user = await User.findById(userId);
const user = await UserModel.findById(userId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -131,7 +131,7 @@ export const deleteUser = async (req, res) => {
try {
const { userId } = req.params;

const user = await User.findById(userId);
const user = await UserModel.findById(userId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -149,7 +149,7 @@ export const toggleNotifyFriends = async (req, res) => {
try {
const { userId } = req.params;

const user = await User.findById(userId);
const user = await UserModel.findById(userId);

if (!user) {
return handleNotFound(res, "User not found");
Expand All @@ -169,7 +169,7 @@ export const updateUserProfile = async (req, res) => {
const { userName, password, firstName, lastName, email, avatar } =
req.body;

const user = await User.findById(userId);
const user = await UserModel.findById(userId);

if (!user) {
return handleNotFound(res, "User not found.");
Expand Down
3 changes: 2 additions & 1 deletion server/models/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export const MessageSchema = new Schema({
replies: [ReplySchema],
});

export const MessageModel = mongoose.model("Message", MessageSchema);
const MessageModel = mongoose.model("Message", MessageSchema);
export default MessageModel;
3 changes: 2 additions & 1 deletion server/models/Reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const ReplySchema = new Schema({
},
});

export const ReplyModel = mongoose.model("Reply", ReplySchema);
const ReplyModel = mongoose.model("Reply", ReplySchema);
export default ReplyModel;
4 changes: 2 additions & 2 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ export const UserSchema = new Schema({
},
});

const User = mongoose.model("User", UserSchema);
export default User;
const UserModel = mongoose.model("User", UserSchema);
export default UserModel;
8 changes: 8 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import dotenv from "dotenv";
import cors from "cors";
import { connectDB } from "./db.js";
import authRoutes from "./routes/auth.js";
import messageRoutes from "./routes/message.js";
import replyRoutes from "./routes/reply.js";
import userRoutes from "./routes/user.js";

// run and set env variables
dotenv.config();
Expand All @@ -24,7 +27,12 @@ app.use(
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));

// Add backend routes
app.use("/auth", authRoutes);
app.use("/message", messageRoutes);
app.use("/reply", replyRoutes);
app.use("/user", userRoutes);


const server = app.listen(PORT, console.log(`Server running on port ${PORT}`));

Expand Down

0 comments on commit 0d1c56d

Please sign in to comment.