Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records kvs #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
elixir 1.17.2-otp-25
erlang 26.2.5.3
nodejs 22.8.0
python 3.12.5
2 changes: 1 addition & 1 deletion lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Forum.Application do
def route(<<"login", _::binary>>), do: Forum.Login
def route(<<"signup", _::binary>>), do: Forum.Signup

def finish(state, ctx), do: {:ok, state, ctx}
def finish(state , ctx), do: {:ok, state, ctx}
def init(state, context) do
%{path: path} = N2O.cx(context, :req)
{:ok, state, N2O.cx(context, path: path, module: route(path))}
Expand Down
75 changes: 75 additions & 0 deletions lib/records.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule User do
require Record
require KVS

Record.defrecord(:user,[
id: :kvs.seq([], []),
next: [],
prev: [],
email: [],
password: [],
posts: "/posts/:user_id",
comments: "/comments/:user_id/:post_id",
date: {2015, 1, 1},
last_login: {2015, 1, 1}
])

def login(user, password) do
if user.password == password do
user(user, last_login: :erlang.date())
else
user
end
end

def save(id, user) do
:kvs.append(user, Enum.join(["users/", id]))
end

def find_by_id(id) do
res = :kvs.get(:writer, Enum.join(["users/", id]))
res
# case res do
# {:ok, {:writer, _, _, user , _, _}} ->
# # IO.puts("User ID: #{user}")
# user
# _ ->
# IO.puts("No user found")
# end
end

def put(user) do
:kvs.cut(Enum.join(["users/", user]), user)
end
end

defmodule Post do
require Record

Record.defrecord(:post,
id: :kvs.seq([], []),
next: [],
prev: [],
title: [],
body: [],
comments: "/comms_by_post/:post_id"
)
end

defmodule Comment do
require Record

Record.defrecord(:comment,
id: :kvs.seq([], []),
perent: [],
children: [],
body: [],
post: [],
user: []
)
end

defmodule Counter do
require Record
Record.defrecord(:counter, count: 0)
end
193 changes: 95 additions & 98 deletions priv/static/add_post.html
Original file line number Diff line number Diff line change
@@ -1,103 +1,100 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add post</title>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<head>
<title>Add post</title>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>

<!-- Bootstrap CSS v5.2.1 -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
</head>
<!-- Bootstrap CSS v5.2.1 -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous"
/>
</head>

<body>
<header>
<!-- place navbar here -->
<!-- Nav tabs -->
<ul
class="nav nav-tabs"
id="navId"
role="tablist"
>
<li class="nav-item">
<a
href="#tab1Id"
class="nav-link active"
data-bs-toggle="tab"
aria-current="page"
>Active</a
>
</li>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
data-bs-toggle="dropdown"
href="#"
role="button"
aria-haspopup="true"
aria-expanded="false"
>Dropdown</a
>
<div class="dropdown-menu">
<a class="dropdown-item" href="#tab2Id">Action</a>
<a class="dropdown-item" href="#tab3Id">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#tab4Id">Action</a>
</div>
</li>
<li class="nav-item" role="presentation">
<a href="#tab5Id" class="nav-link" data-bs-toggle="tab"
>Another link</a
>
</li>
<li class="nav-item" role="presentation">
<a href="#" class="nav-link disabled" data-bs-toggle="tab"
>Disabled</a
>
</li>
</ul>

<!-- Tab panes -->
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="tab1Id" role="tabpanel">

</div>
<div class="tab-pane fade" id="tab2Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab3Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab4Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab5Id" role="tabpanel"></div>
</div>

<!-- (Optional) - Place this js code after initializing bootstrap.min.js or bootstrap.bundle.min.js -->
<script>
var triggerEl = document.querySelector("#navId a");
bootstrap.Tab.getInstance(triggerEl).show(); // Select tab by name
</script>

</header>
<main></main>
<footer>
<!-- place footer here -->
</footer>
<!-- Bootstrap JavaScript Libraries -->
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous"
></script>
<body>
<header>
<!-- place navbar here -->
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="navId" role="tablist">
<li class="nav-item">
<a
href="#tab1Id"
class="nav-link active"
data-bs-toggle="tab"
aria-current="page"
>Active</a
>
</li>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
data-bs-toggle="dropdown"
href="#"
role="button"
aria-haspopup="true"
aria-expanded="false"
>Dropdown</a
>
<div class="dropdown-menu">
<a class="dropdown-item" href="#tab2Id">Action</a>
<a class="dropdown-item" href="#tab3Id">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#tab4Id">Action</a>
</div>
</li>
<li class="nav-item" role="presentation">
<a href="#tab5Id" class="nav-link" data-bs-toggle="tab"
>Another link</a
>
</li>
<li class="nav-item" role="presentation">
<a href="#" class="nav-link disabled" data-bs-toggle="tab"
>Disabled</a
>
</li>
</ul>

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
crossorigin="anonymous"
></script>
</body>
<!-- Tab panes -->
<div class="tab-content" id="myTabContent">
<div
class="tab-pane fade show active"
id="tab1Id"
role="tabpanel"
></div>
<div class="tab-pane fade" id="tab2Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab3Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab4Id" role="tabpanel"></div>
<div class="tab-pane fade" id="tab5Id" role="tabpanel"></div>
</div>

<!-- (Optional) - Place this js code after initializing bootstrap.min.js or bootstrap.bundle.min.js -->
<script>
var triggerEl = document.querySelector("#navId a");
bootstrap.Tab.getInstance(triggerEl).show(); // Select tab by name
</script>
</header>
<main></main>
<footer>
<!-- place footer here -->
</footer>
<!-- Bootstrap JavaScript Libraries -->
<script
src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
crossorigin="anonymous"
></script>

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
crossorigin="anonymous"
></script>
</body>
</html>