Create table and repo for match

This commit is contained in:
Álvaro 2024-05-02 21:45:24 +02:00
parent f32418d294
commit f9271a5287
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,25 @@
defmodule LolAnalytics.Match.MatchRepo do
alias LolAnalytics.Match.MatchSchema
import Ecto.Query
@spec get_match(String.t()) :: %LolAnalytics.Match.MatchSchema{}
def get_match(match_id) do
query = from m in MatchSchema, where: m.match_id == ^match_id
LoLAnalytics.Repo.one(query)
end
@spec insert_match(String.t()) :: %LolAnalytics.Match.MatchSchema{}
def insert_match(match_id) do
MatchSchema.changeset(%MatchSchema{}, %{:match_id => match_id, :processed => false})
|> LoLAnalytics.Repo.insert()
end
@spec update_match(%LolAnalytics.Match.MatchSchema{}, term()) ::
%LolAnalytics.Match.MatchSchema{}
def update_match(match, attrs) do
match = MatchSchema.changeset(match, attrs)
LoLAnalytics.Repo.update(match)
end
end

View File

@ -0,0 +1,18 @@
defmodule LolAnalytics.Match.MatchSchema do
use Ecto.Schema
import Ecto.Changeset
schema "match" do
field :match_id, :string
field :processed, :boolean, default: false
timestamps()
end
def changeset(%__MODULE__{} = match, params \\ %{}) do
match
|> cast(params, [:match_id, :processed])
|> validate_required([:match_id, :processed])
end
end

View File

@ -0,0 +1,13 @@
defmodule LoLAnalytics.Repo.Migrations.Match do
use Ecto.Migration
def change do
create table("match", id: false) do
add :match_id, :string, primary_key: true
add :processed, :boolean
timestamps()
end
create index("match", [:processed])
end
end