ecto works now

This commit is contained in:
Moon Man 2024-08-24 09:53:33 +00:00
parent 4291f2d8fb
commit b20cd38974
4 changed files with 57 additions and 1 deletions

View File

@ -8,6 +8,7 @@ defmodule Vonbraun.Application do
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
children = [ children = [
Vonbraun.Repo,
{Bandit, scheme: :http, plug: Vonbraun.MyRouter, port: 4012} {Bandit, scheme: :http, plug: Vonbraun.MyRouter, port: 4012}
] ]

View File

@ -1,10 +1,33 @@
defmodule Vonbraun.Ecto.Schema.Pair do defmodule Vonbraun.Ecto.Schema.Pair do
use Ecto.Schema use Ecto.Schema
alias Ecto.Changeset
alias Vonbraun.Repo
@primary_key {:key, :binary_id, autogenerate: false} @primary_key {:key, :string, autogenerate: false}
schema "pairs" do schema "pairs" do
field(:value, :string) field(:value, :string)
timestamps() timestamps()
end end
def changeset(struct, params \\ %{}) do
struct
|> Changeset.cast(params, [:value])
end
def set(key, value) do
case Repo.get(__MODULE__, key) do
nil ->
changeset(%__MODULE__{key: key}, %{value: value})
|> Repo.insert()
pair ->
changeset(pair, %{value: value})
|> Repo.update()
end
end
def get(key) do
Repo.get(__MODULE__, key)
end
end end

View File

@ -14,4 +14,14 @@ defmodule Vonbraun.InboxRouter do
send_resp(conn, 404, "fuck off") send_resp(conn, 404, "fuck off")
end end
end end
post "/" do
nickname = Application.fetch_env!(:vonbraun, :nickname)
if conn.params["user"] == nickname do
send_resp(conn, 200, "ok")
else
send_resp(conn, 404, "fuck off")
end
end
end end

View File

@ -2,6 +2,7 @@ defmodule Vonbraun.MyRouter do
use Plug.Router use Plug.Router
require Logger require Logger
alias Vonbraun.Ecto.Schema.Pair
alias Vonbraun.InboxRouter alias Vonbraun.InboxRouter
plug(:match) plug(:match)
@ -224,6 +225,27 @@ defmodule Vonbraun.MyRouter do
forward("/inbox", to: InboxRouter) forward("/inbox", to: InboxRouter)
forward("/users/:user/inbox", to: InboxRouter) forward("/users/:user/inbox", to: InboxRouter)
post "/pairs/:key/:value" do
key = conn.params["key"]
value = conn.params["value"]
Pair.set(key, value)
send_resp(conn, 200, "ok")
end
get "/pairs/:key" do
key = conn.params["key"]
pair = Pair.get(key)
case pair do
nil ->
send_resp(conn, 404, "fuck off")
%{:value => value} ->
send_resp(conn, 200, value)
end
end
match _ do match _ do
send_resp(conn, 404, "fuck off") send_resp(conn, 404, "fuck off")
end end