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
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

7 changes: 1 addition & 6 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
use Mix.Config

# General application configuration
config :api_mock,
ecto_repos: [ApiMock.Repo]

# Configures the endpoint
config :api_mock, ApiMockWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "BqOwcYcO6yzbc8qp6e4SxlldOLILuvJ8JS+EfaJAMC8xqrgHXtdgxP2BmbVIGZ0Q",
render_errors: [view: ApiMockWeb.ErrorView, accepts: ~w(json)],
pubsub: [name: ApiMock.PubSub,
adapter: Phoenix.PubSub.PG2]
render_errors: [view: ApiMockWeb.ErrorView, accepts: ~w(json)]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
Expand Down
8 changes: 0 additions & 8 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,3 @@ config :logger, :console, format: "[$level] $message\n"
# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Configure your database
config :api_mock, ApiMock.Repo,
username: "apimock",
password: "apimock",
database: "api_mock_dev",
hostname: "localhost",
pool_size: 10
5 changes: 0 additions & 5 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ config :api_mock, ApiMockWeb.Endpoint,
# Do not print debug messages in production
config :logger, level: :info

config :api_mock, ApiMock.Repo,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
15 changes: 0 additions & 15 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,3 @@ config :api_mock, ApiMockWeb.Endpoint,

# Print only warnings and errors during test
config :logger, level: :warn

# Configure your database
config :api_mock, ApiMock.Repo,
username: "apimock",
password: "apimock",
database: "api_mock_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox

# Configure the database for GitHub Actions
if System.get_env("GITHUB_ACTIONS") do
config :api_mock, ApiMock.Repo,
username: "postgres",
password: "postgres"
end
4 changes: 0 additions & 4 deletions elixir_buildpack.config

This file was deleted.

2 changes: 0 additions & 2 deletions lib/api_mock/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ defmodule ApiMock.Application do

# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(ApiMock.Repo, []),
# Start the endpoint when the application starts
supervisor(ApiMockWeb.Endpoint, []),
# Start your own worker by calling: ApiMock.Worker.start_link(arg1, arg2, arg3)
Expand Down
138 changes: 87 additions & 51 deletions lib/api_mock/crew/crew.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ defmodule ApiMock.Crew do
The Crew context.
"""

import Ecto.Query, warn: false
alias ApiMock.Repo

alias ApiMock.Crew.Users

@doc """
Expand All @@ -18,7 +15,7 @@ defmodule ApiMock.Crew do

"""
def list_users do
Repo.all(Users)
list_users_json
end

@doc """
Expand All @@ -35,7 +32,12 @@ defmodule ApiMock.Crew do
** (Ecto.NoResultsError)

"""
def get_users!(id), do: Repo.get!(Users, id)
def get_users!(id) do
list_users
|> Enum.find_value({:error, :not_found}, fn u ->
if u[:id] == id, do: {:ok, u}
end)
end

@doc """
Creates a users.
Expand All @@ -52,53 +54,87 @@ defmodule ApiMock.Crew do
def create_users(attrs \\ %{}) do
%Users{}
|> Users.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a users.

## Examples

iex> update_users(users, %{field: new_value})
{:ok, %Users{}}

iex> update_users(users, %{field: bad_value})
{:error, %Ecto.Changeset{}}

"""
def update_users(%Users{} = users, attrs) do
users
|> Users.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a Users.

## Examples

iex> delete_users(users)
{:ok, %Users{}}

iex> delete_users(users)
{:error, %Ecto.Changeset{}}

"""
def delete_users(%Users{} = users) do
Repo.delete(users)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking users changes.

## Examples

iex> change_users(users)
%Ecto.Changeset{source: %Users{}}

