diff --git a/src/api/one/scheme/tip/create-tip.action/function.ts b/src/api/one/scheme/tip/create-tip.action/function.ts index a0d349a5..b14b49e4 100644 --- a/src/api/one/scheme/tip/create-tip.action/function.ts +++ b/src/api/one/scheme/tip/create-tip.action/function.ts @@ -1,6 +1,6 @@ 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' @@ -8,30 +8,21 @@ const func: OneApiFunc = 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({ tipId: tip.id }) + return oneApiResponse({ tipId }) } export default func diff --git a/src/api/one/scheme/tip/delete-tip.action/function.ts b/src/api/one/scheme/tip/delete-tip.action/function.ts index cb147b4d..bdfb4e95 100644 --- a/src/api/one/scheme/tip/delete-tip.action/function.ts +++ b/src/api/one/scheme/tip/delete-tip.action/function.ts @@ -20,11 +20,7 @@ const func: OneApiFunc = async (data) => { return oneApiResponse(OneApiError.FORBIDDEN) } - await prisma.tip.update({ - where: { id: tipId }, - data: { isRemoved: true }, - }) - + Tip.delete(tipId) return oneApiResponse({ isRemoved: true }) } diff --git a/src/api/one/scheme/tip/get-tip-detail.action/function.ts b/src/api/one/scheme/tip/get-tip-detail.action/function.ts index d19e4367..dd8d85d4 100644 --- a/src/api/one/scheme/tip/get-tip-detail.action/function.ts +++ b/src/api/one/scheme/tip/get-tip-detail.action/function.ts @@ -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 = 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(OneApiError.NO_CONTENT) } diff --git a/src/api/one/scheme/tip/get-tips.action/function.ts b/src/api/one/scheme/tip/get-tips.action/function.ts index f6554cab..f5d89f71 100644 --- a/src/api/one/scheme/tip/get-tips.action/function.ts +++ b/src/api/one/scheme/tip/get-tips.action/function.ts @@ -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 = 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 = { diff --git a/src/api/one/scheme/tip/get-tips.action/interface.ts b/src/api/one/scheme/tip/get-tips.action/interface.ts index f33d66ca..850cac5f 100644 --- a/src/api/one/scheme/tip/get-tips.action/interface.ts +++ b/src/api/one/scheme/tip/get-tips.action/interface.ts @@ -10,7 +10,7 @@ import { TipTopic } from '@prisma/client' export type Action = OneApiActionTemplate< AuthRequired<{ - topic: TipTopic + topic: TipTopic | null page: number }>, OneApiActionTemplatePayload< diff --git a/src/api/one/scheme/tip/get-topics.action/interface.ts b/src/api/one/scheme/tip/get-topics.action/interface.ts index 584777f3..4d1d5823 100644 --- a/src/api/one/scheme/tip/get-topics.action/interface.ts +++ b/src/api/one/scheme/tip/get-topics.action/interface.ts @@ -5,5 +5,5 @@ import { } from '@/api/one/types' export type Action = OneApiActionTemplateWithoutReqeustData< - OneApiActionTemplatePayload + OneApiActionTemplatePayload> > diff --git a/src/api/one/scheme/tip/update-tip.action/function.ts b/src/api/one/scheme/tip/update-tip.action/function.ts index c2c758b8..70b123d2 100644 --- a/src/api/one/scheme/tip/update-tip.action/function.ts +++ b/src/api/one/scheme/tip/update-tip.action/function.ts @@ -21,7 +21,7 @@ const func: OneApiFunc = async (data) => { Tip.renew(tipId, updateBody) - return oneApiResponse({ isUpdate: true }) + return oneApiResponse({ isUpdated: true }) } catch (err) { return oneApiResponse(OneApiError.INTERNAL_SERVER_ERROR) } diff --git a/src/api/one/scheme/tip/update-tip.action/interface.ts b/src/api/one/scheme/tip/update-tip.action/interface.ts index 799397d1..94b7de61 100644 --- a/src/api/one/scheme/tip/update-tip.action/interface.ts +++ b/src/api/one/scheme/tip/update-tip.action/interface.ts @@ -11,5 +11,5 @@ export type Action = OneApiActionTemplate< title: string body: string }>, - OneApiActionTemplatePayload + OneApiActionTemplatePayload > diff --git a/src/database/models/tip.ts b/src/database/models/tip.ts index 7833be9f..5b7fc85a 100644 --- a/src/database/models/tip.ts +++ b/src/database/models/tip.ts @@ -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' @@ -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 @@ -144,6 +159,14 @@ export class Tip extends Model { }) return true } + + static async delete(tipId: number): Promise { + await prisma.tip.update({ + where: { id: tipId }, + data: { isRemoved: true }, + }) + return true + } } export const getTip = createInitModelFunction(Tip, 'tip', { diff --git a/src/database/repository/tip-repository.ts b/src/database/repository/tip-repository.ts new file mode 100644 index 00000000..62ecf44f --- /dev/null +++ b/src/database/repository/tip-repository.ts @@ -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 { + 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 { + 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 { + 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 { + 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 + } +}