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

server - remove content from thread - store in replies

This commit is contained in:
2018-01-14 17:58:00 -06:00
parent 2584e6a379
commit d9d7f2d202
4 changed files with 17 additions and 43 deletions

View File

@@ -11,7 +11,7 @@ defmodule MyApp.Data.Reply do
field :thread_id, :integer # references :thread
field :content, :string
field :edited, :boolean, default: false
field :quote, :boolean, default: false
field :quote_id, :integer
timestamps()
end

View File

@@ -9,14 +9,13 @@ defmodule MyApp.Data.Thread do
schema "thread" do
field :title, :string
field :category_id, :integer # references :category
field :content, :string
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
field :reply_count, :integer, default: 0
field :reply_count, :integer, default: 1
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
@@ -25,8 +24,8 @@ defmodule MyApp.Data.Thread do
defp insert_changeset(thread, params \\ %{}) do
thread
|> cast(params, [:title, :category_id, :content, :user_id, :last_reply_id])
|> validate_required([:title, :category_id, :content, :user_id])
|> cast(params, [:title, :category_id, :user_id, :last_reply_id])
|> validate_required([:title, :category_id, :user_id])
|> foreign_key_constraint(:category_id)
|> foreign_key_constraint(:user_id)
end
@@ -37,14 +36,6 @@ defmodule MyApp.Data.Thread do
|> cast(params, [:sticky, :locked])
end
# allow user to update content of their own thread
defp user_update_changeset(thread, params \\ %{}) do
thread
|> cast(params, [:content])
|> force_change(:edited, true) # set edited flag on update
|> validate_required([:content])
end
def get(thread_id) do
query = from t in Data.Thread,
where: t.id == ^thread_id,
@@ -65,7 +56,6 @@ defmodule MyApp.Data.Thread do
:locked,
:last_reply_id,
:edited,
:content,
:category_id,
:title,
:view_count,
@@ -89,25 +79,18 @@ defmodule MyApp.Data.Thread do
@spec insert(map) :: {:ok, map} | {:error, map}
def insert(params) do
params = Map.put(params, "last_reply_id", Map.get(params, "user_id"))
insert_changeset(%Data.Thread{}, params)
|> Repo.insert
|> remove_associations
|> Data.Util.process_insert_or_update
end
Repo.transaction(fn ->
params = Map.put(params, "last_reply_id", Map.get(params, "user_id"))
{:ok, thread} = insert_changeset(%Data.Thread{}, params)
|> Repo.insert
# update thread for user permission
@spec user_update(map) :: {:ok, map} | {:error, map}
def user_update(params) do
id = Map.get(params, "id")
user_id = Map.get(params, "user_id")
if is_nil(id) || is_nil(user_id) do
{:error, "Invalid thread"}
else
Repo.get_by(Data.Thread, %{id: id, user_id: user_id})
|> process_user_update(params)
end
{:ok, _} = Repo.insert(%Data.Reply{
thread_id: Map.get(thread, :id),
content: Map.get(params, "content"),
user_id: Map.get(params, "user_id")
})
"ok"
end)
end
# this doesn't update the 'updated_at' field which is what we want
@@ -120,14 +103,6 @@ defmodule MyApp.Data.Thread do
# TODO: delete thread
defp process_user_update(thread, _params) when is_nil(thread), do: {:error, "Invalid thread"}
defp process_user_update(thread, params) when not is_nil(thread) do
user_update_changeset(thread, params)
|> Repo.update
|> remove_associations
|> Data.Util.process_insert_or_update
end
defp remove_associations({err, data}), do: {err, Map.drop(data, [:user, :replies, :last_reply])}
end

View File

@@ -5,10 +5,9 @@ defmodule MyApp.Repo.Migrations.CreateThread do
create table(:thread) do
add :title, :string
add :category_id, :integer
add :content, :string, size: 2000
add :view_count, :integer
add :user_id, references(:user)
add :last_reply_id, :integer # TODO: figure this out
add :last_reply_id, :integer
add :sticky, :boolean
add :locked, :boolean
add :edited, :boolean

View File

@@ -7,7 +7,7 @@ defmodule MyApp.Repo.Migrations.CreateReply do
add :thread_id, references(:thread)
add :content, :string, size: 2000
add :edited, :boolean
add :quote, :boolean
add :quote_id, :integer
timestamps()
end
end