"""
def change_users(%Users{} = users) do
Users.changeset(users, %{})
def list_users_json() do
[
%{name: "Philip J Fry",
position: "Delivery Boy",
id: "3aa03fb8-57c2-4926-8ab5-51459b477fd1",
profile: %{
gender: "M",
species: "Human",
age: 25,
planet: "Earth",
status: "Alive"
},
company: "Planet Express"},
%{name: "Turanga Leela",
position: "Captain",
id: "5b829f59-ee8d-481e-9980-5aa8f9e46953",
profile: %{
gender: "F",
species: "Mutant, Human",
age: 25,
planet: "Earth",
status: "Alive"
},
company: "Planet Express"},
%{name: "Bender Bending Rodríguez",
position: "Ship's Robot, Ship's Cook, Assistant Manager of Sales, Gunnery Chief",
id: "04921ff5-ccfe-40b1-9c92-1a369ea116a6",
profile: %{
gender: "M",
species: "Robot",
age: 4,
planet: "Earth",
status: "Alive"
},
company: "Planet Express"},
%{name: "Planet Express Ship",
position: "Ship",
id: "33190659-6e73-4162-80ff-563cb2b256e1",
profile: %{
gender: "N/A",
species: "Robotic Spacecraft",
age: "N/A",
planet: "Earth",
status: "N/A"
},
company: "Planet Express"},
%{name: "Dr. John A. Zoidberg Ph.D",
position: "Staff Doctor",
id: "88b5b4a0-c795-48a7-a6a2-5b344c760a94",
profile: %{
gender: "M",
species: "Decapodian",
age: 86,
planet: "Decapod 10",
status: "Alive",
},
company: "Planet Express"},
%{name: "Zapp Brannigan",
position: "Captain",
id: "bc1e90a7-0aa5-47a9-8a64-98d4f0f2ade8",
profile: %{
gender: "M",
species: "Human",
age: 28,
planet: "Earth",
status: "Alive"
},
company: "Democratic Order of Planets"},
%{name: "Kif Kroker",
position: "Lieutenant",
id: "d310ae2a-d301-497a-9956-e1394521ac26",
profile: %{
gender: "M",
species: "Amphibiosans",
age: 24,
planet: "Amphibios 9",
status: "Alive"
},
company: "Democratic Order of Planets"}
]
end
end
10 changes: 2 additions & 8 deletions lib/api_mock/crew/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule ApiMock.Crew.Users do
This module defines the Users model that are part of the Crew.
"""
use Ecto.Schema
import Ecto.Changeset

@primary_key {:id, :binary_id, autogenerate: false}

schema "users" do
field :profile, :map
Expand All @@ -13,11 +14,4 @@ defmodule ApiMock.Crew.Users do

timestamps()
end

@doc false
def changeset(users, attrs) do
users
|> cast(attrs, [:profile, :name, :position, :company])
|> validate_required([:name, :position, :company])
end
end
13 changes: 0 additions & 13 deletions lib/api_mock/repo.ex

This file was deleted.

6 changes: 0 additions & 6 deletions lib/api_mock_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ defmodule ApiMockWeb.FallbackController do
"""
use ApiMockWeb, :controller

def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> render(ApiMockWeb.ChangesetView, "error.json", changeset: changeset)
end

def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
Expand Down
28 changes: 3 additions & 25 deletions lib/api_mock_web/controllers/users_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,10 @@ defmodule ApiMockWeb.UsersController do
render(conn, "index.json", users: users)
end

def create(conn, %{"users" => users_params}) do
with {:ok, %Users{} = users} <- Crew.create_users(users_params) do
conn
|> put_status(:created)
|> put_resp_header("location", users_path(conn, :show, users))
|> render("show.json", users: users)
end
end

def show(conn, %{"id" => id}) do
users = Crew.get_users!(id)
render(conn, "show.json", users: users)
end

def update(conn, %{"id" => id, "users" => users_params}) do
users = Crew.get_users!(id)

with {:ok, %Users{} = users} <- Crew.update_users(users, users_params) do
render(conn, "show.json", users: users)
end
end

def delete(conn, %{"id" => id}) do
users = Crew.get_users!(id)
with {:ok, %Users{}} <- Crew.delete_users(users) do
send_resp(conn, :no_content, "")
with {:ok, user} <- Crew.get_users!(id) do
conn
|> render("show.json", users: user)
end
end
end
2 changes: 1 addition & 1 deletion lib/api_mock_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ defmodule ApiMockWeb.Router do

scope "/api", ApiMockWeb do
pipe_through :api
resources "/users", UsersController, except: [:new, :edit]
resources "/users", UsersController, only: [:index, :show]
end
end
4 changes: 0 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ defmodule ApiMock.Mixfile do
{:phoenix_pubsub, "~> 1.0"},
{:ecto_sql, "~> 3.0"},
{:phoenix_ecto, "~> 4.0"},
{:postgrex, ">= 0.0.0"},
{:jason, "~> 1.0"},
{:gettext, "~> 0.11"},
{:plug_cowboy, "~> 2.0"},
Expand All @@ -55,9 +54,6 @@ defmodule ApiMock.Mixfile do
# See the documentation for `Mix` for more info on aliases.
defp aliases do
[
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
test: ["ecto.create --quiet", "ecto.migrate", "test"]
]
end
end
Loading