more changes
This commit is contained in:
parent
beaf92f7c4
commit
a3a4af7965
18 changed files with 165 additions and 24 deletions
|
|
@ -10,7 +10,7 @@ defmodule EzcontainerRailway.GraphqlClient do
|
|||
|
||||
case Req.Request.put_headers(Req.new(), [{"Authorization", "Bearer #{opts[:token]}"}]) |> Req.post(url: @graphql_url, json: body) do
|
||||
{:ok, resp} -> resp
|
||||
{:error, _} -> :error
|
||||
{:error, e} -> raise(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
14
lib/ezcontainer_railway/projects.ex
Normal file
14
lib/ezcontainer_railway/projects.ex
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
defmodule EzcontainerRailway.Projects do
|
||||
import Ecto.Query
|
||||
|
||||
alias EzcontainerRailway.Repo
|
||||
alias EzcontainerRailway.Project
|
||||
|
||||
def get_project_by_user_id(user_id) do
|
||||
Repo.one(
|
||||
from p in Project,
|
||||
where: p.user_id == ^user_id,
|
||||
limit: 1
|
||||
)
|
||||
end
|
||||
end
|
||||
18
lib/ezcontainer_railway/projects/project.ex
Normal file
18
lib/ezcontainer_railway/projects/project.ex
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
defmodule EzcontainerRailway.Project do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "projects" do
|
||||
field :user_id, :string
|
||||
field :project_id, :string
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(projects, attrs) do
|
||||
projects
|
||||
|> cast(attrs, [:user_id, :project_id])
|
||||
|> validate_required([:user_id, :project_id])
|
||||
end
|
||||
end
|
||||
|
|
@ -8,6 +8,7 @@ defmodule EzcontainerRailway.Railway do
|
|||
edges {
|
||||
node {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,4 +17,53 @@ defmodule EzcontainerRailway.Railway do
|
|||
token: token
|
||||
)
|
||||
end
|
||||
|
||||
def create_service(container, token) when is_map(container) do
|
||||
GraphqlClient.query(
|
||||
"""
|
||||
""",
|
||||
variables: container
|
||||
)
|
||||
end
|
||||
|
||||
def create_project(name, token) do
|
||||
GraphqlClient.query(
|
||||
"""
|
||||
mutation {
|
||||
projectCreate(input: {name: $name}) {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
""",
|
||||
token: token,
|
||||
variables: %{ "name" => name }
|
||||
)
|
||||
|> format_response
|
||||
end
|
||||
|
||||
def is_token_valid?(token) do
|
||||
resp = get_projects(token).body
|
||||
|
||||
if has_errors?(resp) do
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def has_errors?(resp) when is_map(resp) do
|
||||
has_errors_in_array?(resp["errors"])
|
||||
end
|
||||
|
||||
defp format_response(resp) when is_map(resp) do
|
||||
if has_errors?(resp) do
|
||||
{:error, resp["error"]}
|
||||
else
|
||||
{:ok, resp["data"]}
|
||||
end
|
||||
end
|
||||
|
||||
defp has_errors_in_array?([_|_]), do: true
|
||||
defp has_errors_in_array?(_), do: false
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
defmodule EzcontainerRailway.Repo do
|
||||
use Ecto.Repo, otp_app: :ezcontainer_railway, adapter: Ecto.Adapters.SQLite3
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue