feat: add database

main
Moritz Böhme 2024-08-07 00:09:00 +02:00
parent 8355f30948
commit a408a38ffb
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
3 changed files with 97 additions and 47 deletions

View File

@ -3,6 +3,7 @@ defmodule Todo.Cache do
@impl GenServer
def init(_init_args) do
Todo.Database.start()
{:ok, %{}}
end
@ -13,7 +14,7 @@ defmodule Todo.Cache do
{:reply, process, state}
:error ->
new_process = Todo.Server.start()
{:ok, new_process} = Todo.Server.start(name)
new_state = Map.put(state, name, new_process)

51
lib/todo/database.ex Normal file
View File

@ -0,0 +1,51 @@
defmodule Todo.Database do
use GenServer
@db_folder "./persist"
def start do
GenServer.start(__MODULE__, nil, name: __MODULE__)
end
def store(key, data) do
GenServer.cast(__MODULE__, {:store, key, data})
end
def get(key) do
GenServer.call(__MODULE__, {:get, key})
end
@impl GenServer
def init(_) do
File.mkdir_p!(@db_folder)
{:ok, nil}
end
@impl GenServer
def handle_cast({:store, key, data}, state) do
key
|> file_name()
|> File.write!(:erlang.term_to_binary(data))
{:noreply, state}
end
@impl GenServer
def handle_call({:get, key}, _, state) do
data =
case File.read(file_name(key)) do
{:ok, contents} ->
:erlang.binary_to_term(contents)
_ ->
nil
end
{:reply, data, state}
end
def file_name(key) do
Path.join(@db_folder, to_string(key))
end
end

View File

@ -1,52 +1,8 @@
defmodule Todo.Server do
use GenServer
@impl GenServer
def init(%_{} = todo_list) do
{:ok, todo_list}
end
@impl GenServer
def init(entries) do
{:ok, Todo.List.new(entries)}
end
@impl GenServer
def handle_call({:entries, date}, _from, todo_list) do
entries = Todo.List.entries(todo_list, date)
{:reply, entries, todo_list}
end
@impl GenServer
def handle_cast({:add, entry}, todo_list) do
new_todo_list = Todo.List.add(todo_list, entry)
{:noreply, new_todo_list}
end
@impl GenServer
def handle_cast({:update, id, update_fun}, todo_list) do
new_todo_list = Todo.List.update(todo_list, id, update_fun)
{:noreply, new_todo_list}
end
@impl GenServer
def handle_cast({:delete, id}, todo_list) do
new_todo_list = Todo.List.delete(todo_list, id)
{:noreply, new_todo_list}
end
def start(entries \\ [])
def start(%_{} = todo_list) do
GenServer.start(__MODULE__, todo_list)
end
def start(entries) do
GenServer.start(__MODULE__, entries)
def start(name) do
GenServer.start(__MODULE__, name)
end
def add(pid, entry) do
@ -64,4 +20,46 @@ defmodule Todo.Server do
def delete(pid, id) do
GenServer.cast(pid, {:delete, id})
end
@impl GenServer
def init(name) do
{:ok, {name, nil}, {:continue, :init}}
end
@impl GenServer
def handle_continue(:init, {name, nil}) do
todo_list = Todo.Database.get(name) || Todo.List.new()
{:noreply, {name, todo_list}}
end
@impl GenServer
def handle_call({:entries, date}, _from, {name, todo_list}) do
entries = Todo.List.entries(todo_list, date)
{:reply, entries, {name, todo_list}}
end
@impl GenServer
def handle_cast({:add, entry}, {name, todo_list}) do
new_todo_list = Todo.List.add(todo_list, entry)
Todo.Database.store(name, new_todo_list)
{:noreply, {name, new_todo_list}}
end
@impl GenServer
def handle_cast({:update, id, update_fun}, {name, todo_list}) do
new_todo_list = Todo.List.update(todo_list, id, update_fun)
Todo.Database.store(name, new_todo_list)
{:noreply, {name, new_todo_list}}
end
@impl GenServer
def handle_cast({:delete, id}, {name, todo_list}) do
new_todo_list = Todo.List.delete(todo_list, id)
Todo.Database.store(name, new_todo_list)
{:noreply, {name, new_todo_list}}
end
end