1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-10 17:12:48 +00:00

little bit of everything - it's been a long day

This commit is contained in:
2018-01-14 23:46:15 -06:00
parent d9d7f2d202
commit efbee265d3
25 changed files with 298 additions and 125 deletions

View File

@@ -12,12 +12,13 @@ defmodule MyApp.Data.Reply do
field :content, :string
field :edited, :boolean, default: false
field :quote_id, :integer
timestamps()
has_one :user, Data.User, foreign_key: :id, references: :user_id
timestamps(type: :utc_datetime)
end
defp insert_changeset(reply, params \\ %{}) do
reply
|> cast(params, [:user_id, :thread_id, :content, :quote])
|> cast(params, [:user_id, :thread_id, :content, :quote_id])
|> validate_required([:user_id, :thread_id, :content])
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:thread_id)
@@ -33,10 +34,11 @@ defmodule MyApp.Data.Reply do
@spec insert(map) :: {:ok, map} | {:error, map}
def insert(params) do
insert_changeset(%Data.Reply{}, params)
{:ok, data} = insert_changeset(%Data.Reply{}, params)
|> Repo.insert
|> Data.Util.process_insert_or_update
|> update_thread_new_reply
{:ok, Map.drop(data, [:user])} # drop user because we can't encode it if it's not preloaded
end
defp update_thread_new_reply({:error, error}), do: {:error, error}

View File

@@ -15,11 +15,11 @@ defmodule MyApp.Data.Thread do
field :sticky, :boolean, default: false
field :locked, :boolean, default: false
field :edited, :boolean, default: false
field :reply_count, :integer, default: 1
field :reply_count, :integer, default: 0
has_many :replies, Data.Reply
has_one :user, Data.User, foreign_key: :id, references: :user_id
has_one :last_reply, Data.User, foreign_key: :id, references: :last_reply_id
timestamps()
timestamps(type: :utc_datetime)
end
defp insert_changeset(thread, params \\ %{}) do
@@ -39,7 +39,7 @@ defmodule MyApp.Data.Thread do
def get(thread_id) do
query = from t in Data.Thread,
where: t.id == ^thread_id,
preload: [:user, :last_reply, :replies]
preload: [:user, :last_reply, replies: :user]
Repo.one(query)
|> process_get
@@ -79,18 +79,20 @@ defmodule MyApp.Data.Thread do
@spec insert(map) :: {:ok, map} | {:error, map}
def insert(params) do
Repo.transaction(fn ->
{_, data} = Repo.transaction(fn ->
params = Map.put(params, "last_reply_id", Map.get(params, "user_id"))
{:ok, thread} = insert_changeset(%Data.Thread{}, params)
|> Repo.insert
{:ok, _} = Repo.insert(%Data.Reply{
{:ok, data} = Repo.insert(%Data.Reply{
thread_id: Map.get(thread, :id),
content: Map.get(params, "content"),
user_id: Map.get(params, "user_id")
})
"ok"
# return the new thread we inserted - drop associations because we don't load them
{:ok, Map.drop(thread, [:last_reply, :replies, :user])}
end)
data
end
# this doesn't update the 'updated_at' field which is what we want

View File

@@ -15,7 +15,7 @@ defmodule MyApp.Data.User do
field :character_class, :string
field :character_realm, :string
field :character_avatar, :string
timestamps()
timestamps(type: :utc_datetime)
end
defp changeset(user, params \\ %{}) do