...
 
Commits (2)
......@@ -44,7 +44,7 @@ class DatabaseHandler {
pin VARCHAR(128) NOT NULL,
password VARCHAR(128) NOT NULL,
salt VARCHAR(32) NOT NULL,
superuser TINYINT(1) NULL
superuser TINYINT(1) NULL
);
CREATE TABLE IF NOT EXISTS sales (
......@@ -66,8 +66,9 @@ class DatabaseHandler {
article_number INT AUTO_INCREMENT PRIMARY KEY,
name TEXT NOT NULL,
description MEDIUMTEXT NULL,
ean INT(13) NULL,
ean BIGINT(13) UNSIGNED NULL UNIQUE,
man TEXT NULL,
plu INT(4) NULL UNIQUE,
stock INT DEFAULT 0 NOT NULL,
price DECIMAL(13, 4) NULL,
tax INT NOT NULL,
......
......@@ -4,7 +4,11 @@ const Response = require("./Response");
const Std = require("../Std");
const DB = require("../DatabaseHandler");
const AddUser = require("./endpoints/AddUser");
const AddUser = require("./endpoints/AddUser");
const AddProduct = require("./endpoints/AddProduct");
const GetProductEAN = require("./endpoints/GetProductEAN");
const GetProducts = require("./endpoints/GetProducts");
const DeleteProduct = require("./endpoints/DeleteProduct");
class API {
constructor(app) {
......@@ -21,7 +25,11 @@ class API {
});
this.routes = {
addUser : new AddUser(this)
addUser : new AddUser(this),
addProduct : new AddProduct(this),
getProductEAN : new GetProductEAN(this),
getProducts : new GetProducts(this),
deleteProduct : new DeleteProduct(this)
};
this.server.addRoute("/api/auth", (request, response) => {
......@@ -47,4 +55,4 @@ class API {
}
}
module.exports = API;
\ No newline at end of file
module.exports = API;
......@@ -17,4 +17,4 @@ Response.ErrorCode = {
DATABASE_ERROR : 600
};
module.exports = Response;
\ No newline at end of file
module.exports = Response;
const Endpoint = require("./Endpoint");
const Server = require("../Server");
const DB = require("../../DatabaseHandler");
const Response = require("../Response");
const Std = require("../../Std");
class AddProduct extends Endpoint {
constructor(api) {
super("/api/products", api, Server.RouteType.PUT);
}
handle(request, response) {
let body = request.body;
let correct = Endpoint.HasAttributes(body, [
"name",
"description",
"ean",
"man",
"price",
"tax"
]);
if (correct !== true) {
response.send(Server.Error(
`the following field ist missing in the sent object: ${correct.toString()}`,
Response.ErrorCode.MALFORMED_REQUEST
).string());
return;
}
this.db.query(DB.Prepare(`
INSERT INTO products (name, description, ean, man, stock, price, tax)
VALUES (?, ?, ?, ?, ?, ?, ?)
`, [body.name, body.description, parseInt(body.ean), body.man, 1, body.price, body.tax]), (rows, error) => {
if (error) {
let msg;
switch (error.code) {
case "ER_DUP_ENTRY":
msg = "product already exists";
break;
default:
msg = "database error";
break;
}
response.send(Server.Error(msg, Response.ErrorCode.DATABASE_ERROR).string());
return;
}
Std.Log(`[endpoints/AddProduct] successfully added product ${body.name}`, Std.LogLevel.SUCCESS);
response.send(new Response({
msg: "product ok"
}).string());
});
}
}
module.exports = AddProduct;
const Endpoint = require("./Endpoint");
const Server = require("../Server");
const DB = require("../../DatabaseHandler");
const Response = require("../Response");
const Std = require("../../Std");
class DeleteProduct extends Endpoint {
constructor(api) {
super("/api/product/:id", api, Server.RouteType.DELETE);
}
handle(request, response) {
this.db.query(DB.Prepare(`
SELECT COUNT(*) as count FROM products WHERE article_number = ?
`, [request.params.id]), (rows, error) => {
if (error || rows.length < 1 || rows[0].count === 0) {
let msg;
if (rows.length > 0 && rows[0].count === 0)
msg = `the product with article_number '${request.params.id}' does not exist`;
else {
msg = "database error";
Std.Log(`ERROR [endpoints/GetProducts] database error: ${error}`, Std.LogLevel.WARN);
}
response.send(Server.Error(msg, Response.ErrorCode.DATABASE_ERROR).string());
return;
}
this.db.query(DB.Prepare(`
DELETE FROM products WHERE article_number = ?
`, [request.params.id]), (rows, error) => {
if (error) {
Std.Log(`ERROR [endpoints/GetProducts] database error: ${error}`, Std.LogLevel.WARN);
response.send(Server.Error(error, Response.ErrorCode.DATABASE_ERROR).string());
return;
}
response.send(new Response({}).string());
});
});
}
}
module.exports = DeleteProduct;
const Endpoint = require("./Endpoint");
const Server = require("../Server");
const DB = require("../../DatabaseHandler");
const Response = require("../Response");
const Std = require("../../Std");
class GetProductEAN extends Endpoint {
constructor(api) {
super("/api/product/ean/:ean", api);
}
handle(request, response) {
this.db.query(DB.Prepare(`
SELECT * FROM products WHERE ean = ? LIMIT 1
`, [request.params.ean]), (rows, error) => {
if (error) {
Std.Log(`ERROR [endpoints/GetProductEAN] database error: ${error}`, Std.LogLevel.WARN);
response.send(Server.Error(error).string());
return;
}
const data = rows.length > 0 ? rows[0] : {};
response.send(new Response(data).string());
});
}
}
module.exports = GetProductEAN;
const Endpoint = require("./Endpoint");
const Server = require("../Server");
const DB = require("../../DatabaseHandler");
const Response = require("../Response");
const Std = require("../../Std");
class GetProducts extends Endpoint {
constructor(api) {
super("/api/products", api);
}
handle(request, response) {
this.db.query(DB.Prepare(`
SELECT * FROM products
`, []), (rows, error) => {
if (error) {
Std.Log(`ERROR [endpoints/GetProducts] database error: ${error}`, Std.LogLevel.WARN);
response.send(Server.Error(error).string());
return;
}
response.send(new Response(rows).string());
});
}
}
module.exports = GetProducts;