Custom function (Select Statement) using Express-Validator using Knex.js and SQLITE

Viewed 120

first of all I'm a javascript beginner and I'm just trying to teach myself the different components.

I have written an API using Node.js, Express, SQLITE and Knex. Now I want to run a check with the help of express validator.

The standard checks like isLength, isFloat, isNumeric, etc. have already worked. However, now I want to check if an entry I want to write to a table exists in another table.

My database is set up as follows: I have a table with items (article_nr, article_title, article_description, etc.) and a table with bids (article_nr, bid_amount,...). It should only be possible to place a bid for an item (in the table with bids) if the item exists in the table of items.

I have already tried to check the data with the help of this code (Check if email is in database in custom express-validator. (node, express, mysql)). Unfortunately, this did not work.

I would like to use a select statement to check if there is a record in the other table for the bid I want to create.

Here is a snippet of my code.

Articles.js

const knex = require("./knex");

/** Funktion, welche ein Gebot für einen Artikel in die Datenbank schreibt */
function createBid(bid_amount_, fk_article_nr_) {
  return knex("Tbl_bid").insert({
    bid_amount: bid_amount_,
    fk_article_nr: fk_article_nr_,
  });
}

module.exports = {
  createBid,
};

Server.js

const express = require("express");
const app = express();
const db = require("./db/articles");
const { check, validationResult } = require("express-validator");
const isBase64 = require("is-base64");
const port = 8000;

app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", ["http://localhost:3000"]);
  res.header(
    "Access-Control-Allow-Methods",
    "GET, HEAD, OPTIONS, DELETE, POST, PUT, PATCH"
  );
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  next();
});

app.post(
  "/api/articles/bids/:id",
  [
    check("bid_amount", "Bitte geben Sie eine Zahl als Gebot ein.").isNumeric(),
    check("bid_amount", "Bitte geben Sie eine Zahl > 0 als Gebot ein.").isFloat({ min: 0.01 }),
    check("fk_article_nr", "Bitte geben Sie die Artikelnummer ein, für den Artikel auf den geboten werden soll.").isLength({ min: 1 }),
    check("fk_article_nr", "Die Artikelnummer muss eine Zahl sein").isNumeric(),
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    } else {
      try {
        const results = await db.createBid(
          req.body.bid_amount,
          req.body.fk_article_nr
        );
        res.status(201).json({ id: results[0] });
      } catch (error) {
        res.status(400).statusMessage("something went wrong");
      }
    }
  }
);


app.listen(`${port}`, () =>
  console.log(`Server is running on port ${port}....`)
);

Thank you for any advice.

0 Answers
Related