Commit 620892fc authored by Michael Ochmann's avatar Michael Ochmann

added last methods for the "products" endpoint

parent d57ce4be
......@@ -9,6 +9,7 @@ const AddProduct = require("./endpoints/AddProduct");
const GetProductEAN = require("./endpoints/GetProductEAN");
const GetProducts = require("./endpoints/GetProducts");
const DeleteProduct = require("./endpoints/DeleteProduct");
const EditProduct = require("./endpoints/EditProduct");
class API {
constructor(app) {
......@@ -29,7 +30,8 @@ class API {
addProduct : new AddProduct(this),
getProductEAN : new GetProductEAN(this),
getProducts : new GetProducts(this),
deleteProduct : new DeleteProduct(this)
deleteProduct : new DeleteProduct(this),
editProduct : new EditProduct(this)
};
this.server.addRoute("/api/auth", (request, response) => {
......
......@@ -34,6 +34,7 @@ class DeleteProduct extends Endpoint {
response.send(Server.Error(error, Response.ErrorCode.DATABASE_ERROR).string());
return;
}
Std.Log(`[endpoints/DeleteProduct] product with article_number '${request.params.id}' has been deleted`, Std.LogLevel.INFO);
response.send(new Response({}).string());
});
});
......
const Endpoint = require("./Endpoint");
const Server = require("../Server");
const DB = require("../../DatabaseHandler");
const Response = require("../Response");
const Std = require("../../Std");
class EditProduct extends Endpoint {
constructor(api) {
super("/api/product/:id", api, Server.RouteType.POST);
}
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(`
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(`
UPDATE products SET name = ?, description = ?, ean = ?, man = ?, stock = ?, price = ?, tax = ?
WHERE article_number = ?
`, [body.name, body.description, parseInt(body.ean), body.man, 1, body.price, body.tax, request.params.id]), (rows, error) => {
if (error) {
let msg;
switch (error.code) {
default:
msg = "database error";
break;
}
response.send(Server.Error(msg, Response.ErrorCode.DATABASE_ERROR).string());
return;
}
Std.Log(`[endpoints/AddProduct] successfully updated product ${body.name}`, Std.LogLevel.INFO);
response.send(new Response({
msg: "product ok"
}).string());
});
});
}
}
module.exports = EditProduct;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment