Skip to content

Commit

Permalink
refactor: get tips api uses TipRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-garden committed Jun 18, 2020
1 parent 9050acf commit 3cc88c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/api/one/scheme/tip/get-tips.action/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TipAttrs, TipListResponse } from '@/database/models/tip'

import { Action } from './interface'
import { OneApiFunc } from '@/api/one/types'
import { TipRepository } from '@/database/repository/tip-repository'
import { oneApiResponse } from '@/api/one/utils'
import prisma from '@/modules/prisma'

Expand All @@ -12,32 +13,9 @@ const func: OneApiFunc<Action> = async (data) => {

const totalCount = await prisma.tip.count()
if (topic === null) {
tipList = await prisma.tip.findMany({
where: {
isRemoved: false,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
take: -pageSize,
skip: (page - 1) * pageSize,
orderBy: { createdAt: 'desc' },
})
tipList = await TipRepository.findAll(pageSize, page)
} else {
tipList = await prisma.tip.findMany({
where: {
topic: topic,
isRemoved: false,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
take: -pageSize,
skip: (page - 1) * pageSize,
orderBy: { createdAt: 'desc' },
})
tipList = await TipRepository.findByTopic(topic, pageSize, page)
}

const tips = tipList.map((item) => {
Expand Down
38 changes: 38 additions & 0 deletions src/database/repository/tip-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TipAttrs, TipResponse } from '../models/tip'

import { TipTopic } from '@prisma/client'
import prisma from '@/modules/prisma'

export class TipRepository {
Expand All @@ -15,4 +16,41 @@ export class TipRepository {
})
return tip
}

static async findByTopic(
topic: TipTopic,
pageSize: number,
page: number
): Promise<TipAttrs[]> {
const tipList = await prisma.tip.findMany({
where: {
isRemoved: false,
topic: topic,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
take: -pageSize,
skip: (page - 1) * pageSize,
orderBy: { createdAt: 'desc' },
})
return tipList
}

static async findAll(pageSize: number, page: number): Promise<TipAttrs[]> {
const tipList = await prisma.tip.findMany({
where: {
isRemoved: false,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
take: -pageSize,
skip: (page - 1) * pageSize,
orderBy: { createdAt: 'desc' },
})
return tipList
}
}

0 comments on commit 3cc88c6

Please sign in to comment.