Skip to content

Commit

Permalink
refactor: create tip 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 3cc88c6 commit ad2f584
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
27 changes: 9 additions & 18 deletions src/api/one/scheme/tip/create-tip.action/function.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
import { Action } from './interface'
import { OneApiFunc } from '@/api/one/types'
import Time from '@/modules/time'
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, title, topic, body } = data
const { userId } = authPayload

const currentTime = Time.getIsoString()
const user = await prisma.user.findOne({
where: {
id: userId,
},
})

const tip = await prisma.tip.create({
data: {
user: {
connect: {
id: userId,
},
},
title,
topic,
body,
randomNickname: user.randomNickname,
createdAt: currentTime,
editedAt: currentTime,
},
})
const tipId = await TipRepository.create(
userId,
title,
topic,
body,
user.randomNickname
)

return oneApiResponse<Action>({ tipId: tip.id })
return oneApiResponse<Action>({ tipId })
}

export default func
29 changes: 28 additions & 1 deletion src/database/repository/tip-repository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import { TipAttrs, TipResponse } from '../models/tip'
import { Tip, TipAttrs } from '../models/tip'

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

export class TipRepository {
static async create(
userId: number,
title: string,
topic: TipTopic,
body: string,
randomNick: string
): Promise<number> {
const currentTime = Time.getIsoString()
const tip = await prisma.tip.create({
data: {
user: {
connect: {
id: userId,
},
},
title,
topic,
body,
randomNickname: randomNick,
createdAt: currentTime,
editedAt: currentTime,
},
})
return tip.id
}

static async findById(tipId: number): Promise<TipAttrs> {
const tip = await prisma.tip.findOne({
where: {
Expand Down

0 comments on commit ad2f584

Please sign in to comment.