Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

110
LINES

< > BotCompany Repo | #1003083 // Poems Lister

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2328L/15K/51K).

!752

sclass Poem {
  long created, modified;
  long id;
  S title, text;
  
  *() {
    modified = created = now();
  }
}

static PersistentMap<Long, Poem> poems; // key = id
static long nextID = 1100000;

p {
  poems = new PersistentMap("poems");
  load("nextID");
}

synchronized html {
  if (eq(uri, "/save")) {
    S title = params.get("title");
    S text = params.get("text");
    long id = parseLong(or2(params.get("id"), "0"));
    
    new Poem p;
    p.id = id;
    p.title = unnull(title);
    p.text = unnull(text);
    id = savePoem(p);
    
    ret hrefresh(pageLink(""));
  }
  
  if (eq(uri, "/create") || eq(uri, "/edit")) {
    new Poem p;
    if (eq(uri, "/edit")) {
      long id = parseLong(params.get("id"));
      p = poems.get(id);
      if (p == null) ret "Poem " + id + " not found";
    }
    new StringBuilder buf;
    if (p.id != 0) {
      buf.append(tr(tdTop("ID") + tdTop(p.id)));
      buf.append(hhidden("id", p.id));
    }
    buf.append(tr(tdTop("Title") + tdTop(htextinput("title", unnull(p.title)))));
    buf.append(tr(tdTop("Text") + tdTop(tag("textarea", htmlencode(unnull(p.text)), "name", "text", "rows", "25", "cols", "80"))));
    buf.append(tr(tdTop("") + tdTop(hsubmit("Save poem"))));
    ret tag("form", tag("table", buf), "method", "POST", "action", pageLink("save"));
  }
  
  uri = dropPrefix("/", uri);
  if (isInteger(uri)) {
    // show a poem
    
    long id = parseLong(uri);
    Poem p = poems.get(id);
    if (p == null) ret "Poem " + id + " not found";
    S title = p.id + " - " + p.title; // TODO: set HTML title
    ret h3(htmlencode(title)) + hsourcecode(p.text)
      + p(tag("a", "Edit", "href", pageLink("edit?id=" + id)));
  }
  
  new L l;
  for (Map<S, O> map : objectToMap(values(poems))) {
    new TreeMap m;
    long id = cast map.get("id");
    for (S k : keys(map)) {
      O v = map.get(k);
      if (eq(k, "title"))
        m.put(k, tag("a", htmlencode(v), "href", pageLink(str(id))));
      else if (neq(k, "text"))
        m.put(k, htmlencode(v));
    }
    l.add(m);
  }
  ret hmobilefix()
    + htmlTable(l, false)
    + p(targetBlankLink(pageLink("create"), "Create poem"));
}

ssynchronized long saveText(S text, S title) {
  long id = findText(text);
  if (id != 0) ret id;
  new Poem p;
  p.title = title;
  p.text = text;
  ret savePoem(p);
}

static long findText(S text) {
  Poem p = find(values(poems), "text", text);
  ret p != null ? p.id : 0;
}

ssynchronized long savePoem(Poem p) {
  if (p.id == 0)
    p.id = newID();
  poems.put(p.id, p);
  ret p.id;
}

ssynchronized long newID() {
  long id = nextID;
  ++nextID;
  save("nextID");
  ret id;
}

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: 771 / 1327
Referenced in: [show references]