mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 09:32:51 +00:00
server - remove content from thread - store in replies
This commit is contained in:
@@ -11,7 +11,7 @@ defmodule MyApp.Data.Reply do
|
|||||||
field :thread_id, :integer # references :thread
|
field :thread_id, :integer # references :thread
|
||||||
field :content, :string
|
field :content, :string
|
||||||
field :edited, :boolean, default: false
|
field :edited, :boolean, default: false
|
||||||
field :quote, :boolean, default: false
|
field :quote_id, :integer
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,13 @@ defmodule MyApp.Data.Thread do
|
|||||||
schema "thread" do
|
schema "thread" do
|
||||||
field :title, :string
|
field :title, :string
|
||||||
field :category_id, :integer # references :category
|
field :category_id, :integer # references :category
|
||||||
field :content, :string
|
|
||||||
field :view_count, :integer, default: 0
|
field :view_count, :integer, default: 0
|
||||||
field :user_id, :integer # references :user
|
field :user_id, :integer # references :user
|
||||||
field :last_reply_id, :integer
|
field :last_reply_id, :integer
|
||||||
field :sticky, :boolean, default: false
|
field :sticky, :boolean, default: false
|
||||||
field :locked, :boolean, default: false
|
field :locked, :boolean, default: false
|
||||||
field :edited, :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_many :replies, Data.Reply
|
||||||
has_one :user, Data.User, foreign_key: :id, references: :user_id
|
has_one :user, Data.User, foreign_key: :id, references: :user_id
|
||||||
has_one :last_reply, Data.User, foreign_key: :id, references: :last_reply_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
|
defp insert_changeset(thread, params \\ %{}) do
|
||||||
thread
|
thread
|
||||||
|> cast(params, [:title, :category_id, :content, :user_id, :last_reply_id])
|
|> cast(params, [:title, :category_id, :user_id, :last_reply_id])
|
||||||
|> validate_required([:title, :category_id, :content, :user_id])
|
|> validate_required([:title, :category_id, :user_id])
|
||||||
|> foreign_key_constraint(:category_id)
|
|> foreign_key_constraint(:category_id)
|
||||||
|> foreign_key_constraint(:user_id)
|
|> foreign_key_constraint(:user_id)
|
||||||
end
|
end
|
||||||
@@ -37,14 +36,6 @@ defmodule MyApp.Data.Thread do
|
|||||||
|> cast(params, [:sticky, :locked])
|
|> cast(params, [:sticky, :locked])
|
||||||
end
|
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
|
def get(thread_id) do
|
||||||
query = from t in Data.Thread,
|
query = from t in Data.Thread,
|
||||||
where: t.id == ^thread_id,
|
where: t.id == ^thread_id,
|
||||||
@@ -65,7 +56,6 @@ defmodule MyApp.Data.Thread do
|
|||||||
:locked,
|
:locked,
|
||||||
:last_reply_id,
|
:last_reply_id,
|
||||||
:edited,
|
:edited,
|
||||||
:content,
|
|
||||||
:category_id,
|
:category_id,
|
||||||
:title,
|
:title,
|
||||||
:view_count,
|
:view_count,
|
||||||
@@ -89,25 +79,18 @@ defmodule MyApp.Data.Thread do
|
|||||||
|
|
||||||
@spec insert(map) :: {:ok, map} | {:error, map}
|
@spec insert(map) :: {:ok, map} | {:error, map}
|
||||||
def insert(params) do
|
def insert(params) do
|
||||||
params = Map.put(params, "last_reply_id", Map.get(params, "user_id"))
|
Repo.transaction(fn ->
|
||||||
insert_changeset(%Data.Thread{}, params)
|
params = Map.put(params, "last_reply_id", Map.get(params, "user_id"))
|
||||||
|> Repo.insert
|
{:ok, thread} = insert_changeset(%Data.Thread{}, params)
|
||||||
|> remove_associations
|
|> Repo.insert
|
||||||
|> Data.Util.process_insert_or_update
|
|
||||||
end
|
|
||||||
|
|
||||||
# update thread for user permission
|
{:ok, _} = Repo.insert(%Data.Reply{
|
||||||
@spec user_update(map) :: {:ok, map} | {:error, map}
|
thread_id: Map.get(thread, :id),
|
||||||
def user_update(params) do
|
content: Map.get(params, "content"),
|
||||||
id = Map.get(params, "id")
|
user_id: Map.get(params, "user_id")
|
||||||
user_id = Map.get(params, "user_id")
|
})
|
||||||
|
"ok"
|
||||||
if is_nil(id) || is_nil(user_id) do
|
end)
|
||||||
{:error, "Invalid thread"}
|
|
||||||
else
|
|
||||||
Repo.get_by(Data.Thread, %{id: id, user_id: user_id})
|
|
||||||
|> process_user_update(params)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# this doesn't update the 'updated_at' field which is what we want
|
# 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
|
# 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])}
|
defp remove_associations({err, data}), do: {err, Map.drop(data, [:user, :replies, :last_reply])}
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,10 +5,9 @@ defmodule MyApp.Repo.Migrations.CreateThread do
|
|||||||
create table(:thread) do
|
create table(:thread) do
|
||||||
add :title, :string
|
add :title, :string
|
||||||
add :category_id, :integer
|
add :category_id, :integer
|
||||||
add :content, :string, size: 2000
|
|
||||||
add :view_count, :integer
|
add :view_count, :integer
|
||||||
add :user_id, references(:user)
|
add :user_id, references(:user)
|
||||||
add :last_reply_id, :integer # TODO: figure this out
|
add :last_reply_id, :integer
|
||||||
add :sticky, :boolean
|
add :sticky, :boolean
|
||||||
add :locked, :boolean
|
add :locked, :boolean
|
||||||
add :edited, :boolean
|
add :edited, :boolean
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ defmodule MyApp.Repo.Migrations.CreateReply do
|
|||||||
add :thread_id, references(:thread)
|
add :thread_id, references(:thread)
|
||||||
add :content, :string, size: 2000
|
add :content, :string, size: 2000
|
||||||
add :edited, :boolean
|
add :edited, :boolean
|
||||||
add :quote, :boolean
|
add :quote_id, :integer
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user