-1 to null for pointers

This commit is contained in:
jusax23 2023-04-02 16:30:58 +02:00
parent 9c9e930d10
commit b00a14a9c2
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
2 changed files with 28 additions and 30 deletions

View file

@ -277,8 +277,6 @@ export const getProducts: Act = {
let defValue = d[listProducts.defValue]; let defValue = d[listProducts.defValue];
let ean = d[listProducts.ean]; let ean = d[listProducts.ean];
let parent = d[listProducts.parent]; let parent = d[listProducts.parent];
parent = parent == null ? -1 : parent;
category = category == null ? -1 : category;
return { listProdID, title, description, category, defUnit, defValue, ean, parent }; return { listProdID, title, description, category, defUnit, defValue, ean, parent };
}); });
aws("ok", out.filter(d => d != null)); aws("ok", out.filter(d => d != null));
@ -329,8 +327,6 @@ export const getProduct: Act = {
let defValue = req[0][listProducts.defValue]; let defValue = req[0][listProducts.defValue];
let ean = req[0][listProducts.ean]; let ean = req[0][listProducts.ean];
let parent = req[0][listProducts.parent]; let parent = req[0][listProducts.parent];
parent = parent == null ? -1 : parent;
category = category == null ? -1 : category;
aws("ok", { listProdID, title, description, category, defUnit, defValue, ean, parent }); aws("ok", { listProdID, title, description, category, defUnit, defValue, ean, parent });
} else { } else {
aws("error", act_error.PROD_NOT_EXISTS); aws("error", act_error.PROD_NOT_EXISTS);
@ -350,7 +346,7 @@ export const addProduct: Act = {
defUnit: "number", defUnit: "number",
defValue: "string-256", defValue: "string-256",
ean: "string-64", ean: "string-64",
parent: "number" //-1 = no parent parent: "nullpointer" //null = no parent
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) { if (!checkSelfTag(data.server)) {
@ -365,8 +361,8 @@ export const addProduct: Act = {
return; return;
} }
if (await isRoomDataFull(roomID)) return void aws("error", act_error.ROOM_DATA_LIMIT); if (await isRoomDataFull(roomID)) return void aws("error", act_error.ROOM_DATA_LIMIT);
if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS); if (data.listCatID != null && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS);
if (data.parent > -1 && !isProductInRoom(roomID, data.parent)) return void aws("error", act_error.PROD_NOT_EXISTS); if (data.parent != null && !isProductInRoom(roomID, data.parent)) return void aws("error", act_error.PROD_NOT_EXISTS);
let req = await insert( let req = await insert(
listProducts.roomID, listProducts.roomID,
listProducts.title, listProducts.title,
@ -380,11 +376,11 @@ export const addProduct: Act = {
roomID, roomID,
data.title, data.title,
data.description, data.description,
data.listCatID > -1 ? data.listCatID : null, data.listCatID != null ? data.listCatID : null,
data.defUnit, data.defUnit,
data.defValue, data.defValue,
data.ean, data.ean,
data.parent > -1 ? data.parent : null, data.parent != null ? data.parent : null,
).query(db); ).query(db);
if (req.affectedRows > 0) aws("ok", { if (req.affectedRows > 0) aws("ok", {
listProdID: Number(req.insertId) listProdID: Number(req.insertId)
@ -406,7 +402,7 @@ export const changeProduct: Act = {
defUnit: "number", defUnit: "number",
defValue: "string-256", defValue: "string-256",
ean: "string-64", ean: "string-64",
parent: "number" //-1 = no parent parent: "nullpointer" //null = no parent
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) { if (!checkSelfTag(data.server)) {
@ -421,17 +417,17 @@ export const changeProduct: Act = {
return; return;
} }
if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS); if (!isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS);
if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS); if (data.listCatID != null && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS);
if (data.parent > -1 && !isProductInRoom(roomID, data.parent)) return void aws("error", act_error.PROD_NOT_EXISTS); if (data.parent != null && !isProductInRoom(roomID, data.parent)) return void aws("error", act_error.PROD_NOT_EXISTS);
let req = await update(listProducts) let req = await update(listProducts)
.set(listProducts.title, data.title) .set(listProducts.title, data.title)
.set(listProducts.description, data.description) .set(listProducts.description, data.description)
.set(listProducts.category, data.listCatID > -1 ? data.listCatID : null) .set(listProducts.category, data.listCatID != null ? data.listCatID : null)
.set(listProducts.defUnit, data.defUnit) .set(listProducts.defUnit, data.defUnit)
.set(listProducts.defValue, data.defValue) .set(listProducts.defValue, data.defValue)
.set(listProducts.ean, data.ean) .set(listProducts.ean, data.ean)
.set(listProducts.parent, data.parent > -1 ? data.parent : null) .set(listProducts.parent, data.parent != null ? data.parent : null)
.where(and( .where(and(
eq(listProducts.listProdID, data.listProdID), eq(listProducts.listProdID, data.listProdID),
eq(listProducts.roomID, roomID), eq(listProducts.roomID, roomID),
@ -518,10 +514,10 @@ export const getItems: Act = {
state, // 0 = added; 1 = in cart/bought state, // 0 = added; 1 = in cart/bought
title, title,
description, description,
listCatID: listCatID == null ? -1 : listCatID, listCatID,
unit, unit,
value, value,
listProdID: listProdID == null ? -1 : listProdID listProdID
}; };
}); });
aws("ok", out.filter(d => d != null)); aws("ok", out.filter(d => d != null));
@ -577,10 +573,10 @@ export const getItem: Act = {
state, // 0 = added; 1 = in cart/bought state, // 0 = added; 1 = in cart/bought
title, title,
description, description,
listCatID: listCatID == null ? -1 : listCatID, listCatID,
unit, unit,
value, value,
listProdID: listProdID == null ? -1 : listProdID listProdID
}); });
} else { } else {
aws("error", act_error.ITEM_NOT_EXISTS); aws("error", act_error.ITEM_NOT_EXISTS);
@ -597,10 +593,10 @@ export const addItem: Act = {
state: "number", state: "number",
title: "string-256", title: "string-256",
description: "string-4096", description: "string-4096",
listCatID: "number", //-1 = no parent listCatID: "nullpointer", //null = no parent
unit: "number", unit: "number",
value: "string-256", value: "string-256",
listProdID: "number" //-1 = no parent listProdID: "nullpointer" //null = no parent
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) { if (!checkSelfTag(data.server)) {
@ -616,8 +612,8 @@ export const addItem: Act = {
} }
if (data.state != 1 && data.state != 0) return void aws("error", act_error.DATA); if (data.state != 1 && data.state != 0) return void aws("error", act_error.DATA);
if (await isRoomDataFull(roomID)) return void aws("error", act_error.ROOM_DATA_LIMIT); if (await isRoomDataFull(roomID)) return void aws("error", act_error.ROOM_DATA_LIMIT);
if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS); if (data.listCatID != null && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS);
if (data.listProdID > -1 && !isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS); if (data.listProdID != null && !isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS);
let req = await insert( let req = await insert(
listItems.roomID, listItems.roomID,
listItems.state, listItems.state,
@ -632,10 +628,10 @@ export const addItem: Act = {
data.state, data.state,
data.title, data.title,
data.description, data.description,
data.listCatID > -1 ? data.listCatID : null, data.listCatID != null ? data.listCatID : null,
data.unit, data.unit,
data.value, data.value,
data.listProdID > -1 ? data.parent : null, data.listProdID != null ? data.parent : null,
).query(db); ).query(db);
if (req.affectedRows > 0) aws("ok", { if (req.affectedRows > 0) aws("ok", {
listItemID: Number(req.insertId) listItemID: Number(req.insertId)
@ -654,10 +650,10 @@ export const changeItem: Act = {
state: "number", // 0 = added; 1 = in cart/bought state: "number", // 0 = added; 1 = in cart/bought
title: "string-256", title: "string-256",
description: "string-4096", description: "string-4096",
listCatID: "number", //-1 = no parent listCatID: "nullpointer", //null = no parent
unit: "number", unit: "number",
value: "string-256", value: "string-256",
listProdID: "number" //-1 = no parent listProdID: "nullpointer" //null = no parent
}, },
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
if (!checkSelfTag(data.server)) { if (!checkSelfTag(data.server)) {
@ -673,16 +669,16 @@ export const changeItem: Act = {
} }
if (data.state != 1 && data.state != 0) return void aws("error", act_error.DATA); if (data.state != 1 && data.state != 0) return void aws("error", act_error.DATA);
if (!isItemInRoom(roomID, data.listItemID)) return void aws("error", act_error.ITEM_NOT_EXISTS); if (!isItemInRoom(roomID, data.listItemID)) return void aws("error", act_error.ITEM_NOT_EXISTS);
if (data.listCatID > -1 && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS); if (data.listCatID != null && !isCategoryInRoom(roomID, data.listCatID)) return void aws("error", act_error.CAT_NOT_EXISTS);
if (data.listProdID > -1 && !isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS); if (data.listProdID != null && !isProductInRoom(roomID, data.listProdID)) return void aws("error", act_error.PROD_NOT_EXISTS);
let req = await update(listItems) let req = await update(listItems)
.set(listItems.state, data.state) .set(listItems.state, data.state)
.set(listItems.title, data.title) .set(listItems.title, data.title)
.set(listItems.description, data.description) .set(listItems.description, data.description)
.set(listItems.category, data.listCatID > -1 ? data.listCatID : null) .set(listItems.category, data.listCatID != null ? data.listCatID : null)
.set(listItems.unit, data.unit) .set(listItems.unit, data.unit)
.set(listItems.value, data.value) .set(listItems.value, data.value)
.set(listItems.link, data.listProdID > -1 ? data.listProdID : null) .set(listItems.link, data.listProdID != null ? data.listProdID : null)
.where(and( .where(and(
eq(listItems.listItemID, data.listItemID), eq(listItems.listItemID, data.listItemID),
eq(listItems.roomID, data.roomID) eq(listItems.roomID, data.roomID)

View file

@ -126,6 +126,8 @@ export function checktype(data: any, type: string) {
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (typeof data == type) { if (typeof data == type) {
return true; return true;
} else if (type == "nullpointer") {
return data == null || (typeof data == "number" && Number.isInteger(data) && data >= 0);
} else if (type == "int" && typeof data == "number" && Number.isInteger(data)) { } else if (type == "int" && typeof data == "number" && Number.isInteger(data)) {
return true; return true;
} else if (type == "email" && typeof data == "string" && re.test(data)) { } else if (type == "email" && typeof data == "string" && re.test(data)) {