-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from payw-org/refactor/tip-api
refactor/tip api
- Loading branch information
Showing
10 changed files
with
131 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |