1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-03-06 07:45:25 +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}