Skip to content

Commit 38d4d82

Browse files
authored
[WIP] Update to Phoenix 1.3 and Absinthe 1.4 (#17)
* A fresh Phoenix 1.3 app, hold the HTML/JS * Add absinthe dependencies * Add Post schema * Add users * Add Contact type * More build-out * Add mutation { post } * Add seeds, use :naive_datetime * Add post mutation * User creation, authentication stub
1 parent 3411cbe commit 38d4d82

69 files changed

Lines changed: 943 additions & 2009 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
/deps
55
/*.ez
66

7-
# Generate on crash by the VM
7+
# Generated on crash by the VM
88
erl_crash.dump
99

10-
# The config/prod.secret.exs file by default contains sensitive
11-
# data and you should not commit it into version control.
10+
# Files matching config/*.secret.exs pattern contain sensitive
11+
# data and you should not commit them into version control.
1212
#
1313
# Alternatively, you may comment the line below and commit the
14-
# secrets file as long as you replace its contents by environment
14+
# secrets files as long as you replace their contents by environment
1515
# variables.
16-
/config/prod.secret.exs
16+
/config/*.secret.exs

config/config.exs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
# is restricted to this project.
66
use Mix.Config
77

8+
# General application configuration
89
config :blog,
910
ecto_repos: [Blog.Repo]
10-
11+
1112
# Configures the endpoint
12-
config :blog, Blog.Endpoint,
13+
config :blog, BlogWeb.Endpoint,
1314
url: [host: "localhost"],
14-
root: Path.dirname(__DIR__),
15-
secret_key_base: "BLccQuJ1d3vKkB6dsLReE9GiTK6S1T6fp3wG/JVGeEuJiLgDFflq96zvmigYW61L",
16-
render_errors: [accepts: ~w(json)],
15+
secret_key_base: "oxuac8/0WnOgbiE17w/l47ZNzrpOKULZk3aiq+d6j3jvhgjeGizt7f9jcie37oJq",
16+
render_errors: [view: BlogWeb.ErrorView, accepts: ~w(json)],
1717
pubsub: [name: Blog.PubSub,
1818
adapter: Phoenix.PubSub.PG2]
1919

@@ -25,8 +25,3 @@ config :logger, :console,
2525
# Import environment specific config. This must remain at the bottom
2626
# of this file so it overrides the configuration defined above.
2727
import_config "#{Mix.env}.exs"
28-
29-
# Configure phoenix generators
30-
config :phoenix, :generators,
31-
migration: true,
32-
binary_id: false

config/dev.exs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@ use Mix.Config
66
# The watchers configuration can be used to run external
77
# watchers to your application. For example, we use it
88
# with brunch.io to recompile .js and .css sources.
9-
config :blog, Blog.Endpoint,
9+
config :blog, BlogWeb.Endpoint,
1010
http: [port: 4000],
1111
debug_errors: true,
1212
code_reloader: true,
13-
cache_static_lookup: false,
1413
check_origin: false,
1514
watchers: []
1615

16+
# ## SSL Support
17+
#
18+
# In order to use HTTPS in development, a self-signed
19+
# certificate can be generated by running the following
20+
# command from your terminal:
21+
#
22+
# openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -keyout priv/server.key -out priv/server.pem
23+
#
24+
# The `http:` config above can be replaced with:
25+
#
26+
# https: [port: 4000, keyfile: "priv/server.key", certfile: "priv/server.pem"],
27+
#
28+
# If desired, both `http:` and `https:` keys can be
29+
# configured to run both http and https servers on
30+
# different ports.
31+
1732
# Do not include metadata nor timestamps in development logs
1833
config :logger, :console, format: "[$level] $message\n"
1934

20-
# Set a higher stacktrace during development.
21-
# Do not configure such in production as keeping
22-
# and calculating stacktraces is usually expensive.
35+
# Set a higher stacktrace during development. Avoid configuring such
36+
# in production as building large stacktraces may be expensive.
2337
config :phoenix, :stacktrace_depth, 20
2438

2539
# Configure your database

config/prod.exs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
use Mix.Config
22

3-
# For production, we configure the host to read the PORT
4-
# from the system environment. Therefore, you will need
5-
# to set PORT=80 before running your server.
3+
# For production, we often load configuration from external
4+
# sources, such as your system environment. For this reason,
5+
# you won't find the :http configuration below, but set inside
6+
# BlogWeb.Endpoint.init/2 when load_from_system_env is
7+
# true. Any dynamic configuration should be done there.
68
#
7-
# You should also configure the url host to something
8-
# meaningful, we use this information when generating URLs.
9+
# Don't forget to configure the url host to something meaningful,
10+
# Phoenix uses this information when generating URLs.
911
#
10-
# Finally, we also include the path to a manifest
12+
# Finally, we also include the path to a cache manifest
1113
# containing the digested version of static files. This
12-
# manifest is generated by the mix phoenix.digest task
14+
# manifest is generated by the mix phx.digest task
1315
# which you typically run after static files are built.
14-
config :blog, Blog.Endpoint,
15-
http: [port: {:system, "PORT"}],
16+
config :blog, BlogWeb.Endpoint,
17+
load_from_system_env: true,
1618
url: [host: "example.com", port: 80],
17-
cache_static_manifest: "priv/static/manifest.json"
19+
cache_static_manifest: "priv/static/cache_manifest.json"
1820

1921
# Do not print debug messages in production
2022
config :logger, level: :info
@@ -24,10 +26,11 @@ config :logger, level: :info
2426
# To get SSL working, you will need to add the `https` key
2527
# to the previous section and set your `:url` port to 443:
2628
#
27-
# config :blog, Blog.Endpoint,
29+
# config :blog, BlogWeb.Endpoint,
2830
# ...
2931
# url: [host: "example.com", port: 443],
30-
# https: [port: 443,
32+
# https: [:inet6,
33+
# port: 443,
3134
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
3235
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")]
3336
#
@@ -38,7 +41,7 @@ config :logger, level: :info
3841
# We also recommend setting `force_ssl`, ensuring no data is
3942
# ever sent via http, always redirecting to https:
4043
#
41-
# config :blog, Blog.Endpoint,
44+
# config :blog, BlogWeb.Endpoint,
4245
# force_ssl: [hsts: true]
4346
#
4447
# Check `Plug.SSL` for all available options in `force_ssl`.
@@ -53,12 +56,8 @@ config :logger, level: :info
5356
# Alternatively, you can configure exactly which server to
5457
# start per endpoint:
5558
#
56-
# config :blog, Blog.Endpoint, server: true
59+
# config :blog, BlogWeb.Endpoint, server: true
5760
#
58-
# You will also need to set the application root to `.` in order
59-
# for the new static assets to be served after a hot upgrade:
60-
#
61-
# config :blog, Blog.Endpoint, root: "."
6261

6362
# Finally import the config/prod.secret.exs
6463
# which should be versioned separately.

config/test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use Mix.Config
22

33
# We don't run a server during test. If one is required,
44
# you can enable the server option below.
5-
config :blog, Blog.Endpoint,
5+
config :blog, BlogWeb.Endpoint,
66
http: [port: 4001],
77
server: false
88

docs/context.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/ecto.md

Whitespace-only changes.

lib/blog.ex

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
defmodule Blog do
2-
use Application
2+
@moduledoc """
3+
Blog keeps the contexts that define your domain
4+
and business logic.
35
4-
# See http://elixir-lang.org/docs/stable/elixir/Application.html
5-
# for more information on OTP Applications
6-
def start(_type, _args) do
7-
import Supervisor.Spec, warn: false
8-
9-
children = [
10-
# Start the endpoint when the application starts
11-
supervisor(Blog.Endpoint, []),
12-
# Start the Ecto repository
13-
supervisor(Blog.Repo, []),
14-
# Here you could define other workers and supervisors as children
15-
# worker(Blog.Worker, [arg1, arg2, arg3]),
16-
]
17-
18-
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
19-
# for other strategies and supported options
20-
opts = [strategy: :one_for_one, name: Blog.Supervisor]
21-
Supervisor.start_link(children, opts)
22-
end
23-
24-
# Tell Phoenix to update the endpoint configuration
25-
# whenever the application is updated.
26-
def config_change(changed, _new, removed) do
27-
Blog.Endpoint.config_change(changed, removed)
28-
:ok
29-
end
6+
Contexts are also responsible for managing your data, regardless
7+
if it comes from the database, an external API or others.
8+
"""
309
end

lib/blog/accounts/accounts.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Blog.Accounts do
2+
3+
alias Blog.{Accounts, Repo}
4+
5+
def find_user(id) do
6+
Repo.get(Accounts.User, id)
7+
end
8+
9+
def create_user(attrs) do
10+
{contact_attrs, user_attrs} = Map.pop(attrs, :contact)
11+
12+
Repo.transaction fn ->
13+
with {:ok, contact} <- create_contact(contact_attrs),
14+
{:ok, user} <- do_create_user(user_attrs, contact) do
15+
%{user | contacts: [contact]}
16+
end
17+
end
18+
19+
end
20+
21+
def create_contact(attrs) do
22+
attrs
23+
|> Accounts.Contact.changeset
24+
|> Blog.Repo.insert
25+
end
26+
27+
defp do_create_user(attrs, contact) do
28+
attrs
29+
|> Map.put(:contact_id, contact.id)
30+
|> Accounts.User.changeset
31+
|> Blog.Repo.insert
32+
end
33+
34+
end

lib/blog/accounts/contact.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule Blog.Accounts.Contact do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
alias Blog.Accounts
6+
7+
import EctoEnum, only: [defenum: 3]
8+
defenum TypeEnum, :contact_type, [:email, :phone]
9+
10+
schema "contacts" do
11+
field :type, :string
12+
field :value, :string
13+
14+
belongs_to :user, Accounts.User
15+
16+
timestamps()
17+
end
18+
19+
@doc false
20+
def changeset(attrs) do
21+
%__MODULE__{}
22+
|> changeset(attrs)
23+
end
24+
25+
@doc false
26+
def changeset(%Accounts.Contact{} = contact, attrs) do
27+
contact
28+
|> cast(attrs, [:type, :value])
29+
|> validate_required([:type, :value])
30+
end
31+
32+
end

0 commit comments

Comments
 (0)