Skip to content

AF-Foysal/web-dev-final-backend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web-dev-final-backend

backend repository for dual repo web dev final

Link to web-dev-final-frontend

https://github.com/AF-Foysal/web-dev-final-frontend

REQUIREMENTS FOR BACKEND CHECKLIST:

Database:

  • Models that are associated:
    • Model 1 (Book):
      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):
      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;
  • API (Express, Sequelize, CRUD ops)
    • CRUD OPS with associated OPS:
      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:
      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;

About

backend repository for dual repo web dev final

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%