nerack repo
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,3 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}})
|
||||
returning id, title, finished;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
finished INTEGER CHECK(finished IN (1))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_user_id ON todos(user_id);
|
||||
@@ -0,0 +1,4 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id;
|
||||
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,25 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
{{$head}}
|
||||
{{/head}}
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='{{url:get:login}}'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='{{url:get:home}}'>home</a>
|
||||
<a href='{{url:get:about}}'>about us</a>
|
||||
<a href='{{url:get:contact}}'>contact us</a>
|
||||
<a href='{{url:get:todos}}'>todos</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
#include <datastar.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(todo){
|
||||
sqlite(
|
||||
"todos_db",
|
||||
"file:todo.db?mode=rwc",
|
||||
{"create_todos_table"}
|
||||
);
|
||||
|
||||
http("home", "/",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("about", "/about",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("about", "about_s"),
|
||||
http_response("about_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("contact", "/contact",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("contact", "contact_s"),
|
||||
http_response("contact_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("todos", "/todos",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session()
|
||||
},
|
||||
.sse = {"todos:{{user_id}}"},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
html("todos", "todos_s"),
|
||||
http_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todos", .mode = prepend_mode, .elements = "todo_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("todo", "/todos/:id",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = replace_mode, .elements = "todo_s")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo", .error_on_empty = true}),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = remove_mode)
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#todo_data}}
|
||||
{{> todo_item}}
|
||||
{{/todo_data}}
|
||||
@@ -0,0 +1,10 @@
|
||||
<div id="todo_{{id}}">
|
||||
{{^finished}}
|
||||
<input type="checkbox" data-on:click__prevent="@patch('{{url:patch:todo:finished=1}}')">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:patch:todo}}')">
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button data-on:click="@delete('{{url:delete:todo}}')">delete</button>
|
||||
</div>
|
||||
@@ -0,0 +1,16 @@
|
||||
{{< layout}}
|
||||
{{$head}}
|
||||
{{> datastar }}
|
||||
{{/head}}
|
||||
{{$body}}
|
||||
<input type="text" placeholder="new todo" data-bind:title
|
||||
data-on:keydown="evt.key === 'Enter' && @post('{{url:post:todos}}') && ($title = '');"
|
||||
>
|
||||
<button data-on:click="@post('{{url:post:todos}}') && ($title = '')">add</button>
|
||||
<div id="todos" data-init="@get('{{url:events:todos}}')">
|
||||
{{#todos_data}}
|
||||
{{> todo_item}}
|
||||
{{/todos_data}}
|
||||
</div>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id, title, finished;
|
||||
Reference in New Issue
Block a user