Skip to content

Commit 23602c9

Browse files
authored
Added deleting the saved projects functionality (#1215)
* added-saving-project-functionality-and-auto-creation-of-collection * fixed-some-bugs * added-create-collection-manually-and-fixed-the-same * added a default-collection collection to directly save if no collection_name * added-delete-projects-functionality * fixed-a-minor-issue * extracted collection and project id from request body instead of params * updated controller- removed logs
1 parent 7fc6562 commit 23602c9

File tree

4 files changed

+3425
-3
lines changed

4 files changed

+3425
-3
lines changed

backend/Controllers/collection.controller.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Collection from "../Models/collection.model.js";
2+
import Project from "../Models/project.model.js";
23
import User from "../Models/user.model.js";
4+
import mongoose from "mongoose";
35

46
/*create a new collection-
57
1.in case of manula creation of collection, the project_id is set to null since nothing is saved */
8+
69
export const createNewCollection = async(req,res)=>{
710
try{
811
const userID = req.user;
@@ -68,7 +71,6 @@ export const saveProject = async (req, res) => {
6871
);
6972
}
7073

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

95+
export const deleteProject = async(req,res)=>{
96+
try{
97+
const userID = req.user;
98+
const {collectionID,projectID} = req.body;
99+
const existingUser = await User.findById(userID);
100+
if(!existingUser) return res.status(404).json("User not found");
101+
const deletedProject = await Collection.findOne({
102+
userID:userID,
103+
_id:collectionID,
104+
project_id:projectID
105+
})
106+
107+
if(!deletedProject) return res.status(404).json("Project not found in this collection");
108+
109+
await Collection.deleteOne(deletedProject);
110+
return res.status(200).json("Project deleted successfully");
111+
}catch(err){
112+
console.log(err);
113+
return res.status(400).json(err);
114+
}
115+
}
116+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import express from 'express';
2-
import { createNewCollection, saveProject} from '../../Controllers/collection.controller.js';
2+
import { createNewCollection, deleteProject, saveProject} from '../../Controllers/collection.controller.js';
33
import { authenticateUser } from '../../Middlewares/auth.middleware.js';
44
const collectionRoutes = express.Router();
55
collectionRoutes.post("/create-collection", authenticateUser, createNewCollection);
66
collectionRoutes.post("/:id", authenticateUser, saveProject);
7+
collectionRoutes.delete("/saved-projects", authenticateUser, deleteProject);
78

89

910
export default collectionRoutes;
10-

0 commit comments

Comments
 (0)