Skip to content

Pierluigi #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/controllers/books.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const db = require("../../db");

const getAllBooks = async (query_params) => {
let select_query = `SELECT * FROM books`;
let books;

if (query_params.id) {
select_query += ` WHERE id = $1`;
books = await db.query(select_query, [query_params.id]);
} else {
books = await db.query(select_query);
}

return books.rows;
};

const createBook = async (newBook) => {
const { title, type, author, topic, publication_date, pages } = newBook;
const result = await db.query(
"INSERT INTO books (title, type, author, topic, publication_date, pages) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
[title, type, author, topic, publication_date, pages]
);
return result.rows[0];
};

const getBookById = async (id) => {
const result = await db.query("SELECT * FROM books WHERE id = $1", [id]);
return result.rows[0];
};

const updateBook = async (id, newBook) => {
const { title, type, author, topic, publication_date, pages } = newBook;
const result = await db.query(
"UPDATE books SET title = $2, type = $3, author = $4, topic = $5, publication_date = $6, pages = $7 WHERE id = $1 RETURNING *",
[id, title, type, author, topic, publication_date, pages]
);
return result.rows[0];
};

const deleteBook = async (id) => {
const result = await db.query("DELETE FROM books WHERE id = $1 RETURNING *", [
id,
]);
return result.rows[0];
};

module.exports = {
getAllBooks,
createBook,
getBookById,
updateBook,
deleteBook,
};
38 changes: 38 additions & 0 deletions src/controllers/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const db = require("../../db");

const getAllPets = async () => {
const result = await db.query("SELECT * FROM pets");
return result.rows;
};

const createPet = async (body) => {
const { name, age, type, breed, has_microchip } = body;
const result = await db.query(
"INSERT INTO pets (name, age, type, breed, has_microchip) VALUES ($1, $2, $3, $4, $5) RETURNING *",
[name, age, type, breed, has_microchip]
);
return result.rows[0];
};

const getPetById = async (id) => {
const result = await db.query("SELECT * FROM pets WHERE id = $1", [id]);
return result.rows[0];
};

const updatePet = async (id, body) => {
const { name, age, type, breed, has_microchip } = body;
const result = await db.query(
"UPDATE pets SET name = $1, age = $2, type = $3, breed = $4, has_microchip = $5 WHERE id = $6 RETURNING *",
[name, age, type, breed, has_microchip, id]
);
return result.rows[0];
};

const deletePet = async (id) => {
const result = await db.query("DELETE FROM pets WHERE id = $1 RETURNING *", [id]);
return result.rows[0];
};



module.exports = { getAllPets,createPet,getPetById ,updatePet,deletePet};
39 changes: 33 additions & 6 deletions src/routers/books.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
const express = require('express')
const router = express.Router()
const db = require("../../db");
const express = require("express");
const router = express.Router();
const {
getAllBooks,
createBook,
getBookById,
updateBook,
deleteBook,
} = require("../controllers/books");

router.get('/', async (req, res) => {
router.get("/", async (req, res) => {
const books = await getAllBooks(req.query);
res.status(200).json({ books });
});

})
router.post("/", async (req, res) => {
const newBook = await createBook(req.body);
res.status(201).json({ book: newBook });
});

module.exports = router
router.get("/:id", async (req, res) => {
const book = await getBookById(req.params.id);
res.status(200).json({ book });
});

router.put("/:id", async (req, res) => {
const updatedBook = await updateBook(req.params.id, req.body);
res.status(201).json({ book: updatedBook });
});

router.delete("/:id", async (req, res) => {
const deletedBook = await deleteBook(req.params.id);
res.status(201).json({ book: deletedBook });
});

module.exports = router;
37 changes: 37 additions & 0 deletions src/routers/pets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const express = require("express");
const router = express.Router();
const {
getAllPets,
createPet,
getPetById,
updatePet,
deletePet,
} = require("../controllers/pets.js");

router.get("/", async (req, res) => {
const pets = await getAllPets(req.query);
res.status(200).json({ pets });
});

router.post("/", async (req, res) => {
const newPet = await createPet(req.body);
res.status(201).json({ pet : newPet });
});

router.get("/:id", async (req, res) => {
const pet = await getPetById(req.params.id);
res.status(200).json({ pet });
});

router.put("/:id", async (req, res) => {
const updatedPet = await updatePet(req.params.id, req.body);
res.status(201).json({ pet : updatedPet });
});

router.delete("/:id", async (req, res) => {
const deletedPet = await deletePet(req.params.id);
res.status(201).json({ pet : deletedPet });
});


module.exports = router;
2 changes: 2 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ app.use(express.json());

//TODO: Implement books and pets APIs using Express Modular Routers
const booksRouter = require('./routers/books.js')
const petsRouter = require('./routers/pets.js')

app.use('/books', booksRouter)
app.use('/pets', petsRouter)

module.exports = app