mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 17:42:48 +00:00
lots of things done: thread - reply - category - seeds
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user