警告:前面已提问题。
我一直在阅读有关PATCH的实现,但是我在实际的SQL查询中找不到任何东西。
假设我在前端...
GET http://example.com/invoices/123
{
invoiceHeader : {
id: 123,
key: "2019/00001",
client: 42,
dueDate: "2019-12-25"
},
invoiceItems : [
{ id: 2001, sku: "A12345", quantity: 10, price: 300 },
{ id: 2002, sku: "B54321", quantity: 6, price: 500 },
{ id: 2003, sku: "C11223", quantity: 20, price: 200 },
]
}
现在,用户做他的事情,并进行以下更改...
{
invoiceHeader : {
id: 123,
key: "2019/00001",
client: 42,
dueDate: "2019-12-30" // Changes the date
},
invoiceItems : [
{ id: 2001, sku: "A12345", quantity: 10, price: 150 }, // Changes the price
// { id: 2002, sku: "B54321", quantity: 10, price: 500 }, // Deletes this one
{ id: 2003, sku: "C11223", quantity: 20, price: 200 },
{ id: 2004, sku: "D9999", quantity: 1, price: 7000 }, // Adds this item
]
}
数据应该放在这些表中...
invoice
+-----+------------+--------+------------+
| id | key | client | dueDate |
+-----+------------+--------+------------+
| 123 | 2019/00001 | 42 | 2019-12-25 |
+-----+------------+--------+------------+
invoiceItems
+------+--------------+--------+----------+-------+
| id | fk_invoiceId | sku | quantity | price |
+------+--------------+--------+----------+-------+
| 2001 | 123 | A12345 | 10 | 300 |
| 2002 | 123 | B54321 | 6 | 500 |
| 2003 | 123 | C11223 | 20 | 200 |
+------+--------------+--------+----------+-------+
通过此API路线...
router
.patch('/invoices/:id', function (req, res) {
let connection = mysql.createConnection(db);
connection.connect();
let query = `
UPDATE invoice SET dueDate = '2019-12-30' WHERE id = ?; -- 123
UPDATE invoiceItems SET price = 150 WHERE id = ?; -- 2001
DELETE FROM invoiceItems WHERE id = ?; -- 2002
INSERT INTO
invoiceItems (fk_invoiceId, sku, quantity, price)
VALUES (123, "D9999", 1, 7000);
`
let invoiceId = req.params.id;
connection.query(query, [invoiceId, updateProductId, deleteProductId], function (error, results, fields) {
if (error) throw error;
res.sendStatus(200);
});
connection.end();
});
HTTP PATCH请求和SQL查询是什么样的?换句话说,我如何知道为了生成多个SQL语句而进行了哪些更改?
如何为他们发送所有必要的数据,即商品ID?现在,路线中只有发票ID。我是否需要发送带有所有ID的正文?
我应该在INSERT
路由内执行DELETE
和PATCH
吗?我需要将其分为几条路线吗?
PATCH http://example.com/invoices/123
PATCH http://example.com/invoices/123/items/2001
DELETE http://example.com/invoices/123/items/2002
对此的可能答案是我的研究中的JSON PATCH,但我不确定这是否是问题。