Skip to content

Commit

Permalink
feat: create tip repository
Browse files Browse the repository at this point in the history
- findById method
  • Loading branch information
lee-garden committed Jun 18, 2020
1 parent e5db8de commit 9050acf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
12 changes: 2 additions & 10 deletions src/api/one/scheme/tip/get-tip-detail.action/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ import { OneApiError, OneApiFunc } from '@/api/one/types'
import { Tip, TipResponse } from '@/database/models/tip'

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

const func: OneApiFunc<Action> = async (data) => {
const { authPayload, tipId } = data
const { userId } = authPayload

try {
const tip = await prisma.tip.findOne({
where: {
id: tipId,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
})
const tip = await TipRepository.findById(tipId)
if (tip === null || tip.isRemoved) {
return oneApiResponse<Action>(OneApiError.NO_CONTENT)
}
Expand Down
7 changes: 1 addition & 6 deletions src/api/one/scheme/tip/get-tips.action/function.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Tip,
TipAttrs,
TipListResponse,
TipResponse,
} from '@/database/models/tip'
import { TipAttrs, TipListResponse } from '@/database/models/tip'

import { Action } from './interface'
import { OneApiFunc } from '@/api/one/types'
Expand Down
1 change: 1 addition & 0 deletions src/database/models/tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type TipAttrs = {
userId: number
title: string
body: string
isRemoved: boolean
randomNickname: string
isArchived: boolean
createdAt: Date
Expand Down
18 changes: 18 additions & 0 deletions src/database/repository/tip-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TipAttrs, TipResponse } from '../models/tip'

import prisma from '@/modules/prisma'

export class TipRepository {
static async findById(tipId: number): Promise<TipAttrs> {
const tip = await prisma.tip.findOne({
where: {
id: tipId,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
})
return tip
}
}

0 comments on commit 9050acf

Please sign in to comment.