Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 404fa7e

Browse files
committed
Set up games folder and migration for slugs
1 parent 4880965 commit 404fa7e

File tree

8 files changed

+45
-7
lines changed

8 files changed

+45
-7
lines changed

assets/elm/src/Games/Platformer.elm

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Games.Platformer exposing (main)
2+
3+
import Html exposing (..)
4+
5+
6+
main : Html msg
7+
main =
8+
text "Platformer Game"

assets/js/app.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ import "phoenix_html"
1717
// import socket from "./socket"
1818

1919
// Elm
20-
import { Elm } from "../elm/src/Main.elm"
20+
import { Elm } from "../elm/src/Main.elm";
2121

22-
Elm.Main.init({
23-
node: document.getElementById("elm-container")
24-
})
22+
const elmContainer = document.querySelector("#elm-container");
23+
const platformer = document.querySelector("#platformer");
24+
25+
if (elmContainer) {
26+
Elm.Main.init({ node: elmContainer });
27+
}
28+
if (platformer) {
29+
Elm.Games.Platformer.init({ node: platformer });
30+
}

assets/webpack.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ module.exports = (env, options) => ({
3838
use: {
3939
loader: 'elm-webpack-loader',
4040
options: {
41-
cwd: path.resolve(__dirname, 'elm')
41+
cwd: path.resolve(__dirname, 'elm'),
42+
files: [
43+
path.resolve(__dirname, "elm/src/Main.elm"),
44+
path.resolve(__dirname, "elm/src/Games/Platformer.elm")
45+
]
4246
}
4347
}
4448
}

lib/platform/products/game.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Platform.Products.Game do
1010

1111
field :description, :string
1212
field :featured, :boolean, default: false
13+
field :slug, :string, unique: true
1314
field :thumbnail, :string
1415
field :title, :string
1516

@@ -19,7 +20,8 @@ defmodule Platform.Products.Game do
1920
@doc false
2021
def changeset(game, attrs) do
2122
game
22-
|> cast(attrs, [:description, :featured, :thumbnail, :title])
23-
|> validate_required([:description, :featured, :thumbnail, :title])
23+
|> cast(attrs, [:description, :featured, :slug, :thumbnail, :title])
24+
|> validate_required([:description, :featured, :slug, :thumbnail, :title])
25+
|> unique_constraint(:slug)
2426
end
2527
end

lib/platform_web/controllers/game_controller.ex

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ defmodule PlatformWeb.GameController do
2525
render(conn, "show.json", game: game)
2626
end
2727

28+
def play(conn, %{"id" => id}) do
29+
game = Products.get_game!(id)
30+
render(conn, "show.html", game: game)
31+
end
32+
2833
def update(conn, %{"id" => id, "game" => game_params}) do
2934
game = Products.get_game!(id)
3035

lib/platform_web/router.ex

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule PlatformWeb.Router do
1818
pipe_through :browser
1919

2020
get "/", PageController, :index
21+
get "/games/:id", GameController, :play
2122
resources "/players", PlayerController
2223
resources "/sessions", PlayerSessionController, only: [:new, :create, :delete]
2324
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="<%= @game.title |> String.downcase() %>"></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Platform.Repo.Migrations.AddSlugToGames do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:games) do
6+
add :slug, :string
7+
end
8+
9+
create unique_index(:games, [:slug])
10+
end
11+
end

0 commit comments

Comments
 (0)