Alex Xu 8 months ago
parent
commit
e83726945f
3 changed files with 71 additions and 4 deletions
  1. 27 1
      src/controllers/tiktok/index.ts
  2. 28 3
      src/services/tiktokService/index.ts
  3. 16 0
      src/services/types.ts

+ 27 - 1
src/controllers/tiktok/index.ts

@@ -10,6 +10,7 @@ import tiktokService, {
   ShotData,
 } from '../../services/tiktokService'
 import { TiktokData } from '../../db/models/Tiktok'
+import { PaginationData } from '../../services/types'
 
 interface ImportPayload {
   token: string
@@ -47,6 +48,12 @@ export default class TiktokController implements Controller {
       jsonResponseMiddleware,
       this.list as RequestHandler
     )
+    this.router.get(
+      '/page',
+      // apiKeyMiddleware(),
+      jsonResponseMiddleware,
+      this.page as RequestHandler
+    )
     this.router.post(
       '/import',
       // apiKeyMiddleware(),
@@ -80,7 +87,26 @@ export default class TiktokController implements Controller {
   ): void {
     const after = request.query.after
     tiktokService
-      .paginateTweetByOrder(after)
+      .paginateTiktokByOrder(after)
+      .then((tiktok) => {
+        response.jsonSuccess(tiktok)
+      })
+      .catch((e) => {
+        response.status(500).jsonError('Server Error', 1010)
+      })
+  }
+
+  private page(
+    request: Request<any, any, any, { page?: string }>,
+    response: JsonResponse<PaginationData<TiktokData>>,
+    next: NextFunction
+  ): void {
+    let p = parseInt(request.query.page ?? '1')
+    if (isNaN(p)) {
+      p = 1
+    }
+    tiktokService
+      .pageTiktok({ page: p })
       .then((tiktok) => {
         response.jsonSuccess(tiktok)
       })

+ 28 - 3
src/services/tiktokService/index.ts

@@ -6,6 +6,7 @@ import {
   PaginationConnection,
 } from 'sequelize-cursor-pagination'
 import { TiktokData } from '../../db/models/Tiktok'
+import { PaginationData, PaginationOptions } from '../types'
 
 const STATE = 'banana'
 
@@ -134,9 +135,9 @@ let paginate: (
   queryOptions: PaginateOptions<Tiktok>
 ) => Promise<PaginationConnection<Tiktok>>
 
-async function paginateTweetByOrder(
+async function paginateTiktokByOrder(
   after?: string,
-  limit = 24
+  limit = 20
 ): Promise<PaginationConnection<TiktokData>> {
   if (!paginate) {
     paginate = makePaginate(Tiktok)
@@ -165,6 +166,29 @@ async function paginateTweetByOrder(
   return ret
 }
 
+async function pageTiktok(
+  option: Partial<PaginationOptions> = {}
+): Promise<PaginationData<TiktokData>> {
+  const opt = { page: 1, count: 20, ...option }
+  const result = await Tiktok.findAndCountAll({
+    order: [
+      ['order', 'DESC'],
+      ['id', 'DESC'],
+    ],
+    limit: opt.count,
+    offset: (opt.page - 1) * opt.count,
+    where: {
+      disabled: false,
+    },
+  })
+
+  return {
+    data: result.rows.map((row) => row.getData()),
+    total: result.count,
+    ...opt,
+  }
+}
+
 export interface GenerateTiktokResult {
   url: string
   success: boolean
@@ -310,7 +334,8 @@ async function bulkImportShot(
 const tiktokService = {
   STATE,
   generateTweetByPublishAPI,
-  paginateTweetByOrder,
+  paginateTiktokByOrder,
+  pageTiktok,
   bulkGenerateTiktokByPublishAPI,
   makeTweetTop,
   makeTweetDisabled,

+ 16 - 0
src/services/types.ts

@@ -0,0 +1,16 @@
+export interface PaginationOptions {
+  page: number
+  count: number
+}
+
+export interface PaginationData<T> extends PaginationOptions {
+  data: T[]
+  total: number
+}
+
+export const emptyPaginationData: PaginationData<any> = {
+  data: [],
+  total: 0,
+  page: 1,
+  count: 20,
+}