Skip to content

Commit

Permalink
Update book query method and type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMwendwa committed Jun 5, 2023
1 parent cb7cac7 commit 58a6dad
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/pages/api/books/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getServerSession } from "next-auth/next";
import { NextApiRequest, NextApiResponse } from "next";
import { Types } from "mongoose";

import connectDB from "lib/db/connection";
import Book from "lib/db/models/book_model";
Expand Down Expand Up @@ -27,6 +28,13 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return;
}

// Chec if it is a valid id
if (!Types.ObjectId.isValid(id)) {
return res.status(404).json({
error: "Book Not Found!",
});
}

await connectDB();

// Find and delete book
Expand All @@ -51,10 +59,17 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return;
}

// Chec if it is a valid id
if (!Types.ObjectId.isValid(id)) {
return res.status(404).json({
error: "Book Not Found!",
});
}

await connectDB();

// Update book finish date
const book = await Book.findOne({ id });
const book = await Book.findById(id);
book.end = new Date();
await book.save();

Expand Down

0 comments on commit 58a6dad

Please sign in to comment.