Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Gather307/Gather
Browse files Browse the repository at this point in the history
  • Loading branch information
SilveerDusk committed Jun 4, 2024
2 parents 2e13449 + 57f1c04 commit b32be95
Show file tree
Hide file tree
Showing 40 changed files with 304 additions and 89 deletions.
3 changes: 3 additions & 0 deletions backend/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dotenv.config();
type User = { username: string; hashedPassword: string };
const creds = [User]; // username, hashedPassword

// Middleware to authenticate user using JWT
export function authenticateUser(req: Request, res: Response, next: any) {
const authHeader = req.headers["authorization"];
//Getting the 2nd part of the auth header (the token)
Expand All @@ -34,6 +35,7 @@ export function authenticateUser(req: Request, res: Response, next: any) {
}
}

// Controller function to handle user login
export const loginUser = async (req: Request, res: Response) => {
connectDB();
const { username, password } = req.body; // from form
Expand Down Expand Up @@ -71,6 +73,7 @@ export const loginUser = async (req: Request, res: Response) => {
}
};

// Function to generate a JWT access token
function generateAccessToken(username: any) {
return new Promise((resolve, reject) => {
jwt.sign(
Expand Down
11 changes: 7 additions & 4 deletions backend/models/basketSchema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import mongoose, { Schema } from "mongoose";

// Defining the type for a basket object, specifying structure of basket data.
export type IBasket = {
_id: Schema.Types.ObjectId;
_id: Schema.Types.ObjectId; // Unique identifier for the basket, using MongoDB ObjectId.
basketName: string;
description: string;
members: Schema.Types.ObjectId[];
items: Schema.Types.ObjectId[];
created: Date;
members: Schema.Types.ObjectId[]; // Array of ObjectIds referencing the members of the basket.
items: Schema.Types.ObjectId[]; // Array of ObjectIds referencing the items of the basket.
created: Date; // Timestamp for when the basket was creaed
};

// Defining the data types and requirements for each field in our basket schema
const BasketSchema = new Schema<IBasket>({
basketName: { type: String, required: true },
description: { type: String, required: true },
Expand All @@ -17,6 +19,7 @@ const BasketSchema = new Schema<IBasket>({
created: { type: Date, required: true, default: Date.now },
});

// Create a model for the basket schema, using the existing "basket" model if it exists, otherwise creating a new one.
const Basket =
mongoose.models["basket"] || mongoose.model("basket", BasketSchema);

Expand Down
7 changes: 5 additions & 2 deletions backend/models/groupSchema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import mongoose, { Schema } from "mongoose";

// Defining the type for a group object, specifying structure of group data.
export type IGroup = {
_id: Schema.Types.ObjectId;
groupName: string;
privateGroup: boolean;
description: string;
members: Schema.Types.ObjectId[];
baskets: Schema.Types.ObjectId[];
members: Schema.Types.ObjectId[]; // Array of ObjectIds referencing the members of the group.
baskets: Schema.Types.ObjectId[]; // Array of ObjectIds referencing the baskets of the group.
created: Date;
};

// Defining the data types and requirements for each field in our group schema
const GroupSchema = new Schema<IGroup>({
groupName: { type: String, required: true },
privateGroup: { type: Boolean, required: true },
Expand All @@ -19,6 +21,7 @@ const GroupSchema = new Schema<IGroup>({
created: { type: Date, required: true, default: Date.now },
});

// Create a model for the group schema, using the existing "group" model if it exists, otherwise creating a new one.
const Group =
mongoose.models["groups"] || mongoose.model("groups", GroupSchema);

Expand Down
10 changes: 6 additions & 4 deletions backend/models/itemSchema.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import mongoose, { Schema } from "mongoose";

// Defining the type for an item object, specifying the structure of the item data.
export type IItem = {
_id: Schema.Types.ObjectId;
name: string;
toShare: boolean;
isPrivate: boolean;
type: string;
basket: Schema.Types.ObjectId;
basket: Schema.Types.ObjectId; // ObjectId referencing the basket to which the item belongs.
notes: string;
price: number;
quantity: number;
created: Date;
lastModified: Date;
created: Date; // Timestamp for when the item was created.
lastModified: Date; // Timestamp for when the item was last modified.
};

// Mongoose schema
// Defining the data types and requirements for each field in our item schema.
const itemSchema = new Schema({
name: { type: String, required: true },
toShare: { type: Boolean, required: true },
Expand All @@ -28,6 +29,7 @@ const itemSchema = new Schema({
lastModified: { type: Date, required: true, default: Date.now },
});

// Create a model for the item schema, using the existing "items" model if it exists, otherwise creating a new one.
const Event = mongoose.models["items"] || mongoose.model("items", itemSchema);

export default Event;
6 changes: 3 additions & 3 deletions backend/models/userSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mongoose, { Schema } from "mongoose";

// Defining the type for a user object, specifying the structure of the user data.
export type IUser = {
_id: Schema.Types.ObjectId;
username: string;
Expand All @@ -13,9 +14,7 @@ export type IUser = {
joined: Date;
};

//groupId and digitalWaiver seem to require a schema
//currently there is no schema for them so I am leaving them as null for now
//can groupId just be a string and digitalWaiver be a boolean?
// Defining the data types and requirements for each field in our user schema.
const UserSchema = new Schema<IUser>({
username: { type: String, required: true },
email: { type: String, required: true },
Expand All @@ -28,6 +27,7 @@ const UserSchema = new Schema<IUser>({
joined: { type: Date, required: true, default: Date.now },
});

// Create a model for the user schema, using the existing "users" model if it exists, otherwise creating a new one.
const User = mongoose.models["users"] || mongoose.model("users", UserSchema);

export default User;
15 changes: 8 additions & 7 deletions backend/routes/basketRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { authenticateUser } from "../auth.js";

const router = express.Router();

// Route to get all baskets
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
Expand All @@ -20,6 +21,7 @@ router.get("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to get a specific basket by ID or name
router.get(
"/:basketid",
authenticateUser,
Expand Down Expand Up @@ -56,23 +58,19 @@ router.get(
},
);

// Route to create a new basket
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new basket with data:", req.body);
//Create new basket to add
// Create new basket to add
const { basketName, description, members, items } = req.body;
if (!basketName || !description) {
console.error("Missing required fields", req.body);
return res.status(400).send("Missing required fields");
}

const basketToAdd = new Basket({
basketName,
description,
members,
items,
});
const basketToAdd = new Basket({basketName, description, members, items,});

const newBasket = await basketToAdd.save();
console.log("New basket created:", newBasket);
Expand All @@ -83,6 +81,7 @@ router.post("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to update a basket by ID
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get basket ID from URL
const { id } = req.params;
Expand All @@ -106,6 +105,7 @@ router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to remove an item from a basket
router.patch(
"/:bid/removeitem",
authenticateUser,
Expand Down Expand Up @@ -135,6 +135,7 @@ router.patch(
},
);

// Route to delete a basket by ID
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand Down
25 changes: 16 additions & 9 deletions backend/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import connectDB from "../connection.js";

const router = express.Router();

// Route to get all groups
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
Expand All @@ -20,6 +21,7 @@ router.get("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to get a specific group by ID or name
router.get(
"/:groupid",
authenticateUser,
Expand Down Expand Up @@ -61,26 +63,29 @@ router.get(
},
);

// Route to create a new group
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
console.log("Creating a new group with data:", req.body);
//Create new group to add
const { groupName, privateGroup, description, members, baskets } = req.body;
//*assuming groupname and privateGroup is required fields need to add a default description ("No description given") etc.
//*ALSO do we want the baskets to be a list of baskets or just one basket (what we have) something to think
//about because arent there going to be multiple baskets per group
const { groupName,
privateGroup,
description,
members,
baskets
} = req.body;
if (!groupName || privateGroup == null || !description) {
console.error("Missing required fields", req.body);
return res.status(400).send("Missing required fields");
}

const GroupToAdd = new Group({
groupName,
privateGroup,
description,
members,
baskets,
groupName,
privateGroup,
description,
members,
baskets
});

const newGroup = await GroupToAdd.save();
Expand All @@ -92,6 +97,7 @@ router.post("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to update a group by ID
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
Expand All @@ -115,6 +121,7 @@ router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to delete a group by ID
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand Down
5 changes: 5 additions & 0 deletions backend/routes/itemRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import connectDB from "../connection.js";

const router = express.Router();

// Route to get all items
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
Expand All @@ -20,6 +21,7 @@ router.get("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to get a specific item by ID or name
router.get(
"/:itemid",
authenticateUser,
Expand Down Expand Up @@ -57,6 +59,7 @@ router.get(
},
);

// Route to create a new item
router.post("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();
try {
Expand Down Expand Up @@ -95,6 +98,7 @@ router.post("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to update an item by ID
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
Expand All @@ -118,6 +122,7 @@ router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to delete an item by ID
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand Down
11 changes: 9 additions & 2 deletions backend/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bcrypt from "bcrypt";
import mongoose from "mongoose";
const router = express.Router();

// Route to get all users
router.get("/", authenticateUser, async (req: Request, res: Response) => {
connectDB();

Expand All @@ -22,6 +23,7 @@ router.get("/", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to get a specific user by ID
router.get(
"/:userid",
authenticateUser,
Expand All @@ -33,7 +35,7 @@ router.get(
// Use findById correctly with the id parameter from the request
const user = await User.findById(req.params.userid);

// Check if group is null or undefined
// Check if user is null or undefined
if (!user) {
return res.status(404).send("No users found"); // Use return to exit the function after sending the response
}
Expand All @@ -46,7 +48,7 @@ router.get(
// Use findById correctly with the id parameter from the request
const user = await User.findOne({ username: req.params.userid });

// Check if group is null or undefined
// Check if user is null or undefined
if (!user) {
return res.status(404).send("No users found"); // Use return to exit the function after sending the response
}
Expand All @@ -62,6 +64,7 @@ router.get(
},
);

// Route to get a specific user by username
router.get(
"/username/:username",
authenticateUser,
Expand All @@ -82,6 +85,7 @@ router.get(
},
);

// Route to register a new user
router.post("/", async (req: Request, res: Response) => {
connectDB();
let { username, email, password, firstName, lastName } = req.body;
Expand Down Expand Up @@ -132,6 +136,7 @@ router.post("/", async (req: Request, res: Response) => {
}
});

// Route to update a user by ID
router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
// Get user ID from URL
Expand All @@ -155,6 +160,7 @@ router.patch("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to delete a user by ID
router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand All @@ -173,6 +179,7 @@ router.delete("/:id", authenticateUser, async (req: Request, res: Response) => {
}
});

// Route to remove a friend from user's friends list
router.delete(
"/:id/remove-friend",
authenticateUser,
Expand Down
Loading

0 comments on commit b32be95

Please sign in to comment.