-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
216 lines (197 loc) · 10.7 KB
/
Copy pathindex.html
File metadata and controls
216 lines (197 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Product CRUD — Spring Boot From Zero (Day 1 · TechFromZero)</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { font-family: -apple-system, "Inter", sans-serif; }
.fade-in { animation: fadeIn .35s ease-out; }
@keyframes fadeIn { from { opacity:0; transform:translateY(8px); } to { opacity:1; transform:none; } }
</style>
</head>
<body class="bg-slate-100 min-h-screen">
<header class="bg-white border-b border-slate-200 sticky top-0 z-50">
<div class="max-w-6xl mx-auto px-6 py-3 flex items-center justify-between flex-wrap gap-2">
<div>
<div class="text-xs text-emerald-600 font-bold uppercase tracking-wider">TechFromZero · Day 1</div>
<h1 class="text-xl font-bold">☕ Spring Boot — Product CRUD</h1>
</div>
<div class="flex gap-2 text-xs">
<a href="https://dev48v.infy.uk/tech/day1-spring-boot.html" class="bg-amber-100 text-amber-800 px-3 py-1.5 rounded font-semibold hover:bg-amber-200">🧠 Learn</a>
<a href="https://github.com/dev48v/springboot-from-zero" target="_blank" class="bg-slate-900 text-white px-3 py-1.5 rounded font-semibold hover:bg-slate-700">View Code</a>
<a href="https://dev48v.infy.uk/techfromzero.php" class="bg-slate-200 px-3 py-1.5 rounded font-semibold hover:bg-slate-300">← All days</a>
</div>
</div>
</header>
<main class="max-w-6xl mx-auto p-6">
<div class="text-center mb-4">
<p class="text-slate-600">Each click on this UI triggers the <b>same HTTP shape</b> the Spring Boot backend exposes.</p>
<p class="text-xs text-slate-500 mt-1">Trace panel on the right shows Controller → Service → Repository → SQL. Data persists in your browser only.</p>
</div>
<div class="grid lg:grid-cols-3 gap-4">
<div class="lg:col-span-2 bg-white rounded-2xl border border-slate-200 p-5 shadow">
<div class="flex items-center justify-between mb-3">
<h3 class="font-bold">Products</h3>
<div class="text-xs text-slate-500"><span id="count">0</span> in DB</div>
</div>
<form id="form" class="grid grid-cols-12 gap-2 mb-4 pb-4 border-b border-slate-200">
<input id="name" placeholder="Product name" class="col-span-5 border border-slate-300 rounded-lg px-3 py-2 text-sm" required>
<input id="price" type="number" step="0.01" placeholder="Price" class="col-span-3 border border-slate-300 rounded-lg px-3 py-2 text-sm" required>
<input id="stock" type="number" placeholder="Stock" class="col-span-2 border border-slate-300 rounded-lg px-3 py-2 text-sm" required>
<button id="submit" class="col-span-2 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-sm font-semibold">Save</button>
</form>
<div id="rows" class="space-y-2 max-h-[420px] overflow-auto"></div>
</div>
<div class="bg-slate-900 rounded-2xl border border-slate-800 p-5 shadow flex flex-col">
<div class="flex items-center justify-between mb-3">
<div class="text-xs font-bold uppercase text-emerald-400 tracking-widest">Network · HTTP + SQL</div>
<button id="clear" class="text-xs text-slate-500 hover:text-white">clear</button>
</div>
<div id="trace" class="space-y-3 text-xs font-mono flex-1 overflow-auto min-h-[300px]">
<div class="text-slate-500 italic">Click a button on the left — watch the request flow.</div>
</div>
</div>
</div>
<div class="mt-8 bg-white rounded-2xl border border-slate-200 p-5 shadow">
<h3 class="font-bold mb-2">📐 The 3-layer pattern</h3>
<p class="text-sm text-slate-500 mb-4">Every Spring Boot REST API on this shape. The Repository line in the trace = the exact JPA call. The SQL underneath = what Hibernate emits.</p>
<div class="flex items-center justify-between gap-3 flex-wrap">
<div class="flex-1 min-w-[120px] bg-emerald-50 border-2 border-emerald-300 rounded-xl p-3 text-center">
<div class="text-xs uppercase font-bold text-emerald-700 tracking-widest">Layer 1</div>
<div class="font-bold mt-1">Controller</div>
<div class="text-xs text-slate-500 mt-1">@RestController</div>
</div>
<div class="text-2xl text-emerald-500">→</div>
<div class="flex-1 min-w-[120px] bg-emerald-50 border-2 border-emerald-300 rounded-xl p-3 text-center">
<div class="text-xs uppercase font-bold text-emerald-700 tracking-widest">Layer 2</div>
<div class="font-bold mt-1">Service</div>
<div class="text-xs text-slate-500 mt-1">@Service</div>
</div>
<div class="text-2xl text-emerald-500">→</div>
<div class="flex-1 min-w-[120px] bg-emerald-50 border-2 border-emerald-300 rounded-xl p-3 text-center">
<div class="text-xs uppercase font-bold text-emerald-700 tracking-widest">Layer 3</div>
<div class="font-bold mt-1">Repository</div>
<div class="text-xs text-slate-500 mt-1">JpaRepository</div>
</div>
<div class="text-2xl text-emerald-500">→</div>
<div class="flex-1 min-w-[120px] bg-blue-50 border-2 border-blue-300 rounded-xl p-3 text-center">
<div class="text-xs uppercase font-bold text-blue-700 tracking-widest">Database</div>
<div class="font-bold mt-1">MySQL</div>
<div class="text-xs text-slate-500 mt-1">Hibernate ORM</div>
</div>
</div>
</div>
<footer class="text-center text-xs text-slate-500 mt-12 pb-8">
Demo runs in your browser via localStorage. The actual Spring Boot backend at
<a href="https://github.com/dev48v/springboot-from-zero" class="text-emerald-600 underline" target="_blank">github.com/dev48v/springboot-from-zero</a>
exposes the SAME endpoints. Run it locally → swap the API base URL in this UI and you get a real full-stack app.
</footer>
</main>
<script>
const LS_KEY = "tfz_d1_ui";
let nextId = 1, items = [];
try {
const saved = JSON.parse(localStorage.getItem(LS_KEY) || "null");
if (saved) { items = saved.items ?? []; nextId = saved.nextId ?? 1; }
} catch {}
if (!items.length) {
items = [
{ id: 1, name: "Widget", price: 9.99, stock: 100 },
{ id: 2, name: "Gadget", price: 24.50, stock: 35 },
{ id: 3, name: "Sprocket", price: 4.20, stock: 200 }
];
nextId = 4;
}
const rows = document.getElementById("rows");
const count = document.getElementById("count");
const trace = document.getElementById("trace");
const form = document.getElementById("form");
const nameI = document.getElementById("name");
const priceI = document.getElementById("price");
const stockI = document.getElementById("stock");
const submit = document.getElementById("submit");
let editingId = null;
const escapeHtml = s => String(s).replace(/[&<>"]/g, c => ({"&":"&","<":"<",">":">",'"':"""}[c]));
const persist = () => localStorage.setItem(LS_KEY, JSON.stringify({ items, nextId }));
const COLOR = { GET: "text-blue-300", POST: "text-emerald-300", PUT: "text-amber-300", DELETE: "text-red-300" };
function logTrace(method, path, body, sql, response) {
if (trace.firstElementChild && trace.firstElementChild.classList.contains("italic")) trace.innerHTML = "";
const block = document.createElement("div");
block.className = "bg-slate-800 border border-slate-700 rounded-lg p-3 fade-in";
block.innerHTML = `
<div class="flex items-center gap-2 mb-1">
<span class="${COLOR[method]} font-bold">${method}</span>
<span class="text-slate-200">${path}</span>
</div>
${body ? `<div class="text-slate-500 mb-1">body: <span class="text-slate-300">${escapeHtml(JSON.stringify(body))}</span></div>` : ""}
<div class="text-emerald-400 ml-2">↓ Controller.${method.toLowerCase()}()</div>
<div class="text-emerald-400 ml-6">↓ Service.${method.toLowerCase()}()</div>
<div class="text-emerald-400 ml-10">↓ Repository</div>
<div class="text-blue-400 ml-14">↓ <span class="text-blue-300">${sql}</span></div>
${response ? `<div class="text-slate-500 mt-1">response: <span class="text-emerald-200">${escapeHtml(JSON.stringify(response))}</span></div>` : ""}
`;
trace.prepend(block);
}
function render() {
count.textContent = items.length;
rows.innerHTML = items.map(p => `
<div class="flex items-center gap-3 p-3 rounded-lg border border-slate-200 hover:border-emerald-300 hover:bg-emerald-50/40 group">
<div class="w-8 h-8 bg-emerald-100 text-emerald-700 rounded font-mono text-xs font-bold flex items-center justify-center">${p.id}</div>
<div class="flex-1">
<div class="font-semibold text-sm">${escapeHtml(p.name)}</div>
<div class="text-xs text-slate-500">$${p.price.toFixed(2)} · ${p.stock} in stock</div>
</div>
<button data-edit="${p.id}" class="text-xs text-amber-700 hover:underline opacity-0 group-hover:opacity-100">Edit</button>
<button data-del="${p.id}" class="text-xs text-red-700 hover:underline opacity-0 group-hover:opacity-100">Delete</button>
</div>
`).join("");
rows.querySelectorAll("[data-del]").forEach(b => b.onclick = () => doDelete(+b.dataset.del));
rows.querySelectorAll("[data-edit]").forEach(b => b.onclick = () => startEdit(+b.dataset.edit));
}
logTrace("GET", "/api/products", null, "SELECT * FROM products", items);
form.addEventListener("submit", (e) => {
e.preventDefault();
const body = { name: nameI.value, price: +priceI.value, stock: +stockI.value };
if (editingId !== null) {
const idx = items.findIndex(p => p.id === editingId);
if (idx >= 0) {
items[idx] = { ...items[idx], ...body };
logTrace("PUT", `/api/products/${editingId}`, body,
`UPDATE products SET name='${body.name}', price=${body.price}, stock=${body.stock} WHERE id=${editingId}`,
items[idx]);
}
editingId = null;
submit.textContent = "Save";
} else {
const created = { id: nextId++, ...body };
items.push(created);
logTrace("POST", "/api/products", body,
`INSERT INTO products(name, price, stock) VALUES('${body.name}', ${body.price}, ${body.stock})`,
created);
}
persist();
render();
form.reset();
});
function startEdit(id) {
const p = items.find(x => x.id === id);
if (!p) return;
nameI.value = p.name; priceI.value = p.price; stockI.value = p.stock;
editingId = id; submit.textContent = "Update"; nameI.focus();
logTrace("GET", `/api/products/${id}`, null, `SELECT * FROM products WHERE id=${id}`, p);
}
function doDelete(id) {
items = items.filter(p => p.id !== id);
persist();
render();
logTrace("DELETE", `/api/products/${id}`, null, `DELETE FROM products WHERE id=${id}`, { status: 204 });
}
document.getElementById("clear").onclick = () => {
trace.innerHTML = `<div class="text-slate-500 italic">Click a button on the left — watch the request flow.</div>`;
};
render();
</script>
</body>
</html>