feature parity
This commit is contained in:
parent
7b87fc61ce
commit
d87be8d42a
4 changed files with 54 additions and 29 deletions
|
|
@ -1,18 +1,11 @@
|
||||||
defmodule TopPosterServer do
|
defmodule TopPosterServer do
|
||||||
@moduledoc """
|
|
||||||
Documentation for `TopPosterServer`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Hello world.
|
Checks that `discord_id` *could be* a valid discord user id
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
iex> TopPosterServer.hello()
|
|
||||||
:world
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def hello do
|
@spec is_valid_discord_id(term()) :: boolean()
|
||||||
:world
|
def is_valid_discord_id(discord_id) when is_binary(discord_id) do
|
||||||
|
String.match?(discord_id, ~r/^\d{17,20}$/)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_valid_discord_id(_), do: false
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -20,25 +20,39 @@ defmodule TopPosterServer.Plug do
|
||||||
|
|
||||||
discord_id = conn.query_params["id"]
|
discord_id = conn.query_params["id"]
|
||||||
|
|
||||||
if is_valid_discord_id(discord_id) do
|
if conn.method == "POST" and TopPosterServer.is_valid_discord_id(discord_id) do
|
||||||
conn
|
result =
|
||||||
|> send_resp(200, "valid id")
|
case Repo.get(User, discord_id) do
|
||||||
else
|
nil ->
|
||||||
Repo.insert!(%User{discord_id: "354988989100589058", posts: 413})
|
Repo.insert!(%User{id: discord_id, posts: 1}).posts
|
||||||
|
|
||||||
query = from u in User, order_by: [desc: u.posts, asc: u.discord_id]
|
_user ->
|
||||||
|
query =
|
||||||
|
from u in User,
|
||||||
|
update: [inc: [posts: 1]],
|
||||||
|
where: u.id == ^discord_id,
|
||||||
|
select: u.posts
|
||||||
|
|
||||||
|
{1, [posts]} = Repo.update_all(query, [])
|
||||||
|
posts
|
||||||
|
end
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> send_resp(200, "#{inspect(result)}\n")
|
||||||
|
else
|
||||||
|
query = from u in User, order_by: [desc: u.posts, asc: u.id]
|
||||||
users = Repo.all(query)
|
users = Repo.all(query)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> send_resp(200, "default page #{inspect(users)}")
|
|> send_resp(400, leaderboard(users))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Valid in format, not necessarily actually existing lol
|
@spec leaderboard([User]) :: String.t()
|
||||||
@spec is_valid_discord_id(term()) :: boolean()
|
defp leaderboard(users) do
|
||||||
defp is_valid_discord_id(discord_id) when is_binary(discord_id) do
|
"POST https://puyo.cattenheimer.xyz/top-poster?id=<discord-id>
|
||||||
String.match?(discord_id, ~r/^\d{17,20}$/)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp is_valid_discord_id(_), do: false
|
#{users |> Enum.map(fn user -> "#{String.pad_leading(user.id, 20)}: #{user.posts}" end) |> Enum.join("\n")}
|
||||||
|
"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
defmodule TopPosterServer.User do
|
defmodule TopPosterServer.User do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
discord_id: String.t(),
|
id: String.t(),
|
||||||
posts: non_neg_integer()
|
posts: non_neg_integer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@primary_key {:id, :string, []}
|
||||||
|
|
||||||
schema "users" do
|
schema "users" do
|
||||||
field(:discord_id, :string)
|
field :posts, :integer, default: 0
|
||||||
field(:posts, :integer, default: 0)
|
end
|
||||||
|
|
||||||
|
def changeset(user, params \\ %{}) do
|
||||||
|
user
|
||||||
|
|> cast(params, [:id, :posts])
|
||||||
|
|> validate_required([:id, :posts])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
10
priv/repo/migrations/20260119015123_initial.exs
Normal file
10
priv/repo/migrations/20260119015123_initial.exs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
defmodule TopPosterServer.Repo.Migrations.Initial do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create table("users", primary_key: false) do
|
||||||
|
add :id, :string, size: 32, primary_key: true, null: false
|
||||||
|
add :posts, :integer, default: 0, null: false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue