Skip to content

Commit

Permalink
Refactor database connection and add validation for creating pets
Browse files Browse the repository at this point in the history
  • Loading branch information
birongliu committed Sep 7, 2024
1 parent c8394da commit e9c6df6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
7 changes: 5 additions & 2 deletions backend/database/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Using ES6 imports
import mongoose from 'mongoose';

(async () => await mongoose.connect('mongodb://localhost:27017')
(async () => await mongoose.connect(process.env.MONGODB_URI)
.then(() => console.log('Connected!')))();





10 changes: 9 additions & 1 deletion backend/database/models/petModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ import { Schema, model } from "mongoose";

const petModel = new Schema({
breed: String,
_id: String,
name: String,
type: String,
feature: String
});

const pets = model("pets", petModel);


export async function create(data) {
const result = await pets.create(data)
return result;
}

export async function findAll() {
return await pets.find()
}

export async function get(name) {
const data = await pets.findOne({
name,
})
if (!data) return null
return data;
}

export async function update(name, data) {
const pet = await get(name);
if(!pet) return null;
return await pets.updateOne({ name }, data);
}
30 changes: 25 additions & 5 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,47 @@ import 'dotenv/config'
import './database/db.js'
import './chatbot.js'
import express from 'express';
import z from "zod"
import bodyParser from 'body-parser';
import cors from 'cors';
import { findAll, get } from './database/models/petModel.js';
import { create, findAll, get } from './database/models/petModel.js';

const port = 3001;

const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cors());
app.use(cors({
origin: 'http://localhost:3000'
}));

app.get("/api/pet", async (req, res) => {

const id = req.body.id
if(!id) return res.json({ data: "invalid id provided", status: 404 })
const pet = await get(id)
res.json({ data: pet ? pet : "no ", status: pet ? 200 : 500 })
res.json({ data: pet ? pet : null, status: pet ? 200 : 404 })
})

app.get("/api/pets", async (req, res) => {
const pet = await findAll()
res.json({ data: pet, status: 200 })
})

app.listen(port, () => console.log("Listening"))
app.post("/api/pets", async (req, res) => {
const validate = z.object({
breed: z.string(),
name: z.string(),
type: z.string(),
feature: z.string()
})
console.log(req.body)
const data = await validate.safeParseAsync(req.body);
if(data.error) return res.json({ data: data.error.issues, status: 400 })
const pet = await create(req.body)
res.json({ data: pet, status: 200 })
})


app.listen(port, () => console.log(`Server is running on port ${port}`));
14 changes: 11 additions & 3 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"dependencies": {
"@google/generative-ai": "^0.17.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"fs": "^0.0.1-security",
"mongoose": "^8.6.1"
"mongoose": "^8.6.1",
"zod": "^3.23.8"
},
"name": "backend",
"version": "1.0.0",
Expand Down

0 comments on commit e9c6df6

Please sign in to comment.