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
26 changes: 25 additions & 1 deletion backend/Controllers/collection.controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Collection from "../Models/collection.model.js";
import Project from "../Models/project.model.js";
import User from "../Models/user.model.js";
import mongoose from "mongoose";

/*create a new collection-
1.in case of manula creation of collection, the project_id is set to null since nothing is saved */

export const createNewCollection = async(req,res)=>{
try{
const userID = req.user;
Expand Down Expand Up @@ -68,7 +71,6 @@ export const saveProject = async (req, res) => {
);
}


// Case 3: No empty slots → create new document for the project being saved
const newDoc = new Collection(
{
Expand All @@ -90,3 +92,25 @@ export const saveProject = async (req, res) => {
}
}

export const deleteProject = async(req,res)=>{
try{
const userID = req.user;
const {collectionID,projectID} = req.body;
const existingUser = await User.findById(userID);
if(!existingUser) return res.status(404).json("User not found");
const deletedProject = await Collection.findOne({
userID:userID,
_id:collectionID,
project_id:projectID
})

if(!deletedProject) return res.status(404).json("Project not found in this collection");

await Collection.deleteOne(deletedProject);
return res.status(200).json("Project deleted successfully");
}catch(err){
console.log(err);
return res.status(400).json(err);
}
}

4 changes: 2 additions & 2 deletions backend/Routes/api/collections.routes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import express from 'express';
import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js';
import { createNewCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
const collectionRoutes = express.Router();
collectionRoutes.post("/create-collection", authenticateUser, createNewCollection);
collectionRoutes.post("/:id", authenticateUser, saveProject);
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);


export default collectionRoutes;

Loading