// CLARA Projects: authenticated, revision-safe application routes. route GET "/api/tasks" auth -> tasks; route GET "/api/tasks/:id" auth -> task; route PUT "/api/tasks/:id" auth -> saveTask; route GET "/api/brief/:id" auth -> brief; route GET "/api/score/:input" -> score; template "brief" = "Project brief

{{title}}

{{description}}

Owner: {{owner}}

Status: {{status}}

Due: {{due}}

"; fn digit(c: String): Bool { return c == "0" || c == "1" || c == "2" || c == "3" || c == "4" || c == "5" || c == "6" || c == "7" || c == "8" || c == "9"; } fn validId(id: String): Bool { if len(id) != 32 { return false; } mut i: Int = 0; while i < 32 { let c: String = id[i]; if !(digit(c) || (c == "a" || c == "b" || c == "c" || c == "d" || c == "e" || c == "f")) { return false; } i = i + 1; } return true; } fn validDate(d: String): Bool { if d == "" { return true; } if len(d) != 10 || d[4] != "-" || d[7] != "-" { return false; } mut i: Int = 0; while i < 10 { if i != 4 && i != 7 { if !digit(d[i]) { return false; } } i = i + 1; } let year: Int = int(d[0]+d[1]+d[2]+d[3]); let month: Int = int(d[5]+d[6]); let day: Int = int(d[8]+d[9]); if month < 1 || month > 12 || day < 1 || year < 1 { return false; } mut maxDay: Int = 31; if month == 4 || month == 6 || month == 9 || month == 11 { maxDay = 30; } if month == 2 { maxDay = 28; if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) { maxDay = 29; } } return day <= maxDay; } fn priorityScore(x: Int): Int { return x * x + 2 * x; } fn score(req: Request): Response { let x: Int = int(path(req,"input","0")); require(x >= 0 && x <= 50, "Input must be 0..50"); return json(200,{input:x,score:priorityScore(x)}); } fn tasks(req: Request): Response { let items: Array = storeList("desk/"); return json(200,{items:items,count:len(items)}); } fn task(req: Request): Response { let id: String = path(req,"id",""); require(validId(id),"Invalid identifier"); let item: Object = storeGetVersion("desk/"+id); return json(200,item); } fn saveTask(req: Request): Response { let id: String = path(req,"id",""); let revision: Int = bodyInt(req,"revision",-1); let title: String = bodyString(req,"title",""); let description: String = bodyString(req,"description",""); let owner: String = bodyString(req,"owner",""); let due: String = bodyString(req,"due",""); let status: String = bodyString(req,"status","todo"); let priority: Int = bodyInt(req,"priority",2); let effort: Int = bodyInt(req,"effort",1); require(validId(id) && revision >= 0,"Invalid identifier or revision"); require(len(title) > 0 && len(title) <= 100 && len(description) <= 1500 && len(owner) <= 60 && validDate(due),"Invalid task fields"); require(status == "todo" || status == "doing" || status == "done" || status == "archived","Invalid status"); require(priority >= 1 && priority <= 3 && effort >= 1 && effort <= 10,"Invalid priority or effort"); if revision == 0 { require(len(storeList("desk/")) < 100,"Workspace limit: 100 records including archived"); } let item: Object = {title:title,description:description,owner:owner,due:due,status:status,priority:priority,effort:effort,updatedAt:now()}; if !storeComparePut("desk/"+id,revision,item) { return json(409,{error:"Revision conflict; reload before editing"}); } return json(200,{id:id,revision:revision+1,item:item}); } fn brief(req: Request): Response { let id: String = path(req,"id",""); require(validId(id),"Invalid identifier"); let item: Any = storeGet("desk/"+id); if item == null { return json(404,{error:"Task not found"}); } return render(200,"brief",{title:item["title"],description:item["description"],owner:item["owner"],due:item["due"],status:item["status"]}); }