backend repository for dual repo web dev final
https://github.com/AF-Foysal/web-dev-final-frontend
Database:
- Models that are associated:
- Model 1 (Book):
web-dev-final-backend/database/models/Book.js
Lines 5 to 31 in 803c29a
const Book = db.define("book", { isbn: { type: Sequelize.STRING, allowNull: false, primaryKey: true, }, title: { type: Sequelize.STRING, }, authors: { type: Sequelize.STRING, }, publication_date: { type: Sequelize.STRING, }, description: { type: Sequelize.STRING, }, page_count: { type: Sequelize.INTEGER, }, genre: { type: Sequelize.STRING, }, }); module.exports = Book; - Model 2 (Thread):
web-dev-final-backend/database/models/Thread.js
Lines 2 to 28 in 803c29a
const Sequelize = require("sequelize"); const db = require("../db"); //id BIGSERIAL NOT NULL PRIMARY KEY const Thread = db.define("thread", { isbn_num: { type: Sequelize.STRING, allowNull: false, required: true, foreignKey: true, }, name: { type: Sequelize.STRING, required: true, }, review: { type: Sequelize.STRING, required: true, }, rating: { type: Sequelize.INTEGER, }, }); module.exports = Thread;
- Model 1 (Book):
- API (Express, Sequelize, CRUD ops)
- CRUD OPS with associated OPS:
web-dev-final-backend/routes/books.js
Lines 1 to 46 in 803c29a
const express = require("express"); const router = express.Router(); const db = require("../database/db"); const Book = require("../database/models/Book"); // retrieves book list router.get("/findall", async (req, res) => { Book.findAll() .then((books) => { res.send(books); }) .catch((err) => console.log(err)); }); // add book to database router.post("/createbook", function (req, res, next) { Book.create(req.body) .then((createdBook) => res.status(200).json(createdBook)) .catch((err) => next(err)); }); // get book by isbn router.get("/:isbn", async (req, res) => { Book.findByPk(req.params.isbn) .then((query_book) => res.status(200).json(query_book)) .catch((err) => console.log(err)); }); // delete book by isbn router.delete("/:isbn", function (req, res, next) { Book.destroy({ where: { isbn: req.params.isbn, }, }) .then(() => res.status(200).json("Deleted book.")) .catch((err) => next(err)); }); // update router.put("/:isbn", async (req, res) => { await Book.update(req.body, { where: { isbn: req.params.isbn } }); let book = await Book.findByPk(req.params.isbn); res.status(200).json(book); }); module.exports = router; - SECOND INSTANCE FOR SECOND MODEL:
web-dev-final-backend/routes/threads.js
Lines 1 to 56 in 803c29a
const express = require("express"); const router = express.Router(); const db = require("../database/db"); const Thread = require("../database/models/Thread"); router.get("/findthreads", async (req, res) => { Thread.findAll() .then((threads) => { res.send(threads); }) .catch((err) => console.log(err)); }); router.post("/create", function (req, res, next) { Thread.create(req.body) .then((createdThread) => res.status(200).json(createdThread)) .catch((err) => next(err)); }); router.get("/:id", async (req, res) => { Thread.findByPk(req.params.id) .then((query_thread) => res.status(200).json(query_thread)) .catch((err) => console.log(err)); }); router.get("/isbn/:isbn", async (req, res) => { try { Thread.findAll({ where: { isbn_num: req.params.isbn, }, }) .then((query_thread) => res.status(200).json(query_thread)) .catch((err) => next(err)); } catch (error) { console.log(error); } }); router.delete("/:id", function (req, res, next) { Thread.destroy({ where: { id: req.params.id, }, }) .then(() => res.status(200).json("Deleted thread.")) .catch((err) => next(err)); }); router.put("/:id", async (req, res) => { await Thread.update(req.body, { where: { id: req.params.id } }); let thread = await Thread.findByPk(req.params.id); res.status(200).json(thread); }); module.exports = router;
- CRUD OPS with associated OPS: