Skip to content

Commit 9ffe42f

Browse files
committed
new:get top3 threads for APP
1 parent 244f000 commit 9ffe42f

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

Back/model/forum.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,3 @@ export const forumRuntime = async function(req: Request, res: Response) {
9797
res.json({ code: -1, msg: e.message });
9898
}
9999
};
100-
101-
102-
export const forumListTop = async function(req: Request, res: Response) {
103-
req.header('Content-Type');
104-
res.json({ code: 1, msg: 'unfinished'});
105-
}

Back/model/thread.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,94 @@ export const threadInfo = async function(req: Request, res: Response) {
241241
}
242242
};
243243

244+
/*
245+
return msg: {
246+
list: [
247+
{
248+
threadInfo,
249+
threadAuthor,
250+
},
251+
{
252+
threadInfo,
253+
threadAuthor,
254+
},
255+
{
256+
threadInfo,
257+
threadAuthor,
258+
},
259+
]
260+
}
261+
*/
262+
export const threadGetTop3 = async function (req:Request, res: Response) {
263+
try {
264+
verifyJWT(req.header("Authorization"));
265+
266+
let { fid } = req.params;
267+
let topArr: Array<Thread> = [];
268+
let threadTop1 = await prisma.threads({
269+
where: {
270+
top: 1,
271+
forum: {
272+
id: fid
273+
}
274+
},
275+
orderBy: "lastDate_DESC"
276+
})
277+
if (threadTop1.length > 0) {
278+
topArr.push(threadTop1[0]);
279+
} else {
280+
topArr.push(null);
281+
}
282+
let threadMostPost = await prisma.threads({
283+
where: {
284+
top: 0,
285+
forum: {
286+
id: fid
287+
},
288+
active: true
289+
},
290+
orderBy: "postCount_DESC"
291+
})
292+
if (threadMostPost.length > 0) {
293+
topArr.push(threadMostPost[0]);
294+
} else {
295+
topArr.push(null);
296+
}
297+
let threadNewUpdate = await prisma.threads({
298+
where: {
299+
top: 0,
300+
forum: {
301+
id: fid
302+
},
303+
active: true,
304+
},
305+
orderBy: "updatedAt_DESC"
306+
})
307+
if (threadNewUpdate.length > 1) {
308+
if (topArr[1] !== null && threadNewUpdate[0].id === topArr[1].id) {
309+
topArr.push(threadNewUpdate[1]);
310+
} else {
311+
topArr.push(threadNewUpdate[0]);
312+
}
313+
} else {
314+
topArr.push(null);
315+
}
316+
317+
const resultArr = await Promise.all(
318+
topArr.map(async item => {
319+
return item? {
320+
thread: item,
321+
user: filterUserInfo(
322+
await prisma.thread({ id: item.id }).user()
323+
)
324+
}: null;
325+
})
326+
);
327+
res.json({ code: 1, msg: {list: resultArr}});
328+
} catch (e) {
329+
res.json({ code: -1 , msg: e.message});
330+
}
331+
}
244332
export const threadCreate = async function(req: Request, res: Response) {
245333
try {
246334
const {
@@ -713,6 +801,7 @@ export const threadTop = async function(req: Request, res: Response) {
713801
}
714802
};
715803

804+
716805
export const threadClose = async function(req: Request, res: Response) {
717806
try {
718807
const authObj = verifyJWT(req.header("Authorization"));

Back/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
userUpdateJWT
3434
} from "./model/user";
3535
import {
36+
threadGetTop3,
3637
threadDeleteHard,
3738
threadList,
3839
threadInfo,
@@ -56,7 +57,7 @@ import {
5657
postSearch,
5758
postInfo
5859
} from "./model/post";
59-
import { forumList, forumListSimple, forumRuntime, forumListTop } from "./model/forum";
60+
import { forumList, forumListSimple, forumRuntime } from "./model/forum";
6061
import {
6162
reportCreate,
6263
reportInfo,
@@ -192,9 +193,9 @@ app.post("/user/update/jwt", userUpdateJWT);
192193
app.get("/forum/list", forumList);
193194
app.get("/forum/runtime", forumRuntime);
194195
app.get("/forum/listSimple", forumListSimple);
195-
app.get("/forum/listTop", forumListTop);
196196

197197
//Thread
198+
app.get("/thread/top3/:fid", threadGetTop3);
198199
app.get("/thread/list/:fid/:page", threadList);
199200
app.get("/thread/info/:tid/:page", threadInfo);
200201
app.get("/thread/runtime", threadRuntime);

0 commit comments

Comments
 (0)