mufidu / booku

0 stars 1 forks source link

Sweep: Fix logic error in get books by author endpoint #144

Closed mufidu closed 5 months ago

mufidu commented 5 months ago

Details

We should validate the string first before attempting to find the book in the database. Fix it.

Checklist - [X] Modify `routes/book.routes.js` ✓ https://github.com/mufidu/booku/commit/fe7d4ac16a37b18920b6286a46b997d05d933f8e [Edit](https://github.com/mufidu/booku/edit/sweep/fix_logic_error_in_get_books_by_author_e/routes/book.routes.js) - [X] Running GitHub Actions for `routes/book.routes.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/fix_logic_error_in_get_books_by_author_e/routes/book.routes.js) - [X] Modify `utils/validateInput.js` ✓ https://github.com/mufidu/booku/commit/6cbd65ba977d261044a6fca7d80fdfafbdb84945 [Edit](https://github.com/mufidu/booku/edit/sweep/fix_logic_error_in_get_books_by_author_e/utils/validateInput.js) - [X] Running GitHub Actions for `utils/validateInput.js` ✓ [Edit](https://github.com/mufidu/booku/edit/sweep/fix_logic_error_in_get_books_by_author_e/utils/validateInput.js)
sweep-ai[bot] commented 5 months ago

🚀 Here's the PR! #145

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 94d479e902)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/mufidu/booku/blob/e9352e9bde84bdc3d4271acd98463ff229f9a502/routes/book.routes.js#L92-L106 https://github.com/mufidu/booku/blob/e9352e9bde84bdc3d4271acd98463ff229f9a502/utils/validateInput.js#L1-L26

Step 2: ⌨️ Coding

--- 
+++ 
@@ -90,14 +90,14 @@
     }
 });

-// Get books by author
+// Get books by author with validation
 router.get("/author/:authorName", async (req, res) => {
     const { authorName } = req.params;
+    // Validate authorName before querying the database
+    if (!validateInput('authorName', authorName)) {
+        return res.status(400).send("Invalid author name");
+    }
     const books = await Book.find({ author: new RegExp(authorName, 'i') });
-
-    if (!/^[a-zA-Z\s]+$/.test(authorName)) {
-        return res.status(404).send("Invalid author");
-    }

     if (books.length === 0) {
         return res.json([]);

Ran GitHub Actions for fe7d4ac16a37b18920b6286a46b997d05d933f8e:

--- 
+++ 
@@ -16,6 +16,10 @@
     case 'category':
       const regexCategory = /^[a-zA-Z]+$/;
       return regexCategory.test(value);
+    // Validates that the author name contains only letters and spaces.
+    case 'authorName':
+      const regexAuthorName = /^[a-zA-Z\s]+$/;
+      return regexAuthorName.test(value);
     // Placeholder for future validation types (e.g., phone number validation)
     // case 'phone':
     //   TODO: Implement phone number validation

Ran GitHub Actions for 6cbd65ba977d261044a6fca7d80fdfafbdb84945:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/fix_logic_error_in_get_books_by_author_e.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.

This is an automated message generated by Sweep AI.