-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
162 additions
and
57 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { z } from 'zod'; | ||
|
||
/** 반환값이 조건을 만족하지 않으면 오류 throw */ | ||
const throwIf = <T>(value: T, ok: (v: T) => boolean) => { | ||
if (ok(value)) { | ||
return value; | ||
} | ||
throw new Error(`값이 예상과 달리 ${value}입니다`); | ||
}; | ||
|
||
export type Visibility = 'public' | 'private' | 'all' | ||
const roles = ['user', 'cadet', 'librarian', 'staff'] as const; | ||
export type Role = typeof roles[number] | ||
|
||
const fromEnum = (role: number): Role => | ||
throwIf(roles[role], (v) => v === undefined); | ||
|
||
export const toRole = (role: Role): number => | ||
throwIf(roles.indexOf(role), (v) => v === -1); | ||
|
||
export const roleSchema = z.number().int().min(0).max(3) | ||
.transform(fromEnum); |
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,36 +1,122 @@ | ||
import jipDataSource from '~/app-data-source'; | ||
import { BookInfo, Reviews } from '~/entity/entities'; | ||
import { match } from 'ts-pattern'; | ||
import { db } from '~/kysely/mod.ts'; | ||
import { executeWithOffsetPagination } from 'kysely-paginate'; | ||
import { Visibility } from '~/kysely/shared.js'; | ||
import { SqlBool } from 'kysely'; | ||
|
||
export const reviewsRepo = jipDataSource.getRepository(Reviews); | ||
export const bookInfoRepo = jipDataSource.getRepository(BookInfo); | ||
export const bookInfoExistsById = (id: number) => | ||
db.selectFrom('book_info').where('id', '=', id).executeTakeFirst(); | ||
|
||
type ToggleReviewArgs = { | ||
export const getReviewById = (id: number) => | ||
db | ||
.selectFrom('reviews') | ||
.where('id', '=', id) | ||
.select(['userId', 'isDeleted', 'disabled', 'disabledUserId']) | ||
.executeTakeFirst(); | ||
|
||
type SearchOption = { | ||
query: string; | ||
page: number; | ||
perPage: number; | ||
visibility: Visibility; | ||
sort: 'asc' | 'desc'; | ||
}; | ||
|
||
const queryReviews = () => | ||
db | ||
.selectFrom('reviews') | ||
.leftJoin('user', 'user.id', 'reviews.userId') | ||
.leftJoin('book_info', 'book_info.id', 'reviews.bookInfoId') | ||
.select([ | ||
'id', | ||
'userId', | ||
'bookInfoId', | ||
'content', | ||
'createdAt', | ||
'book_info.title', | ||
'user.nickname', | ||
'user.intraId', | ||
]); | ||
|
||
export const searchReviews = ({ | ||
query, | ||
sort, | ||
visibility, | ||
page, | ||
perPage, | ||
}: SearchOption) => { | ||
const searchQuery = queryReviews() | ||
.where('content', 'like', `%${query}%`) | ||
.orderBy('updatedAt', sort); | ||
|
||
const withVisibility = match(visibility) | ||
.with('public', () => searchQuery.where('disabled', '=', false)) | ||
.with('private', () => searchQuery.where('disabled', '=', true)) | ||
.with('all', () => searchQuery) | ||
.exhaustive(); | ||
|
||
return executeWithOffsetPagination(withVisibility, { page, perPage }); | ||
}; | ||
|
||
type InsertOption = { | ||
userId: number; | ||
bookInfoId: number; | ||
content: string; | ||
}; | ||
export const insertReview = ({ userId, bookInfoId, content }: InsertOption) => | ||
db | ||
.insertInto('reviews') | ||
.values({ | ||
userId, | ||
updateUserId: userId, | ||
bookInfoId, | ||
content, | ||
disabled: false, | ||
isDeleted: false, | ||
createdAt: new Date(), | ||
}) | ||
.executeTakeFirst(); | ||
|
||
type DeleteOption = { | ||
reviewsId: number; | ||
deleteUserId: number; | ||
}; | ||
export const deleteReviewById = ({ reviewsId, deleteUserId }: DeleteOption) => | ||
db | ||
.updateTable('reviews') | ||
.where('id', '=', reviewsId) | ||
.set({ deleteUserId, isDeleted: true }) | ||
.executeTakeFirst(); | ||
|
||
type ToggleVisibilityOption = { | ||
reviewsId: number; | ||
userId: number; | ||
disabled: boolean; | ||
disabled: SqlBool; | ||
}; | ||
export const toggleReviewVisibilityById = async ({ | ||
export const toggleVisibilityById = ({ | ||
reviewsId, | ||
userId, | ||
disabled, | ||
}: ToggleReviewArgs) => | ||
reviewsRepo.update(reviewsId, { | ||
disabled: !disabled, | ||
disabledUserId: disabled ? null : userId, | ||
}); | ||
|
||
export const findDisabledReviewById = ({ | ||
id, | ||
}: { | ||
id: number; | ||
}): Promise<Pick<Reviews, 'disabled' | 'disabledUserId'> | null> => | ||
reviewsRepo.findOne({ | ||
select: { disabled: true, disabledUserId: true }, | ||
where: { id }, | ||
}); | ||
type RemoveReviewById = { reviewsId: number; deleteUserId: number }; | ||
export const removeReviewById = async ({ | ||
}: ToggleVisibilityOption) => | ||
db | ||
.updateTable('reviews') | ||
.where('id', '=', reviewsId) | ||
.set({ disabled: !disabled, disabledUserId: userId }) | ||
.executeTakeFirst(); | ||
|
||
type UpdateOption = { | ||
reviewsId: number; | ||
userId: number; | ||
content: string; | ||
}; | ||
|
||
export const updateReviewById = ({ | ||
reviewsId, | ||
deleteUserId, | ||
}: RemoveReviewById) => | ||
reviewsRepo.update(reviewsId, { deleteUserId, isDeleted: true }); | ||
userId, | ||
content, | ||
}: UpdateOption) => | ||
db | ||
.updateTable('reviews') | ||
.where('id', '=', reviewsId) | ||
.set({ content, updateUserId: userId }) | ||
.executeTakeFirst(); |
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