mirror of https://github.com/fantasticit/think.git
server: improve get views
This commit is contained in:
parent
5c4019c102
commit
802f7c9228
|
@ -624,7 +624,7 @@ export class DocumentService {
|
|||
|
||||
const [views, createUser] = await Promise.all([
|
||||
await this.viewService.getDocumentTotalViews(documentId),
|
||||
doc && doc.createUserId ? await this.userService.findById(doc.createUserId) : null,
|
||||
await this.userService.findById(doc.createUserId),
|
||||
]);
|
||||
|
||||
return {
|
||||
|
|
|
@ -33,8 +33,17 @@ export class ViewService {
|
|||
}
|
||||
|
||||
async getDocumentTotalViews(documentId) {
|
||||
const [, total] = await this.viewRepo.findAndCount({ documentId });
|
||||
return total;
|
||||
try {
|
||||
const count = await this.viewRepo.query(
|
||||
`SELECT COUNT(1)
|
||||
FROM view
|
||||
WHERE view.documentId = '${documentId}'
|
||||
`
|
||||
);
|
||||
return count[0]['COUNT(1)'];
|
||||
} catch (e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getDocumentViews(documentId, pagination: IPagination) {
|
||||
|
@ -64,15 +73,14 @@ export class ViewService {
|
|||
}>
|
||||
> {
|
||||
const count = 20;
|
||||
|
||||
const ret = await this.viewRepo.query(
|
||||
`
|
||||
SELECT v.documentId, v.visitedAt FROM (
|
||||
SELECT ANY_VALUE(documentId) as documentId, ANY_VALUE(created_at) as visitedAt
|
||||
FROM view
|
||||
WHERE view.userId = '${userId}'
|
||||
GROUP BY visitedAt
|
||||
ORDER BY visitedAt DESC
|
||||
FROM view
|
||||
WHERE view.userId = '${userId}'
|
||||
GROUP BY visitedAt
|
||||
ORDER BY visitedAt DESC
|
||||
) v
|
||||
GROUP BY v.documentId
|
||||
LIMIT ${count}
|
||||
|
|
Loading…
Reference in New Issue