Skip to content

Commit 540e99a

Browse files
committed
refactor: project status
1 parent dc7db73 commit 540e99a

File tree

5 files changed

+197
-132
lines changed

5 files changed

+197
-132
lines changed

src/app/api/project/route.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,44 @@ export async function DELETE(req) {
124124
}
125125

126126
export async function PUT(req) {
127-
const { projectid, ...updates } = await req.json(); // Destructure the project JSON
128-
console.log(projectid, updates);
129-
// Connect to MongoDB
130-
await connectMongoDB();
127+
try {
128+
const { projectid, ...updates } = await req.json();
129+
console.log("Updating Project:", projectid, updates);
131130

132-
// Check if there is a valid `projectid` and update fields are not empty
133-
if (!projectid || Object.keys(updates).length === 0) {
134-
return NextResponse.json(
135-
{ message: "Invalid input, project ID and updates are required" },
136-
{ status: 400 }
131+
// Connect to MongoDB
132+
await connectMongoDB();
133+
134+
// Validate input
135+
if (!projectid || Object.keys(updates).length === 0) {
136+
return NextResponse.json(
137+
{ message: "Project ID and update fields are required" },
138+
{ status: 400 }
139+
);
140+
}
141+
142+
// Perform the update and return the updated document
143+
const updatedProject = await Project.findOneAndUpdate(
144+
{ _id: projectid },
145+
{ $set: updates },
146+
{ new: true } // Return the updated document
137147
);
138-
}
139148

140-
// Perform the update
141-
const updatedProjects = await Project.updateMany(
142-
{ projectid },
143-
{ $set: updates } // Only update fields provided in the body
144-
);
149+
if (!updatedProject) {
150+
return NextResponse.json(
151+
{ message: "Project not found" },
152+
{ status: 404 }
153+
);
154+
}
145155

146-
return NextResponse.json(
147-
{ message: "Successfully updated requests", updatedProjects },
148-
{ status: 200 }
149-
);
156+
return NextResponse.json(
157+
{ message: "Successfully updated request", updatedProject },
158+
{ status: 200 }
159+
);
160+
} catch (error) {
161+
console.error("Error updating project:", error);
162+
return NextResponse.json(
163+
{ message: "Internal Server Error", error: error.message },
164+
{ status: 500 }
165+
);
166+
}
150167
}

src/app/api/request/route.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function GET(req) {
77
await connectMongoDB();
88

99
const url = new URL(req.url);
10-
const projectid = url.searchParams.get("projectid");
10+
const projectid = url.searchParams.get("projectId");
1111

1212
const query = projectid ? { projectid } : {};
1313

@@ -46,27 +46,44 @@ export async function POST(req) {
4646
}
4747

4848
export async function PUT(req) {
49-
const { projectid, ...updates } = await req.json(); // Destructure the request JSON
49+
try {
50+
const { projectid, ...updates } = await req.json();
51+
console.log("Updating Project:", projectid, updates);
5052

51-
// Connect to MongoDB
52-
await connectMongoDB();
53+
// Connect to MongoDB
54+
await connectMongoDB();
5355

54-
// Check if there is a valid `projectid` and update fields are not empty
55-
if (!projectid || Object.keys(updates).length === 0) {
56-
return NextResponse.json(
57-
{ message: "Invalid input, project ID and updates are required" },
58-
{ status: 400 }
56+
// Validate input
57+
if (!projectid || Object.keys(updates).length === 0) {
58+
return NextResponse.json(
59+
{ message: "Project ID and update fields are required" },
60+
{ status: 400 }
61+
);
62+
}
63+
64+
// Perform the update and return the updated document
65+
const updatedProject = await Request.findOneAndUpdate(
66+
{ projectid },
67+
{ $set: updates },
68+
{ new: true } // Return the updated document
5969
);
60-
}
6170

62-
// Perform the update
63-
const updatedRequests = await Request.updateMany(
64-
{ projectid },
65-
{ $set: updates } // Only update fields provided in the body
66-
);
71+
if (!updatedProject) {
72+
return NextResponse.json(
73+
{ message: "Project not found" },
74+
{ status: 404 }
75+
);
76+
}
6777

68-
return NextResponse.json(
69-
{ message: "Successfully updated requests", updatedRequests },
70-
{ status: 200 }
71-
);
78+
return NextResponse.json(
79+
{ message: "Successfully updated request", updatedProject },
80+
{ status: 200 }
81+
);
82+
} catch (error) {
83+
console.error("Error updating project:", error);
84+
return NextResponse.json(
85+
{ message: "Internal Server Error", error: error.message },
86+
{ status: 500 }
87+
);
88+
}
7289
}

src/app/projectops/[projectId]/page.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import { useRouter } from "next/navigation";
1313
export default function Projectdetails({ params }) {
1414
const resolvedParams = use(params);
1515
const projectId = resolvedParams.projectId;
16-
const requestId = resolvedParams.projectId;
1716
console.log("projectId", projectId);
18-
console.log("requestId", requestId);
1917
const router = useRouter();
2018

2119
const [projectDetails, setProjectDetails] = useState({
@@ -67,8 +65,7 @@ export default function Projectdetails({ params }) {
6765

6866
const fetchResource = async () => {
6967
try {
70-
71-
const res = await fetch(`/api/resource/?requestId=${requestId}`, {
68+
const res = await fetch(`/api/resource/?projectRequest=${projectId}`, {
7269
method: "GET",
7370
headers: { "Content-Type": "application/json" },
7471
});
@@ -109,16 +106,16 @@ export default function Projectdetails({ params }) {
109106
}, [projectId]);
110107

111108
useEffect(() => {
112-
if (requestId) {
109+
if (projectId) {
113110
fetchResource();
114111
}
115-
}, [requestId]);
112+
}, [projectId]);
116113

117114
const handleDelete = async () => {
118115
toast.success("Resource deleted successfully");
119116

120117
try {
121-
const response = await fetch(`/api/resource/?requestId=${requestId}`, {
118+
const response = await fetch(`/api/resource/?requestId=${projectId}`, {
122119
method: "DELETE",
123120
headers: {
124121
"Content-Type": "application/json",
@@ -140,7 +137,9 @@ export default function Projectdetails({ params }) {
140137
<div>
141138
{/* Details Box */}
142139
<div className="flex flex-row items-center">
143-
<h1 className="text-5xl font-bold mx-16 my-5">{projectDetails.projectName}</h1>
140+
<h1 className="text-5xl font-bold mx-16 my-5">
141+
{projectDetails.projectName}
142+
</h1>
144143
{/* <span
145144
className={`rounded-2xl px-6 py-1 mt-3 ml-8 ${
146145
selectedButton === "Approved"
@@ -167,19 +166,25 @@ export default function Projectdetails({ params }) {
167166
<p className="text-xl font-medium ml-5 -16 mt-5">
168167
Application name
169168
</p>
170-
<p className="text-lg font-normal ml-5 mt-2">{projectDetails.projectName}</p>
169+
<p className="text-lg font-normal ml-5 mt-2">
170+
{projectDetails.projectName}
171+
</p>
171172
</div>
172173
<div>
173174
<p className="text-xl font-medium mx-16 mt-5">Description</p>
174-
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.projectDescription}</p>
175+
<p className="text-lg font-normal ml-16 mt-2">
176+
{projectDetails.projectDescription}
177+
</p>
175178
</div>
176179
{/* <div>
177180
<p className="text-xl font-medium mx-16 mt-5">Last Update</p>
178181
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.lastUpdate}</p>
179182
</div> */}
180183
<div>
181184
<p className="text-xl font-medium mx-16 mt-5">Create By</p>
182-
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.pathWithNamespace}</p>
185+
<p className="text-lg font-normal ml-16 mt-2">
186+
{projectDetails.pathWithNamespace}
187+
</p>
183188
</div>
184189
</div>
185190
</div>
@@ -209,7 +214,9 @@ export default function Projectdetails({ params }) {
209214
<div className="flex flex-col space-y-4 mt-4">
210215
<div className="flex flex-row">
211216
<p className="text-lg font-semibold w-32">Name</p>
212-
<p className="text-lg font-light items-center">{resource.resourceName}</p>
217+
<p className="text-lg font-light items-center">
218+
{resource.resourceName}
219+
</p>
213220
</div>
214221

215222
<div className="flex flex-row items-center">

0 commit comments

Comments
 (0)