Skip to content

Commit

Permalink
Merge pull request #55 from payw-org/refactor/tip-api
Browse files Browse the repository at this point in the history
refactor/tip api
  • Loading branch information
jhaemin authored Jun 19, 2020
2 parents 17a9e06 + 8dcdc12 commit ee12d3f
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 53 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
6 changes: 1 addition & 5 deletions src/api/one/scheme/tip/delete-tip.action/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const func: OneApiFunc<Action> = async (data) => {
return oneApiResponse<Action>(OneApiError.FORBIDDEN)
}

await prisma.tip.update({
where: { id: tipId },
data: { isRemoved: true },
})

Tip.delete(tipId)
return oneApiResponse<Action>({ isRemoved: true })
}

Expand Down
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
24 changes: 9 additions & 15 deletions src/api/one/scheme/tip/get-tips.action/function.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
import { TipAttrs, TipListResponse } from '@/database/models/tip'

import { Action } from './interface'
import { OneApiFunc } from '@/api/one/types'
import { TipListResponse } from '@/database/models/tip'
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 { topic, page } = data
const pageSize = 10
let tipList: TipAttrs[]

const totalCount = await prisma.tip.count()

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

const tips = tipList.map((item) => {
const response: TipListResponse = {
Expand Down
2 changes: 1 addition & 1 deletion src/api/one/scheme/tip/get-tips.action/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TipTopic } from '@prisma/client'

export type Action = OneApiActionTemplate<
AuthRequired<{
topic: TipTopic
topic: TipTopic | null
page: number
}>,
OneApiActionTemplatePayload<
Expand Down
2 changes: 1 addition & 1 deletion src/api/one/scheme/tip/get-topics.action/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import {
} from '@/api/one/types'

export type Action = OneApiActionTemplateWithoutReqeustData<
OneApiActionTemplatePayload<OneApiError, { [key: string]: string }>
OneApiActionTemplatePayload<OneApiError, Record<string, string>>
>
2 changes: 1 addition & 1 deletion src/api/one/scheme/tip/update-tip.action/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const func: OneApiFunc<Action> = async (data) => {

Tip.renew(tipId, updateBody)

return oneApiResponse<Action>({ isUpdate: true })
return oneApiResponse<Action>({ isUpdated: true })
} catch (err) {
return oneApiResponse<Action>(OneApiError.INTERNAL_SERVER_ERROR)
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/one/scheme/tip/update-tip.action/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export type Action = OneApiActionTemplate<
title: string
body: string
}>,
OneApiActionTemplatePayload<OneApiError, { isUpdate: boolean }>
OneApiActionTemplatePayload<OneApiError, { isUpdated: boolean }>
>
25 changes: 24 additions & 1 deletion src/database/models/tip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataTypes, Model } from 'sequelize'
import { TipBookmark, TipLike, TipTopic } from '@prisma/client'

import { PrimaryAIAttribute } from '../utils/model-attributes'
import { TipTopic } from '@prisma/client'
import { createInitModelFunction } from '../create-init-model'
import prisma from '@/modules/prisma'

Expand All @@ -16,6 +16,21 @@ export type TipUpdateBody = {
body: string
}

export type TipAttrs = {
id: number
topic: TipTopic
userId: number
title: string
body: string
isRemoved: boolean
randomNickname: string
isArchived: boolean
createdAt: Date
editedAt: Date
tipLikes: TipLike[]
tipBookmarks: TipBookmark[]
}

export type TipResponse = {
id: number
topic: TipTopic
Expand Down Expand Up @@ -144,6 +159,14 @@ export class Tip extends Model {
})
return true
}

static async delete(tipId: number): Promise<boolean> {
await prisma.tip.update({
where: { id: tipId },
data: { isRemoved: true },
})
return true
}
}

export const getTip = createInitModelFunction(Tip, 'tip', {
Expand Down
82 changes: 82 additions & 0 deletions src/database/repository/tip-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Time from '@/modules/time'
import { TipAttrs } from '../models/tip'
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: {
id: tipId,
},
include: {
tipLikes: true,
tipBookmarks: true,
},
})
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 ee12d3f

Please sign in to comment.