Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
env_file:
- .env
healthcheck:
test: ["CMD", "pg_isready"]
test: ["CMD", "pg_isready -U admin"]
timeout: 5s
retries: 5
networks:
Expand All @@ -21,8 +21,8 @@ services:
env_file:
- .env
volumes:
# - .:/app
- volume-app-data:/app
- .:/app
# - volume-app-data:/app
ports:
- "4000:4000"
networks:
Expand Down
2 changes: 1 addition & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

mix deps.get --only prod
mix deps.get

/wait

Expand Down
14 changes: 14 additions & 0 deletions lib/api/game.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Api.Game do
alias Api.Repo
alias Api.Game.Lobby

def list_lobbies do
Repo.all(Lobby)
end

def create_lobby(attrs \\ %{}) do
%Lobby{}
|> Lobby.changeset(attrs)
|> Repo.insert()
end
end
24 changes: 24 additions & 0 deletions lib/api/game/lobby.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Api.Game.Lobby do
use Ecto.Schema
import Ecto.Changeset
alias Api.Game.Lobby

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "lobbies" do
field :name, :string
field :topic, :string

timestamps()
end

@doc false
def changeset(%Lobby{} = lobby, attrs) do
lobby
|> cast(attrs, [:name, :topic])
|> validate_required([:name])
|> unique_constraint(:name)
|> validate_length(:name, min: 5, max: 30)
|> validate_length(:topic, min: 5, max: 120)
end
end
21 changes: 21 additions & 0 deletions lib/api_web/controllers/v1/lobby_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule ApiWeb.V1.LobbyController do
use ApiWeb, :controller

alias Api.Game.Lobby
alias Api.Game
alias Api.Repo

def index(conn, _params) do
lobbies = Game.list_lobbies
render(conn, "index.json", lobbies: lobbies)
end

# curl -X POST http://localhost:4000/api/v1/lobbies -H "Content-Type: application/json" -d '{"lobby": {"name": "From cURL", "topic": "Create test topic"}}'
def create(conn, %{"lobby" => lobby_params}) do
case Game.create_lobby(lobby_params) do
{:ok, lobby} ->
conn
|> render("show.json", lobby: lobby)
end
end
end
7 changes: 5 additions & 2 deletions lib/api_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ defmodule ApiWeb.Router do

scope "/api", ApiWeb do
pipe_through :api

scope "/v1", V1 do
get "/health", HealthController, :index
get "/health", HealthController, :index
get "/lobbies", LobbyController, :index
post "/lobbies", LobbyController, :create
end
end
end

29 changes: 29 additions & 0 deletions lib/api_web/views/v1/room_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule ApiWeb.V1.LobbyView do
use ApiWeb, :view

alias Api.Lobby

def render("index.json", %{lobbies: lobbies}) do
%{
lobbies: Enum.map(lobbies, &lobby_json/1)
}
end

def render("show.json", %{lobby: lobby}) do
%{
id: lobby.id,
name: lobby.name,
topic: lobby.topic
}
end

def lobby_json(lobby) do
%{
id: lobby.id,
name: lobby.name,
topic: lobby.topic,
# inserted_at: lobby.inserted_at,
# updated_at: lobby.updated_at
}
end
end
14 changes: 14 additions & 0 deletions priv/repo/migrations/20210406105408_create_lobbies.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Api.Repo.Migrations.CreateLobbies do
use Ecto.Migration

def change do
create table(:lobbies, primary_key: false) do
add :id, :binary_id, primary_key: true
add :name, :string, null: false, size: 30
add :topic, :string, size: 120

timestamps()
end

end
end