Libraryless. Click here for Pure Java version (2328L/15K/51K).
1 | !752 |
2 | |
3 | sclass Poem { |
4 | long created, modified; |
5 | long id; |
6 | S title, text; |
7 | |
8 | *() { |
9 | modified = created = now(); |
10 | } |
11 | } |
12 | |
13 | static PersistentMap<Long, Poem> poems; // key = id |
14 | static long nextID = 1100000; |
15 | |
16 | p { |
17 | poems = new PersistentMap("poems"); |
18 | load("nextID"); |
19 | } |
20 | |
21 | synchronized html { |
22 | if (eq(uri, "/save")) { |
23 | S title = params.get("title"); |
24 | S text = params.get("text"); |
25 | long id = parseLong(or2(params.get("id"), "0")); |
26 | |
27 | new Poem p; |
28 | p.id = id; |
29 | p.title = unnull(title); |
30 | p.text = unnull(text); |
31 | id = savePoem(p); |
32 | |
33 | ret hrefresh(pageLink("")); |
34 | } |
35 | |
36 | if (eq(uri, "/create") || eq(uri, "/edit")) { |
37 | new Poem p; |
38 | if (eq(uri, "/edit")) { |
39 | long id = parseLong(params.get("id")); |
40 | p = poems.get(id); |
41 | if (p == null) ret "Poem " + id + " not found"; |
42 | } |
43 | new StringBuilder buf; |
44 | if (p.id != 0) { |
45 | buf.append(tr(tdTop("ID") + tdTop(p.id))); |
46 | buf.append(hhidden("id", p.id)); |
47 | } |
48 | buf.append(tr(tdTop("Title") + tdTop(htextinput("title", unnull(p.title))))); |
49 | buf.append(tr(tdTop("Text") + tdTop(tag("textarea", htmlencode(unnull(p.text)), "name", "text", "rows", "25", "cols", "80")))); |
50 | buf.append(tr(tdTop("") + tdTop(hsubmit("Save poem")))); |
51 | ret tag("form", tag("table", buf), "method", "POST", "action", pageLink("save")); |
52 | } |
53 | |
54 | uri = dropPrefix("/", uri); |
55 | if (isInteger(uri)) { |
56 | // show a poem |
57 | |
58 | long id = parseLong(uri); |
59 | Poem p = poems.get(id); |
60 | if (p == null) ret "Poem " + id + " not found"; |
61 | S title = p.id + " - " + p.title; // TODO: set HTML title |
62 | ret h3(htmlencode(title)) + hsourcecode(p.text) |
63 | + p(tag("a", "Edit", "href", pageLink("edit?id=" + id))); |
64 | } |
65 | |
66 | new L l; |
67 | for (Map<S, O> map : objectToMap(values(poems))) { |
68 | new TreeMap m; |
69 | long id = cast map.get("id"); |
70 | for (S k : keys(map)) { |
71 | O v = map.get(k); |
72 | if (eq(k, "title")) |
73 | m.put(k, tag("a", htmlencode(v), "href", pageLink(str(id)))); |
74 | else if (neq(k, "text")) |
75 | m.put(k, htmlencode(v)); |
76 | } |
77 | l.add(m); |
78 | } |
79 | ret hmobilefix() |
80 | + htmlTable(l, false) |
81 | + p(targetBlankLink(pageLink("create"), "Create poem")); |
82 | } |
83 | |
84 | ssynchronized long saveText(S text, S title) { |
85 | long id = findText(text); |
86 | if (id != 0) ret id; |
87 | new Poem p; |
88 | p.title = title; |
89 | p.text = text; |
90 | ret savePoem(p); |
91 | } |
92 | |
93 | static long findText(S text) { |
94 | Poem p = find(values(poems), "text", text); |
95 | ret p != null ? p.id : 0; |
96 | } |
97 | |
98 | ssynchronized long savePoem(Poem p) { |
99 | if (p.id == 0) |
100 | p.id = newID(); |
101 | poems.put(p.id, p); |
102 | ret p.id; |
103 | } |
104 | |
105 | ssynchronized long newID() { |
106 | long id = nextID; |
107 | ++nextID; |
108 | save("nextID"); |
109 | ret id; |
110 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003083 |
Snippet name: | Poems Lister |
Eternal ID of this version: | #1003083/1 |
Text MD5: | 3d8bd6ec7c2197f9a85f2e2d260dc0e9 |
Transpilation MD5: | 8aad2ff86fed32fcfad6d111a7de18fa |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-06-05 22:00:50 |
Source code size: | 2751 bytes / 110 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 772 / 1329 |
Referenced in: | [show references] |