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

cache character end point - update reply count/thread reply id

This commit is contained in:
2018-01-09 23:24:09 -06:00
parent 2d6ff0875f
commit d3d3bef9a5
17 changed files with 226 additions and 22 deletions

View File

@@ -28,16 +28,28 @@ defmodule MyApp.BattleNet.User do
end
end
@spec get_user_characters(String.t) :: {:ok, map} | {:error, any}
def get_user_characters(access_token) do
access_token
|> resource_url("wow/user/characters")
|> HTTPoison.get
|> parse_character_response
# end point is cached for one minute per user
@spec get_user_characters(integer, String.t) :: {:ok, map} | {:error, any}
def get_user_characters(user_id, access_token) do
case Cachex.get(:myapp, "usr_char:#{user_id}") do
{:ok, data} -> {:ok, data}
{:missing, _} ->
access_token
|> resource_url("wow/user/characters")
|> HTTPoison.get
|> parse_character_response(user_id)
end
end
defp parse_character_response({:error, error}), do: {:error, error}
defp parse_character_response({:ok, %HTTPoison.Response{body: body}}), do: Poison.decode(body)
defp parse_character_response({:error, error}, _), do: {:error, error}
defp parse_character_response({:ok, %HTTPoison.Response{body: body}}, user_id) do
case Poison.decode(body) do
{:ok, data} ->
Cachex.set(:myapp, "usr_char:#{user_id}", data, ttl: :timer.minutes(1)) # 1 minute
{:ok, data}
{:error, error} -> {:error, error}
end
end
defp resource_url(access_token, path) do
"#{api_url()}/#{path}?access_token=#{access_token}"

View File

@@ -1,6 +1,7 @@
defmodule MyApp.Data.Reply do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias MyApp.Repo
alias MyApp.Data
@@ -35,6 +36,20 @@ defmodule MyApp.Data.Reply do
insert_changeset(%Data.Reply{}, params)
|> Repo.insert
|> Data.Util.process_insert_or_update
|> update_thread_new_reply
end
defp update_thread_new_reply({:error, error}), do: {:error, error}
defp update_thread_new_reply({:ok, reply}) do
thread_id = Map.get(reply, :thread_id)
user_id = Map.get(reply, :user_id)
query = from t in Data.Thread, where: t.id == ^thread_id,
update: [set: [last_reply_id: ^user_id], inc: [reply_count: 1]]
case Repo.update_all(query, []) do
nil -> {:error, "update thread error"}
_ -> {:ok, reply}
end
end
@spec user_update(map) :: {:ok, map} | {:error, map}

View File

@@ -16,6 +16,7 @@ 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: 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
@@ -24,7 +25,7 @@ defmodule MyApp.Data.Thread do
defp insert_changeset(thread, params \\ %{}) do
thread
|> cast(params, [:title, :category_id, :content, :user_id])
|> cast(params, [:title, :category_id, :content, :user_id, :last_reply_id])
|> validate_required([:title, :category_id, :content, :user_id])
|> foreign_key_constraint(:category_id)
|> foreign_key_constraint(:user_id)
@@ -66,6 +67,9 @@ defmodule MyApp.Data.Thread do
:edited,
:content,
:category_id,
:title,
:view_count,
:reply_count,
user: [:id, :battletag],
last_reply: [:id, :battletag],
]),
@@ -85,6 +89,7 @@ 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