Skip to content

Commit 7ac3ef3

Browse files
committed
23 Apr 2023 Tutorial Update
1 parent 3970811 commit 7ac3ef3

10 files changed

+116
-123
lines changed

tutorial/assets/PAGE-items.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// (A) HELPER - CALL API
2+
function ajax (mod, act, data, after) {
3+
// (A1) FORM DATA
4+
let form = new FormData();
5+
if (data) {
6+
for (let [k,v] of Object.entries(data)) { form.append(k, v); }
7+
}
8+
9+
// (A2) AJAX FETCH
10+
fetch(`${cbhost.api}${mod}/${act}`, { method:"POST", body:form })
11+
.then(res => res.json())
12+
.then(res => {
13+
if (res.status) { after(res.data); }
14+
else { alert(res.message); }
15+
})
16+
.catch(err => console.error(err));
17+
}
18+
19+
// (B) GET ALL - DRAW HTML LIST
20+
function list () {
21+
ajax("items", "getAll", null, data => { if (data != null) {
22+
let wrap = document.getElementById("iList");
23+
wrap.innerHTML = "";
24+
for (let [i,n] of Object.entries(data)) {
25+
let row = document.createElement("li");
26+
row.className = "list-item";
27+
row.innerHTML = `<form class="d-flex mb-2" onsubmit="return update(${i});">
28+
<input type="button" class="btn btn-danger me-1" value="Del" onclick="del(${i})">
29+
<input class="form-control me-1" type="text" id="iItem${i}" value="${n}" required>
30+
<input class="btn btn-primary" type="submit" value="Save">
31+
</form>`;
32+
wrap.appendChild(row);
33+
}
34+
}});
35+
}
36+
37+
// (C) ADD ITEM
38+
function add () {
39+
ajax("items", "save", { name: document.getElementById("iAdd").value }, list);
40+
return false;
41+
}
42+
43+
// (D) UPDATE ITEM
44+
function update (id) {
45+
ajax("items", "save", { name: document.getElementById("iItem"+id).value, id: id }, list);
46+
return false;
47+
}
48+
49+
// (C) DELETE ITEM
50+
function del (id) {
51+
ajax("items", "del", { id: id }, list);
52+
}
53+
window.addEventListener("load", list);

tutorial/assets/content.js

-63
This file was deleted.

tutorial/lib/API-content.php

-17
This file was deleted.

tutorial/lib/API-items.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
// (A) API ENDPOINTS
3+
$_CORE->autoAPI([
4+
"getAll" => ["Items", "getAll"],
5+
"save" => ["Items", "save"],
6+
"del" => ["Items", "del"]
7+
]);
8+
9+
// (B) INVALID REQUEST
10+
$_CORE->respond(0, "Invalid request", null, null, 400);

tutorial/lib/LIB-Content.php

-17
This file was deleted.

tutorial/lib/LIB-Items.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
class Items extends Core {
3+
// (A) GET ALL ITEMS
4+
function getAll () {
5+
return $this->DB->fetchKV(
6+
"SELECT * FROM `items`", null, "item_id", "item_name"
7+
);
8+
}
9+
10+
// (B) SAVE ITEM
11+
function save ($name, $id=null) : void {
12+
// (B1) DATA SETUP
13+
$fields = ["item_name"];
14+
$data = [$name];
15+
if ($id!=null) { $data[] = $id; }
16+
17+
// (B2) INSERT OR UPDATE
18+
if ($id==null) { $this->DB->insert("items", $fields, $data); }
19+
else { $this->DB->update("items", $fields, "`item_id`=?", $data); }
20+
}
21+
22+
// (C) DELETE ITEM
23+
function del ($id) : void {
24+
$this->DB->delete("items", "`item_id`=?", [$id]);
25+
}
26+
}

tutorial/lib/SQL-Content.sql

-10
This file was deleted.

tutorial/lib/SQL-Items.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE `items` (
2+
`item_id` bigint(20) NOT NULL AUTO_INCREMENT,
3+
`item_name` varchar(255) NOT NULL,
4+
PRIMARY KEY (`item_id`)
5+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

tutorial/pages/PAGE-content.php

-16
This file was deleted.

tutorial/pages/PAGE-items.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
// (A) PAGE META DATA & SCRIPTS
3+
$_PMETA = [
4+
"title" => "Core Boxx Tutorial Page",
5+
"load" => [
6+
["s", HOST_ASSETS . "PAGE-items.js", "defer"]
7+
//,["s", HOST_ASSETS . "more.js"]
8+
//,["c", HOST_ASSETS . "my.css"]
9+
]
10+
];
11+
12+
// (B) HTML PAGE
13+
require PATH_PAGES . "TEMPLATE-top.php"; ?>
14+
<!-- (B1) ADD ITEM -->
15+
<form class="d-flex mb-4" onsubmit="return add()">
16+
<input type="text" class="form-control me-1" required id="iAdd">
17+
<input type="submit" class="btn btn-primary" value="Add">
18+
</form>
19+
20+
<!-- (B2) LIST ITEMS -->
21+
<ul id="iList" class="list-group"></ul>
22+
<?php require PATH_PAGES . "TEMPLATE-bottom.php"; ?>

0 commit comments

Comments
 (0)