Skip to content

Backend schema change#121

Open
Priyamanjare54 wants to merge 3 commits intotarinagarwal:mainfrom
Priyamanjare54:database-schema-change
Open

Backend schema change#121
Priyamanjare54 wants to merge 3 commits intotarinagarwal:mainfrom
Priyamanjare54:database-schema-change

Conversation

@Priyamanjare54
Copy link

@Priyamanjare54 Priyamanjare54 commented Jan 17, 2026

📝 Description

Implemented full Labs CRUD functionality with authentication and authorization.Fixes #58

Key highlights:

  • Added Create, Read, Update, Delete APIs for Labs
  • Secured routes using JWT-based authentication
  • Ensured only the creator can update/delete their lab
  • Fixed MongoDB connection to support Prisma transactions using a replica set
  • Verified protected endpoints using Bearer token flow

This completes the Labs module backend functionality and prepares it for frontend integration.


🔗 Related Issue

Closes: N/A


🏷️ Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 📝 Documentation update
  • 🎨 Style/UI update
  • ♻️ Code refactoring
  • ⚡ Performance improvement
  • 🧪 Test update

📸 Screenshots (if applicable)

N/A (Backend-only changes)


✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have tested my changes locally
  • Any dependent changes have been merged and published

🧪 Testing

How I tested:

  • Generated JWT token via login

  • Tested protected endpoints using Authorization header

  • Verified ownership checks for update/delete

  • Confirmed Prisma works with MongoDB replica set

  • Tested API endpoints (Postman)

  • Tested on Chrome

  • Tested on Firefox

  • Tested on mobile


📋 Additional Notes

  • MongoDB replica set was configured locally to support Prisma transactions.
  • All endpoints return correct HTTP status codes (201, 403, 404, etc.).
  • Ready for frontend consumption.

SWOC 2026 Participant
Please add the swoc2026 label to this PR 🎉

Copy link
Owner

@tarinagarwal tarinagarwal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on the Labs CRUD implementation! The core functionality is solid. A few improvements needed:

1. Use existing Prisma instance
Replace const prisma = new PrismaClient(); with import prisma from "../db.js"; to use the existing connection.

2. Add input validation

  • Validate required fields (title, language, difficulty, etc.)
  • Validate enum values for difficulty ("beginner", "intermediate", "advanced")
  • Validate visibility ("private", "public", "link")

3. Add filters to GET /api/labs
Add query parameter support for filtering by language, difficulty, creator, etc.

4. Minor fixes

  • Add newline at end of schema.prisma file
  • Add basic validation for empty/invalid ObjectIds

5. Error handling
Add validation for invalid ObjectId format in route parameters.

The MongoDB adaptations and ObjectId usage are perfect for this setup. Core CRUD logic and auth checks look excellent!

@Priyamanjare54
Copy link
Author

Hello, @tarinagarwal
I've completed the changes for Backend schema change #121
Please review when you have time. Happy to address any feedback!

@Priyamanjare54
Copy link
Author

Hello @tarinagarwal please review the changes

@tarinagarwal
Copy link
Owner

@Priyamanjare54 going thru it

@Priyamanjare54
Copy link
Author

Hello any changes required?

Copy link

@tarin-lgtm tarin-lgtm bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes Requested 🐈

This PR implements full CRUD functionality for Labs with JWT authentication and authorization. Review found critical performance issue due to missing pagination on GET /api/labs, multiple high-severity documentation gaps, and security concerns on unauthenticated GET endpoints. Additional improvements in validation, error handling, and code structure are recommended.

There are a few things I'd like to see addressed before we merge this:

Before merging

  1. Add pagination and limit to GET /api/labs endpoint to prevent performance degradation.
  2. Provide comprehensive API documentation for all Labs routes including methods, parameters, responses, authentication, and error handling.
  3. Implement authentication and authorization checks on GET endpoints and sanitize user inputs to mitigate security risks.
Findings breakdown (33 total)

1 critical / 6 high / 8 medium / 11 low / 7 info

Confidence: 95%


🔗 View Full Review Report — detailed findings, severity breakdown, and agent analysis

Reviewed by Looks Good To Meow — AI-powered code review


