feat!: make a project using mix

This commit is contained in:
Moritz Böhme 2024-08-04 13:21:15 +02:00
parent 323d35cad2
commit 222968c495
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
11 changed files with 301 additions and 139 deletions

29
lib/todo/csv_importer.ex Normal file
View file

@ -0,0 +1,29 @@
defmodule Todo.CSVImporter do
alias Todo.List
def import(path) do
path
|> File.stream!()
|> Stream.map(&parse_line/1)
|> List.new()
end
defp parse_line(line) do
line
|> String.trim()
|> String.split(",")
|> create_entry()
end
defp create_entry([date, title]) do
final_date = parse_date(date)
%{date: final_date, title: title}
end
defp parse_date(string) do
[year, month, day] = string |> String.split("-") |> Enum.map(&String.to_integer/1)
Date.new!(year, month, day)
end
end

43
lib/todo/list.ex Normal file
View file

@ -0,0 +1,43 @@
defmodule Todo.List do
defstruct entries: %{}, next_id: 1
def new(entries \\ []) do
Enum.reduce(entries, %__MODULE__{}, &add(&2, &1))
end
def add(
%__MODULE__{entries: entries, next_id: next_id} = todo_list,
%{date: _, title: _} = entry
) do
new_entry = Map.put(entry, :id, next_id)
new_entries = Map.put(entries, next_id, new_entry)
%__MODULE__{todo_list | entries: new_entries, next_id: next_id + 1}
end
def entries(%__MODULE__{} = todo_list, date) do
todo_list.entries
|> Map.filter(fn {_, entry} -> entry.date == date end)
|> Map.values()
end
def update(%__MODULE__{entries: entries} = todo_list, id, update_fun)
when is_function(update_fun, 1) do
case Map.fetch(entries, id) do
:error ->
todo_list
{:ok, entry} ->
new_entry = update_fun.(entry)
new_entries = Map.put(entries, id, new_entry)
%__MODULE__{todo_list | entries: new_entries}
end
end
def delete(%__MODULE__{entries: entries} = todo_list, id) when is_number(id) do
new_entries = Map.delete(entries, id)
%__MODULE__{todo_list | entries: new_entries}
end
end

67
lib/todo/server.ex Normal file
View file

@ -0,0 +1,67 @@
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)
end
def add(pid, entry) do
GenServer.cast(pid, {:add, entry})
end
def entries(pid, date) do
GenServer.call(pid, {:entries, date})
end
def update(pid, id, update_fun) do
GenServer.cast(pid, {:update, id, update_fun})
end
def delete(pid, id) do
GenServer.cast(pid, {:delete, id})
end
end