1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-09 16:42:49 +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

@@ -1,2 +1,4 @@
export * from './category';
export * from './reply';
export * from './thread';
export * from './user';

10
client/app/model/reply.ts Normal file
View File

@@ -0,0 +1,10 @@
export interface RepyModel {
content: string;
edited: boolean;
id: number;
inserted_at: string;
quote: boolean;
thread_id: number;
updated_at: string;
user_id: number;
}

View File

@@ -0,0 +1,20 @@
import { RepyModel } from './reply';
export interface ThreadModel {
category_id: number;
content: string;
title: string;
edited: boolean;
id: number;
inserted_at: string;
last_reply: { id: number; battletag: string };
last_reply_id: number;
locked: boolean;
replies: RepyModel[];
reply_count: number;
sticky: boolean;
updated_at: string;
user: { id: number; battletag: string };
user_id: number;
view_count: number;
}

View File

@@ -1,18 +1,32 @@
import React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom';
import { get } from 'lodash';
import { ThreadService } from '../../services';
import { LoginButton } from '../../components';
import { ThreadModel } from '../../model';
import './forum.scss';
interface Props extends RouteComponentProps<any> {}
interface State {}
interface State {
threads: ThreadModel[];
}
export class Forum extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
threads: [],
};
}
componentDidMount() {
this.getThreads();
}
private async getThreads() {
const threads = await ThreadService.getCategoryThreads(this.props.match.params['id']);
this.setState({ threads });
}
renderHeader() {
@@ -59,15 +73,15 @@ export class Forum extends React.Component<Props, State> {
}
renderThreadRows() {
return Array(40).fill('test 123').map((thread, index) => {
return this.state.threads.map((thread, index) => {
return (
<div className={`forum-row ${index % 2 === 0 && 'forum-row--dark'}`} key={index}>
{this.renderCell(thread, { maxWidth: '50px' })}
{this.renderCell(<Link to="#">This is a test subject that could be really long</Link>, { minWidth: '200px' })}
{this.renderCell(<b>thread</b>, { maxWidth: '150px' })}
{this.renderCell(<b>thread</b>, { maxWidth: '150px' }, true)}
{this.renderCell(<b>thread</b>, { maxWidth: '150px' }, true)}
{this.renderCell(<span>by <b>thread</b></span>, { maxWidth: '200px' })}
{this.renderCell('flag', { maxWidth: '50px' })}
{this.renderCell(<Link to={`/thread/${thread.id}`}>{thread.title}</Link>, { minWidth: '200px' })}
{this.renderCell(<b>{thread.user.battletag}</b>, { maxWidth: '150px' })}
{this.renderCell(<b>{thread.reply_count}</b>, { maxWidth: '150px' }, true)}
{this.renderCell(<b>{thread.view_count}</b>, { maxWidth: '150px' }, true)}
{this.renderCell(<span>by <b>{get(thread, 'last_reply.battletag')}</b></span>, { maxWidth: '200px' })}
</div>
);
});

View File

@@ -3,5 +3,6 @@ export * from './home/home';
export * from './not-found/not-found';
export * from './oauth/oauth';
export * from './realms/realms';
export * from './thread/thread';
export * from './user-account/user-account';

View File

@@ -0,0 +1,25 @@
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
import { ThreadService } from '../../services';
interface Props extends RouteComponentProps<any> {}
interface State {}
export class Thread extends React.Component<Props, State> {
componentDidMount() {
this.getThreads();
}
private async getThreads() {
const thread = await ThreadService.getThread(this.props.match.params['id']);
console.log(thread);
}
render() {
return (
<div></div>
);
}
}

View File

@@ -2,7 +2,7 @@ import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { Provider } from 'mobx-react';
import { Footer, Header } from './components';
import { Forum, Home, NotFound, Oauth, Realms, UserAccount } from './pages';
import { Forum, Home, NotFound, Oauth, Realms, Thread, UserAccount } from './pages';
import { stores } from './stores/stores';
// styling
@@ -26,6 +26,7 @@ export class Routes extends React.Component<Props, State> {
<Route exact path="/f/:id" component={Forum} />
<Route exact path="/oauth" component={Oauth} />
<Route exact path="/user-account" component={UserAccount} />
<Route exact path="/thread/:id" component={Thread} />
<Route component={NotFound} />
</Switch>
<Footer />

View File

@@ -1,2 +1,3 @@
export * from './category.service';
export * from './thread.service';
export * from './user.service';

View File

@@ -0,0 +1,29 @@
import axios from '../axios/axios';
import { ThreadModel } from '../model';
const getCategoryThreads = async (category_id: string): Promise<ThreadModel[]> => {
try {
const res = await axios.get(`/api/thread?category_id=${category_id}`);
return res.data.data;
} catch (e) {
console.error(e);
}
return [];
};
const getThread = async (thread_id: string | number): Promise<ThreadModel> => {
try {
const res = await axios.get(`/api/thread/${thread_id}`);
return res.data.data;
} catch (e) {
console.error(e);
}
return [] as any;
};
export const ThreadService = {
getCategoryThreads,
getThread,
};

View File

@@ -13,6 +13,7 @@
"license": "MIT",
"dependencies": {
"@types/lodash": "^4.14.92",
"@types/node": "^9.3.0",
"@types/query-string": "^5.0.1",
"@types/react": "^16.0.34",
"@types/react-dom": "^16.0.3",

View File

@@ -14,6 +14,10 @@
version "8.5.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.5.5.tgz#6f9e8164ae1a55a9beb1d2571cfb7acf9d720c61"
"@types/node@^9.3.0":
version "9.3.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.3.0.tgz#3a129cda7c4e5df2409702626892cb4b96546dd5"
"@types/query-string@^5.0.1":
version "5.0.1"
resolved "https://registry.yarnpkg.com/@types/query-string/-/query-string-5.0.1.tgz#6cb41c724cb1644d56c2d1dae7c7b204e706b39e"

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

View File

@@ -24,12 +24,12 @@ defmodule MyAppWeb.BattleNetController do
# TODO: cache this end point
def characters(conn, _params) do
token = conn
%{"access_token" => token, "id" => user_id} = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.get("access_token")
|> Map.take(["access_token", "id"])
{output, status} = token
|> BattleNet.User.get_user_characters
{output, status} = user_id
|> BattleNet.User.get_user_characters(token)
|> Response.put_resp
conn

View File

@@ -0,0 +1,9 @@
defmodule MyApp.Repo.Migrations.ThreadReplyCount do
use Ecto.Migration
def change do
alter table(:thread) do
add :reply_count, :integer
end
end
end

View File

@@ -0,0 +1,55 @@
defmodule MyAppWeb.ReplyControllerTest do
use MyAppWeb.ConnCase, async: true
import MyApp.Data.TestHelpers
test "insert new reply should fail" do
{:ok, user} = new_user()
new_conn = build_conn()
|> put_req_header("authorization", "Bearer " <> user.token)
conn = post(new_conn, "/api/reply")
body = conn |> response(400) |> Poison.decode!
assert body["error"]["message"] == [
%{"thread_id" => "can't be blank"},
%{"content" => "can't be blank"},
]
conn = post(new_conn, "/api/reply", %{"content" => "t"})
body = conn |> response(400) |> Poison.decode!
assert body["error"]["message"] == [%{"thread_id" => "can't be blank"}]
conn = post(new_conn, "/api/reply", %{"content" => "t", "thread_id" => 1})
body = conn |> response(400) |> Poison.decode!
assert body["error"]["message"] == [%{"thread_id" => "does not exist"}]
end
test "insert new reply should succeed" do
{:ok, user} = new_user()
new_conn = build_conn()
|> put_req_header("authorization", "Bearer " <> user.token)
# insert new thread first
conn = post(new_conn, "/api/thread", %{"title" => "t", "category_id" => 1, "content" => "t"})
body = conn |> response(200) |> Poison.decode!
conn = post(new_conn, "/api/reply", %{"content" => "c", "thread_id" => body["data"]["id"]})
body = conn |> response(200) |> Poison.decode!
data = body["data"]
user_id = data["user_id"]
assert data["content"] == "c"
assert data["edited"] == false
assert data["quote"] == false
# make sure thread reply count and last reply id are updated
conn = get(new_conn, "/api/thread?category_id=1")
body = conn |> response(200) |> Poison.decode!
data = Enum.at(body["data"], 0)
assert data["reply_count"] == 1
assert data["last_reply_id"] == user_id
end
end