Retrieve languages from JSON endpoint.
parent
ba5070be04
commit
283980176e
|
@ -22,7 +22,6 @@ defmodule BoardWise.Coaches do
|
|||
def list_coaches do
|
||||
Coach
|
||||
|> limit(6)
|
||||
|> where(site: "lichess")
|
||||
|> Repo.all(prefix: @prefix)
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
defmodule BoardWise.Languages do
|
||||
@moduledoc """
|
||||
The Languages context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias BoardWise.Repo
|
||||
|
||||
alias BoardWise.Languages.Language
|
||||
|
||||
@prefix "coach_scraper"
|
||||
|
||||
@doc """
|
||||
Returns the list of languages.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_languages()
|
||||
[%Language{}, ...]
|
||||
|
||||
"""
|
||||
def list_languages do
|
||||
Repo.all(Language, prefix: @prefix)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single language.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Language does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_language!(123)
|
||||
%Language{}
|
||||
|
||||
iex> get_language!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_language!(id), do: Repo.get!(Language, id, prefix: @prefix)
|
||||
|
||||
@doc """
|
||||
Creates a language.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_language(%{field: value})
|
||||
{:ok, %Language{}}
|
||||
|
||||
iex> create_language(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_language(attrs \\ %{}) do
|
||||
%Language{}
|
||||
|> Language.changeset(attrs)
|
||||
|> Repo.insert(prefix: @prefix)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a language.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_language(language, %{field: new_value})
|
||||
{:ok, %Language{}}
|
||||
|
||||
iex> update_language(language, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_language(%Language{} = language, attrs) do
|
||||
language
|
||||
|> Language.changeset(attrs)
|
||||
|> Repo.update(prefix: @prefix)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a language.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_language(language)
|
||||
{:ok, %Language{}}
|
||||
|
||||
iex> delete_language(language)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_language(%Language{} = language) do
|
||||
Repo.delete(language, prefix: @prefix)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking language changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_language(language)
|
||||
%Ecto.Changeset{data: %Language{}}
|
||||
|
||||
"""
|
||||
def change_language(%Language{} = language, attrs \\ %{}) do
|
||||
Language.changeset(language, attrs)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,17 @@
|
|||
defmodule BoardWise.Languages.Language do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "languages" do
|
||||
field :code, :string
|
||||
field :name, :string
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(language, attrs) do
|
||||
language
|
||||
|> cast(attrs, [:code, :name])
|
||||
|> validate_required([:code, :name])
|
||||
|> unique_constraint(:code_unique, name: :code_unique)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,10 @@
|
|||
defmodule BoardWiseWeb.LanguageController do
|
||||
use BoardWiseWeb, :controller
|
||||
|
||||
alias BoardWise.Languages
|
||||
|
||||
def index(conn, _params) do
|
||||
langs = Languages.list_languages()
|
||||
render(conn, :index, langs: langs)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,17 @@
|
|||
defmodule BoardWiseWeb.LanguageJSON do
|
||||
alias BoardWise.Languages.Language
|
||||
|
||||
@doc """
|
||||
Renders a list of coaches.
|
||||
"""
|
||||
def index(%{langs: langs}) do
|
||||
%{data: for(lang <- langs, do: data(lang))}
|
||||
end
|
||||
|
||||
defp data(%Language{} = lang) do
|
||||
%{
|
||||
code: lang.code,
|
||||
name: lang.name
|
||||
}
|
||||
end
|
||||
end
|
|
@ -26,6 +26,7 @@ defmodule BoardWiseWeb.Router do
|
|||
pipe_through :api
|
||||
|
||||
get "/coaches", CoachController, :index
|
||||
get "/languages", LanguageController, :index
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
defmodule BoardWise.Repo.Migrations.CreateLanguages do
|
||||
use Ecto.Migration
|
||||
|
||||
@prefix "coach_scraper"
|
||||
|
||||
def change do
|
||||
create table(:languages, prefix: @prefix) do
|
||||
add :code, :string, null: false
|
||||
add :name, :string, null: false
|
||||
end
|
||||
|
||||
create unique_index(
|
||||
:languages,
|
||||
[:code],
|
||||
prefix: @prefix,
|
||||
name: "code_unique"
|
||||
)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,61 @@
|
|||
defmodule BoardWise.LanguagesTest do
|
||||
use BoardWise.DataCase
|
||||
|
||||
alias BoardWise.Languages
|
||||
|
||||
describe "languages" do
|
||||
alias BoardWise.Languages.Language
|
||||
|
||||
import BoardWise.LanguagesFixtures
|
||||
|
||||
@invalid_attrs %{code: nil, name: nil}
|
||||
|
||||
test "list_languages/0 returns all languages" do
|
||||
language = language_fixture()
|
||||
assert Languages.list_languages() == [language]
|
||||
end
|
||||
|
||||
test "get_language!/1 returns the language with given id" do
|
||||
language = language_fixture()
|
||||
assert Languages.get_language!(language.id) == language
|
||||
end
|
||||
|
||||
test "create_language/1 with valid data creates a language" do
|
||||
valid_attrs = %{code: "some code", name: "some name"}
|
||||
|
||||
assert {:ok, %Language{} = language} = Languages.create_language(valid_attrs)
|
||||
assert language.code == "some code"
|
||||
assert language.name == "some name"
|
||||
end
|
||||
|
||||
test "create_language/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Languages.create_language(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_language/2 with valid data updates the language" do
|
||||
language = language_fixture()
|
||||
update_attrs = %{code: "some updated code", name: "some updated name"}
|
||||
|
||||
assert {:ok, %Language{} = language} = Languages.update_language(language, update_attrs)
|
||||
assert language.code == "some updated code"
|
||||
assert language.name == "some updated name"
|
||||
end
|
||||
|
||||
test "update_language/2 with invalid data returns error changeset" do
|
||||
language = language_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Languages.update_language(language, @invalid_attrs)
|
||||
assert language == Languages.get_language!(language.id)
|
||||
end
|
||||
|
||||
test "delete_language/1 deletes the language" do
|
||||
language = language_fixture()
|
||||
assert {:ok, %Language{}} = Languages.delete_language(language)
|
||||
assert_raise Ecto.NoResultsError, fn -> Languages.get_language!(language.id) end
|
||||
end
|
||||
|
||||
test "change_language/1 returns a language changeset" do
|
||||
language = language_fixture()
|
||||
assert %Ecto.Changeset{} = Languages.change_language(language)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,21 @@
|
|||
defmodule BoardWise.LanguagesFixtures do
|
||||
@moduledoc """
|
||||
This module defines test helpers for creating
|
||||
entities via the `BoardWise.Languages` context.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Generate a language.
|
||||
"""
|
||||
def language_fixture(attrs \\ %{}) do
|
||||
{:ok, language} =
|
||||
attrs
|
||||
|> Enum.into(%{
|
||||
code: "some code",
|
||||
name: "some name"
|
||||
})
|
||||
|> BoardWise.Languages.create_language()
|
||||
|
||||
language
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue