Skip to content
Merged
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
77 changes: 77 additions & 0 deletions backend/Controllers/collection.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Collection from "../Models/collection.model.js";
import User from "../Models/user.model.js";

//create a new collection
export const createNewCollection = async(req,res)=>{
try{
const userID = req.user;
const {collection_name}=req.body;
const existingUser = await User.findOne({_id:userID});
if(!existingUser) return res.status(404).json("User not found");

const newCollection = new Collection({
userID,
collection_name
})

await newCollection.save();
return res.status(200).json(`Collection ${newCollection.collection_name} for ${existingUser.personal_info.username}`);
}catch(err){
console.log(err);
return res.status(400).json(err);
}
}

//save projects to user profile in existing collection
export const saveProject = async (req, res) => {
try {
const userID = req.user;
const project_id = req.params.id;
const { collection_name } = req.body;

const existingUser = await User.findById(userID);
if (!existingUser) return res.status(404).json("User not found");

const existingCollection = await Collection.find({ userID, collection_name });

const existingProject = await Collection.findOne({userID,collection_name,project_id});
if(existingProject) return res.status(400).json("Project already exists in this collection");

// Case 1: No collection exists → create new one
if (existingCollection.length === 0) {
const newCollection = new Collection({ userID, collection_name, project_id });
await newCollection.save();

return res.status(201).json(
`New collection '${collection_name}' created and project saved for ${existingUser.personal_info.username}`
);
}

// Case 2: Try to update empty slot
const emptySlot = await Collection.findOneAndUpdate(
{ userID, collection_name, project_id: null },
{ $set: { project_id } },
{ new: true }
);

if (emptySlot) {
return res.status(200).json(
`Project added to empty slot in collection '${collection_name}' for ${existingUser.personal_info.username}`
);
}

// Case 3: No empty slots → create new document
const newDoc = new Collection({ userID, collection_name, project_id });
await newDoc.save();


return res.status(201).json(
`New document created in collection '${collection_name}' with project for ${existingUser.personal_info.username}`
);

} catch (err) {
console.log(err);
return res.status(400).json(err);
}
}

6 changes: 6 additions & 0 deletions backend/Models/collection.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mongoose from 'mongoose'
import collectionSchema from "../Schemas/collection.schema.js";

const Collection = mongoose.model("Collection", collectionSchema);

export default Collection;
9 changes: 9 additions & 0 deletions backend/Routes/api/collections.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express';
import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js';
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
const collection = express.Router();
collection.post("/create-collection", authenticateUser, createNewCollection);
collection.post("/:id", authenticateUser, saveProject);


export default collection;
3 changes: 3 additions & 0 deletions backend/Routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import projectRoutes from './api/project.routes.js';
import userRoutes from './api/user.routes.js';
import notificationRoutes from './api/notification.routes.js';
import subscriberRoutes from './api/subscriber.routes.js';
import collectionRoutes from './api/collections.routes.js';
import collaborationRoutes from './api/collaboration.routes.js';


const router = express.Router();

router.use('/auth', authRoutes);
Expand All @@ -15,6 +17,7 @@ router.use('/media', mediaRoutes);
router.use('/project', projectRoutes);
router.use('/notification', notificationRoutes);
router.use('/subscriber', subscriberRoutes);
router.use("/collection", collectionRoutes);
router.use('/collaboration', collaborationRoutes);

export default router;
19 changes: 19 additions & 0 deletions backend/Schemas/collection.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Schema } from "mongoose";
const collectionSchema = Schema({
userID:{
type:Schema.Types.ObjectId,
required:true,
ref:'User'
},
collection_name:{
type:String,
required:true
},
project_id:{
type:String,
default:null,
ref:'Project'
}
},{timestamps:true});

export default collectionSchema;
8 changes: 4 additions & 4 deletions backend/package-lock.json

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

8 changes: 6 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"start": "node server.js",
"dev": "nodemon server.js"
},
"keywords": [],
"author": "Avdhesh Varshney",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.1",
"cloudinary": "^2.5.1",
Expand All @@ -35,6 +38,7 @@
"nodemailer": "^7.0.5"
},
"devDependencies": {
"nodemon": "^3.1.9"
}
"nodemon": "^3.1.10"
},
"description": ""
}