Created
December 9, 2016 19:19
-
-
Save burabure/0b83388e23e962aedff7710f2b03cdff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Todo.Server do | |
use GenServer | |
def init(_) do | |
{:ok, Todo.List.new} | |
end | |
def start do | |
GenServer.start(Todo.Server, nil) | |
end | |
def add_entry(todo_server, new_entry) do | |
GenServer.cast(todo_server, {:add_entry, new_entry}) | |
end | |
def entries(todo_server, date) do | |
GenServer.call(todo_server, {:entries, date}) | |
end | |
def update_entry(todo_server, id, updater) do | |
GenServer.cast(todo_server, {:update_entry, id, updater}) | |
end | |
def delete(todo_server, id) do | |
GenServer.cast(todo_server, {:delete, id}) | |
end | |
def handle_call({:entries, date}, _,state) do | |
{:reply, Todo.List.entries(state, date), state} | |
end | |
def handle_cast({:add_entry, new_entry}, state) do | |
{:noreply, Todo.List.add_entry(state, new_entry)} | |
end | |
def handle_cast({:update_entry, id, updater}, state) do | |
{:noreply, Todo.List.update_entry(state, id, updater)} | |
end | |
def handle_cast({:delete, id}, state) do | |
{:noreply, Todo.List.delete(state, id)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment