1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-09 16:42:49 +00:00

lots of things done: thread - reply - category - seeds

This commit is contained in:
2018-01-02 23:38:17 -06:00
parent 3a61151191
commit 115dacdd12
15 changed files with 291 additions and 27 deletions

View File

@@ -0,0 +1,24 @@
defmodule MyApp.Data.Category do
use Ecto.Schema
import Ecto.Query
import Ecto.Changeset
alias MyApp.Repo
alias MyApp.Data
@derive {Poison.Encoder, except: [:__meta__]}
schema "category" do
field :category, :string
field :title, :string
end
def changeset(category, params \\ %{}) do
category
|> cast(params, [:category, :title])
|> validate_required([:category, :title])
end
def get_categories() do
Repo.all(Data.Category)
end
end

32
lib/myapp/data/reply.ex Normal file
View File

@@ -0,0 +1,32 @@
defmodule MyApp.Data.Reply do
use Ecto.Schema
import Ecto.Query
import Ecto.Changeset
alias MyApp.Repo
alias MyApp.Data
@derive {Poison.Encoder, except: [:__meta__]}
schema "reply" do
field :user_id, :integer # references :user
field :thread_id, :integer # references :thread
field :content, :string
field :edited, :boolean, default: false
field :quote, :boolean, default: false
timestamps()
end
def changeset(reply, params \\ %{}) do
reply
|> cast(params, [:user_id, :thread_id, :content, :edited, :quote])
|> validate_required([:user_id, :thread_id, :content])
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:thread_id)
end
def insert_reply(params) do
changeset(%Data.Reply{}, params)
|> Repo.insert
|> Data.Util.process_insert_or_update
end
end

View File

@@ -8,21 +8,43 @@ defmodule MyApp.Data.Thread do
@derive {Poison.Encoder, except: [:__meta__]}
schema "thread" do
field :title, :string
field :category_id, :integer # references :category
field :content, :string
field :view_count, :integer
field :user_id, :integer
field :view_count, :integer, default: 0
field :user_id, :integer # references :user
field :last_reply_id, :integer
field :sticky, :boolean, default: false
field :locked, :boolean, default: false
field :edited, :boolean, default: false
belongs_to :user, Data.User, define_field: false
timestamps()
end
def changeset(thread, params \\ %{}) do
thread
|> cast(params, [:title, :content, :user_id, :view_count, :last_reply_id, :sticky, :locked])
|> validate_required([:title, :content, :user_id])
|> cast(params, [:id, :title, :category_id, :content, :user_id, :view_count, :last_reply_id, :sticky, :locked, :edited])
|> validate_required([:title, :category_id, :content, :user_id])
|> foreign_key_constraint(:category_id)
end
def insert_thread(params) do
changeset(%Data.Thread{}, params)
|> Repo.insert
|> Data.Util.process_insert_or_update
end
def update_thread(params) do
Repo.get(Data.Thread, Map.get(params, "id"))
|> process_update(params)
end
# TODO: check user permissions for sticky/locked
defp process_update(thread, _params) when is_nil(thread), do: {:error, "Invalid thread"}
defp process_update(thread, params) when not is_nil(thread) do
changeset(thread, Map.take(params, ["content", "edited", "sticky", "locked"]))
|> IO.inspect
|> Repo.update
|> Data.Util.process_insert_or_update
end
end

View File

@@ -5,12 +5,11 @@ defmodule MyApp.Data.User do
alias MyApp.Repo
alias MyApp.Data
@derive {Poison.Encoder, except: [:__meta__]}
@derive {Poison.Encoder, except: [:__meta__, :__struct__]}
schema "user" do
field :battle_net_id, :integer
field :battletag, :string
has_many :threads, Data.Thread
timestamps()
end
@@ -55,29 +54,22 @@ defmodule MyApp.Data.User do
end
defp insert_user(params) do
cs = changeset(%Data.User{}, params)
cs
changeset(%Data.User{}, params)
|> Repo.insert
|> process_insert_or_update
|> Data.Util.process_insert_or_update
|> filter_values
end
# it's possible for a user's battle tag to change - if so update it
defp update_battletag(user, params) do
cs = Data.User.changeset(Map.merge(%Data.User{}, user), %{battletag: Map.get(params, "battletag")})
cs
changeset(Map.merge(%Data.User{}, user), %{battletag: Map.get(params, "battletag")})
|> Repo.update
|> process_insert_or_update
|> Data.Util.process_insert_or_update
|> filter_values
end
defp process_insert_or_update({:error, changeset}), do: {:error, map_changeset(changeset)}
defp process_insert_or_update({:ok, user}) do
{:ok, Map.take(user, [:id, :battle_net_id, :battletag])} # only grab the fields we need
end
defp map_changeset(changeset) do
Enum.map(changeset.errors, fn {key, val} ->
%{key => elem(val, 0)}
end)
end
# take certain values after insertion
defp filter_values({:error, error}), do: {:error, error}
defp filter_values({:ok, user}), do: {:ok, Map.take(user, [:id, :battle_net_id, :battletag])}
end

12
lib/myapp/data/util.ex Normal file
View File

@@ -0,0 +1,12 @@
defmodule MyApp.Data.Util do
def map_changeset(changeset) do
Enum.map(changeset.errors, fn {key, val} ->
%{key => elem(val, 0)}
end)
end
def process_insert_or_update({:error, changeset}), do: {:error, map_changeset(changeset)}
def process_insert_or_update({:ok, data}), do: {:ok, data}
end

View File

@@ -0,0 +1,16 @@
defmodule MyAppWeb.CategoryController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.Data
@spec get_collection(map, map) :: any
def get_collection(conn, _params) do
output = Data.Category.get_categories()
conn
|> put_status(200)
|> Response.json(output)
end
end

View File

@@ -0,0 +1,22 @@
defmodule MyAppWeb.ReplyController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.Data
@spec insert(map, map) :: any
def insert(conn, params) do
user_id = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.get("id")
{output, status} = params
|> Map.put("user_id", user_id)
|> Data.Reply.insert_reply
|> Response.put_resp
conn
|> put_status(status)
|> Response.json(output)
end
end

View File

@@ -0,0 +1,38 @@
defmodule MyAppWeb.ThreadController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.Data
@spec insert(map, map) :: any
def insert(conn, params) do
user_id = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.get("id")
{output, status} = params
|> Map.put("user_id", user_id)
|> Data.Thread.insert_thread
|> Response.put_resp
conn
|> put_status(status)
|> Response.json(output)
end
@spec update(map, map) :: any
def update(conn, params) do
user_id = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.get("id")
{output, status} = params
|> Map.put("user_id", user_id)
|> Data.Thread.update_thread
|> Response.put_resp
conn
|> put_status(status)
|> Response.json(output)
end
end

View File

@@ -26,6 +26,23 @@ defmodule MyAppWeb.Router do
pipe_through [:api_auth]
get "/", UserController, :index
end
scope "/thread" do
# authenticated routes
pipe_through [:api_auth]
post "/", ThreadController, :insert
put "/", ThreadController, :update
end
scope "reply" do
# authenticated routes
pipe_through [:api_auth]
post "/", ReplyController, :insert
end
scope "/category" do
get "/", CategoryController, :get_collection
end
end
end

View File

@@ -57,7 +57,8 @@ defmodule MyApp.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.setup": ["ecto.create", "ecto.migrate", "seeds"],
"seeds": ["run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"test": ["ecto.create --quiet", "ecto.migrate", "test"]
]

View File

@@ -4,15 +4,15 @@ defmodule MyApp.Repo.Migrations.CreateThread do
def change do
create table(:thread) do
add :title, :string
add :category_id, :integer
add :content, :string
add :view_count, :integer
add :user_id, references(:user)
add :last_reply_id, :integer
add :last_reply_id, :integer # TODO: figure this out
add :sticky, :boolean
add :locked, :boolean
add :edited, :boolean
timestamps()
end
end
end

View File

@@ -0,0 +1,13 @@
defmodule MyApp.Repo.Migrations.CreateCategory do
use Ecto.Migration
def change do
create table(:category) do
add :category, :string
add :title, :string
end
create unique_index(:category, [:category, :title])
end
end

View File

@@ -0,0 +1,9 @@
defmodule MyApp.Repo.Migrations.ThreadCategoryForeignKey do
use Ecto.Migration
def change do
alter table(:thread) do
modify :category_id, references(:category)
end
end
end

View File

@@ -0,0 +1,14 @@
defmodule MyApp.Repo.Migrations.CreateReply do
use Ecto.Migration
def change do
create table(:reply) do
add :user_id, references(:user)
add :thread_id, references(:thread)
add :content, :string
add :edited, :boolean
add :quote, :boolean
timestamps()
end
end
end

View File

@@ -9,3 +9,55 @@
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias MyApp.Repo
alias MyApp.Data
defmodule Category do
def get_seed() do
map_categories("class", get_classes())
|> Enum.concat(map_categories("realm", get_realms()))
|> Enum.concat(map_categories("other", get_other()))
end
defp map_categories(category, titles) do
titles
|> Enum.map(fn (t) -> %{category: category, title: t} end)
end
defp get_classes() do
[
"Druid",
"Rogue",
"Priest",
"Hunter",
"Shaman",
"Warrior",
"Mage",
"Paladin",
"Warlock",
]
end
# TODO: add all realms
defp get_realms() do
[
"Stonemaul",
]
end
defp get_other() do
[
"Off-Topic",
"Guild Recruitment",
"General Discussion",
"Suggestions",
"Role-Playing",
"Raid and Dungeon Discussion",
]
end
end
Repo.insert_all(Data.Category, Category.get_seed())