Skip to content

Commit

Permalink
fix: updates backend to match prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SilveerDusk committed May 20, 2024
1 parent 6af7672 commit 51b6e1e
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 114 deletions.
8 changes: 4 additions & 4 deletions backend/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import mongoose from "mongoose";
import dotenv from "dotenv";

dotenv.config();

Expand All @@ -12,9 +12,9 @@ let connection: typeof mongoose;
* @returns {Promise<typeof mongoose>}
*/
const connectDB = async () => {
console.log('Checking database connection...');
console.log("Checking database connection...");
if (!connection) {
console.log('Connecting to database...');
console.log("Connecting to database...");
console.log(url);
try {
connection = await mongoose.connect(url);
Expand Down
9 changes: 4 additions & 5 deletions backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { userEndpoints } from "./routes/userRoutes";
import { groupEndpoints } from "./routes/groupRoutes";
import { basketEndpoints } from "./routes/basketRoutes";
import { itemEndpoints } from "./routes/itemRoutes";
import connectDB from "./connection";

const app: Express = express();
app.use(express.json());

// Enable CORS
app.use((req: Request, res: Response, next: NextFunction) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept",
);
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE, PUT");
next();
});
Expand All @@ -34,9 +36,6 @@ app.get("/", async (req: Request, res: Response) => {
res.send(result);
});




// Error handling middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.stack);
Expand Down
27 changes: 14 additions & 13 deletions backend/models/basketSchema.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import mongoose, { Schema } from "mongoose";

export type IBasket = {
_id: Schema.Types.ObjectId;
basketName: string;
description: string;
members: Schema.Types.ObjectId[] | null;
items: Schema.Types.ObjectId[] | null;
created: Date;
_id: Schema.Types.ObjectId;
basketName: string;
description: string;
members: Schema.Types.ObjectId[] | null;
items: Schema.Types.ObjectId[] | null;
created: Date;
};

const BasketSchema = new Schema<IBasket>({
basketName: { type: String, required: true },
description: { type: String, required: true },
members: { type: [Schema.Types.ObjectId], required: true, default: [] },
items: { type: [Schema.Types.ObjectId], required: true, default: [] },
created: { type: Date, required: true, default: Date.now },
basketName: { type: String, required: true },
description: { type: String, required: true },
members: { type: [Schema.Types.ObjectId], required: true, default: [] },
items: { type: [Schema.Types.ObjectId], required: true, default: [] },
created: { type: Date, required: true, default: Date.now },
});

const Basket = mongoose.models["basket"] || mongoose.model("basket", BasketSchema);
const Basket =
mongoose.models["basket"] || mongoose.model("basket", BasketSchema);

export default Basket;
export default Basket;
29 changes: 15 additions & 14 deletions backend/models/groupSchema.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import mongoose, { Schema } from "mongoose";

export type IGroup = {
_id: Schema.Types.ObjectId;
groupName: string;
privateGroup: boolean;
description: string;
members: Schema.Types.ObjectId[] | null;
baskets: Schema.Types.ObjectId[] | null;
created: Date;
_id: Schema.Types.ObjectId;
groupName: string;
privateGroup: boolean;
description: string;
members: Schema.Types.ObjectId[] | null;
baskets: Schema.Types.ObjectId[] | null;
created: Date;
};

const GroupSchema = new Schema<IGroup>({
groupName: { type: String, required: true },
privateGroup: { type: Boolean, required: true },
description: { type: String, required: true },
members: { type: [Schema.Types.ObjectId], required: true, default: [] },
baskets: { type: [Schema.Types.ObjectId], required: true, default: [] },
created: { type: Date, required: true, default: Date.now },
groupName: { type: String, required: true },
privateGroup: { type: Boolean, required: true },
description: { type: String, required: true },
members: { type: [Schema.Types.ObjectId], required: true, default: [] },
baskets: { type: [Schema.Types.ObjectId], required: true, default: [] },
created: { type: Date, required: true, default: Date.now },
});

const Group = mongoose.models["groups"] || mongoose.model("groups", GroupSchema);
const Group =
mongoose.models["groups"] || mongoose.model("groups", GroupSchema);

export default Group;
42 changes: 21 additions & 21 deletions backend/models/itemSchema.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import mongoose, { Schema } from "mongoose";

export type IItem = {
_id: Schema.Types.ObjectId,
name: string,
toShare: boolean,
isPrivate: boolean,
type: string,
basket: Schema.Types.ObjectId,
notes: string,
price: number,
quantity: number,
created: Date,
lastModified: Date,
_id: Schema.Types.ObjectId;
name: string;
toShare: boolean;
isPrivate: boolean;
type: string;
basket: Schema.Types.ObjectId;
notes: string;
price: number;
quantity: number;
created: Date;
lastModified: Date;
};

// Mongoose schema
const itemSchema = new Schema({
name: { type: String, required: true },
toShare: { type: Boolean, required: true },
isPrivate: { type: Boolean, required: true },
type: { type: String, required: true },
basket: { type: Schema.Types.ObjectId, required: true },
notes: { type: String, required: true },
price: { type: Number, required: true },
quantity: { type: Number, required: true },
created: { type: Date, required: true, default: Date.now },
lastModified: { type: Date, required: true, default: Date.now },
name: { type: String, required: true },
toShare: { type: Boolean, required: true },
isPrivate: { type: Boolean, required: true },
type: { type: String, required: true },
basket: { type: Schema.Types.ObjectId, required: true },
notes: { type: String, required: true },
price: { type: Number, required: true },
quantity: { type: Number, required: true },
created: { type: Date, required: true, default: Date.now },
lastModified: { type: Date, required: true, default: Date.now },
});

const Event = mongoose.models["items"] || mongoose.model("items", itemSchema);
Expand Down
34 changes: 17 additions & 17 deletions backend/models/userSchema.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import mongoose, { Schema } from "mongoose";

export type IUser = {
_id: Schema.Types.ObjectId;
username: string;
email: string;
firstName: string;
lastName: string;
groups: Schema.Types.ObjectId[] | null;
friends: Schema.Types.ObjectId[] | null;
wishlist: string[] | null;
joined: Date;
_id: Schema.Types.ObjectId;
username: string;
email: string;
firstName: string;
lastName: string;
groups: Schema.Types.ObjectId[] | null;
friends: Schema.Types.ObjectId[] | null;
wishlist: string[] | null;
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?
const UserSchema = new Schema<IUser>({
username: { type: String, required: true },
email: {type: String, required: true},
firstName: {type: String, required: true},
lastName: {type: String, required: true},
groups: { type: [Schema.Types.ObjectId], required: true, default: [] },
friends: { type: [Schema.Types.ObjectId], required: true, default: [] },
wishlist: { type: [String], required: true, default: [] },
joined: { type: Date, required: true, default: Date.now }
username: { type: String, required: true },
email: { type: String, required: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
groups: { type: [Schema.Types.ObjectId], required: true, default: [] },
friends: { type: [Schema.Types.ObjectId], required: true, default: [] },
wishlist: { type: [String], required: true, default: [] },
joined: { type: Date, required: true, default: Date.now },
});

const User = mongoose.models["users"] || mongoose.model("users", UserSchema);
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.ts",
"lint": "npx eslint . --report-unused-disable-directives --max-warnings 0 && npx prettier --check ."
"lint": "npx eslint && npx prettier --check ."
},
"author": "",
"license": "ISC",
Expand Down
17 changes: 10 additions & 7 deletions backend/routes/basketRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,26 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id",async (req: Request, res: Response) => {
router.patch("/:id", async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
const { id } = req.params;
const updatedData: Partial<IBasket> = req.body; //Not a full update only partial

try {
connectDB();

const updatedBasket = await Basket.findByIdAndUpdate(id, updatedData, {new: true, runValidators: true}).lean();
if (!updatedBasket){
return res.status(404).send('Basket not found');
const updatedBasket = await Basket.findByIdAndUpdate(id, updatedData, {
new: true,
runValidators: true,
}).lean();
if (!updatedBasket) {
return res.status(404).send("Basket not found");
}

res.status(200).json(updatedBasket);
} catch (error) {
console.error('Error updating Basket: ', error);
res.status(500).send('Internal Server Error');
console.error("Error updating Basket: ", error);
res.status(500).send("Internal Server Error");
}
});

Expand Down
26 changes: 14 additions & 12 deletions backend/routes/groupRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Request, Response } from "express";
import Group, { IGroup } from "../models/groupSchema";
import connectDB from "../connection";


const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
Expand All @@ -25,9 +24,9 @@ router.post("/", async (req: Request, res: Response) => {
try {
console.log("Creating a new group with data:", req.body);
//Create new group to add
const {groupName, privateGroup, description, members, baskets} = req.body;
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
//*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
if (!groupName || privateGroup == null || !description) {
console.error("Missing required fields", req.body);
Expand All @@ -51,26 +50,29 @@ router.post("/", async (req: Request, res: Response) => {
}
});

router.patch("/:id",async (req: Request, res: Response) => {
router.patch("/:id", async (req: Request, res: Response) => {
// Get user ID from URL
const { id } = req.params;
const { id } = req.params;
const updatedData: Partial<IGroup> = req.body; //Not a full update only partial

try {
connectDB();

const updatedGroup = await Group.findByIdAndUpdate(id, updatedData, {new: true, runValidators: true}).lean();
if (!updatedGroup){
return res.status(404).send('Group not found');
const updatedGroup = await Group.findByIdAndUpdate(id, updatedData, {
new: true,
runValidators: true,
}).lean();
if (!updatedGroup) {
return res.status(404).send("Group not found");
}

res.status(200).json(updatedGroup);
} catch (error) {
console.error('Error updating group: ', error);
res.status(500).send('Internal Server Error');
console.error("Error updating group: ", error);
res.status(500).send("Internal Server Error");
}
});

router.delete("/:id", async (req: Request, res: Response) => {
connectDB();
const { id } = req.params;
Expand All @@ -88,4 +90,4 @@ router.delete("/:id", async (req: Request, res: Response) => {
}
});

export { router as groupEndpoints};
export { router as groupEndpoints };
Loading

0 comments on commit 51b6e1e

Please sign in to comment.