Skip to content

Commit

Permalink
✨ Add Search Query API
Browse files Browse the repository at this point in the history
  • Loading branch information
saranshsinhaa committed Jun 27, 2023
1 parent a9f6e84 commit 1098b27
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const varieties = require("./routes/varietyRoutes");
const contactUs = require("./routes/contactUsRoutes");
const healthCheck = require("./routes/healthCheckRoutes");
const rfq = require("./routes/rfqRoutes");
const search = require("./routes/searchQueryRoutes");

app.use("/api/v1", user);
app.use("/api/v1", animalType);
Expand All @@ -37,6 +38,7 @@ app.use("/api/v1", varieties);
app.use("/api/v1", contactUs);
app.use("/api/v1", healthCheck);
app.use("/api/v1", rfq);
app.use("/api/v1", search);

app.use("*", (req, res) => {
res.status(404).json({
Expand Down
2 changes: 1 addition & 1 deletion backend/config/config.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PORT = 4000
MONGO_URL = mongodb+srv://vetmedman501:[email protected]/vetmedman?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true
MONGO_URL = mongodb+srv://vetmedman501:[email protected]/vetmed?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true


JWT_SECRET = secreatkeycanberandom
Expand Down
26 changes: 26 additions & 0 deletions backend/controllers/searchQueryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const SearchQuery = require("../models/searchQueryModel");
const ErrorHandler = require("../utils/errorhandler");
const catchAsyncErrors = require("../middleware/catchAsyncError");

exports.createSearchQuery = catchAsyncErrors(async (req, res, next) => {
const { query } = req.body;

if (!query) {
return next(new ErrorHandler("Query field is required", 400));
}

try {
const newSearchQuery = new SearchQuery({
query,
});

await newSearchQuery.save();

res.status(200).json({
success: true,
message: "Search query saved successfully",
});
} catch (error) {
next(error);
}
});
8 changes: 8 additions & 0 deletions backend/models/searchQueryModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const mongoose = require("mongoose");

const searchQuerySchema = new mongoose.Schema({
query: String,
timestamp: { type: Date, default: Date.now },
});

module.exports = mongoose.model("SearchQueries", searchQuerySchema);
8 changes: 8 additions & 0 deletions backend/routes/searchQueryRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require("express");
const router = express.Router();

const searchController = require("../controllers/searchQueryController");
const catchAsyncErrors = require("../middleware/catchAsyncError");
router.post("/search", catchAsyncErrors(searchController.createSearchQuery));

module.exports = router;

0 comments on commit 1098b27

Please sign in to comment.