💬 You can interact with me directly in this PR:

  • @tarin-lgtm fix [any constraints]
  • @tarin-lgtm explain [your question]
  • @tarin-lgtm improve [focus area]
  • @tarin-lgtm test [what to focus on]

});

// --- GET ALL Labs ---
router.get("/", async (req, res) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Critical — The GET /api/labs endpoint fetches all labs without any pagination or limit, potentially returning a very large dataset which can degrade performance and increase memory usage.

Implement pagination by accepting page and limit query parameters and use Prisma's skip and take options to limit the number of labs returned per request.

performance

@@ -0,0 +1,199 @@
import express from "express";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ High — The Express router defines multiple API endpoints for labs but lacks any documentation describing the HTTP methods, paths, request body schemas, response schemas, authentication requirements, or error responses.

Add comprehensive JSDoc or API documentation comments above each route handler describing the endpoint's purpose, HTTP method, URL path, expected request parameters and body schema, response format, authentication requirements, and possible error responses.

documentation

const ALLOWED_VISIBILITY = ["private", "public", "link"];

// --- CREATE Lab ---
router.post("/", authenticateToken, async (req, res) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ High — The POST /api/labs endpoint for creating a lab is missing documentation including description, request body schema, response schema, authentication, and error responses.

Add JSDoc or API documentation for this endpoint specifying it is a POST request to create a lab, detailing required and optional fields in the request body, authentication via JWT, the structure of the success response, and possible error responses with status codes.

documentation

});

// --- GET ALL Labs ---
router.get("/", async (req, res) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ High — The GET /api/labs endpoint for fetching all labs is undocumented. It lacks details on query parameters, response schema, and error handling.

Add documentation describing this GET endpoint, including optional query parameters (language, difficulty, creator), the structure of the returned labs array, and error responses.

documentation

});

// --- GET SINGLE Lab ---
router.get("/:id", async (req, res) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ High — The GET /api/labs/:id endpoint for fetching a single lab by ID is undocumented. Missing details on path parameter, response schema, and error cases.

Add documentation specifying this GET endpoint accepts a lab ID as a path parameter, returns the lab object with related data, and possible error responses for invalid ID or not found.

documentation

return res.status(400).json({ error: "Invalid visibility value" });
}

const lab = await prisma.lab.update({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion — The PUT /:id route does not handle the case where no fields are provided for update, which may cause Prisma to update with undefined values.

Filter out undefined fields from the update data before passing to Prisma to avoid overwriting fields with undefined.

best-practices


if (existing.creatorId !== req.user.id) return res.status(403).json({ error: "Forbidden" });

await prisma.lab.delete({ where: { id } });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion — The DELETE /:id route returns a JSON message on success but does not set an explicit HTTP status code. The default 200 is acceptable but 204 No Content is more conventional for successful deletes.

Consider returning status 204 No Content with no body for successful DELETE requests to align with REST conventions.

best-practices

});

// --- UPDATE Lab ---
router.put("/:id", authenticateToken, async (req, res) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion — The PUT /api/labs/:id endpoint updates lab fields without sanitizing or validating string inputs such as title, description, content, tasks, testCases, and solution. This could lead to stored XSS if these fields are rendered in a client without proper escaping.

Implement input sanitization or escaping for user-supplied string fields before storing them in the database. Additionally, consider validating the structure and content of complex fields like tasks and testCases.

security

visibility,
} = req.body;

if (difficulty && !ALLOWED_DIFFICULTIES.includes(difficulty)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion — In the UPDATE Lab route, the difficulty validation returns a generic error without listing allowed values, unlike the CREATE Lab route which provides allowed values for better client feedback.

Include the allowedValues array in the error response for consistency and better client-side validation feedback.

bugs

return res.status(400).json({ error: "Invalid difficulty value" });
}

if (visibility && !ALLOWED_VISIBILITY.includes(visibility)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Suggestion — In the UPDATE Lab route, the visibility validation returns a generic error without listing allowed values, unlike the CREATE Lab route which provides allowed values for better client feedback.

Include the allowedValues array in the error response for consistency and better client-side validation feedback.

bugs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI Labs (1/6): Database Schema & Basic API

2 participants