Skip to content

Commit

Permalink
refactor: /books 경로 정리
Browse files Browse the repository at this point in the history
#767 (comment)
#767 (comment)

Co-authored-by: Jeong Jihwan <[email protected]>
Co-authored-by: jwoo <[email protected]>
  • Loading branch information
3 people committed Sep 14, 2023
1 parent d2916a3 commit c95d0de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 168 deletions.
123 changes: 23 additions & 100 deletions contracts/src/books/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,38 @@ import {
searchAllBooksQuerySchema,
searchAllBooksResponseSchema,
searchBookByIdResponseSchema,
searchBookInfoCreateQuerySchema,
searchBookInfoCreateResponseSchema,
createBookBodySchema,
createBookResponseSchema,
categoryNotFoundSchema,
pubdateFormatErrorSchema,
insertionFailureSchema,
isbnNotFoundSchema,
naverBookNotFoundSchema,
updateBookBodySchema,
updateBookResponseSchema,
unknownPatchErrorSchema,
nonDataErrorSchema,
searchAllBookInfosQuerySchema,
searchBookInfosByTagQuerySchema,
searchBookInfosResponseSchema,
searchBookInfosSortedQuerySchema,
searchBookInfosSortedResponseSchema,
searchBookInfoByIdResponseSchema,
updateDonatorBodySchema,
updateDonatorResponseSchema,
searchBookInfoByIdPathSchema,
searchBookByIdParamSchema,
} from './schema';
import {
badRequestSchema,
bookInfoNotFoundSchema,
bookNotFoundSchema,
} from '../shared';
import { badRequestSchema, bookInfoNotFoundSchema, bookNotFoundSchema } from '../shared';

const c = initContract();

export const booksContract = c.router(
export const bookMetasContracts = c.router(
{
// searchAllBookInfos: {
// method: 'GET',
// path: '/info/search',
// description: '책 정보(book_info)를 검색하여 가져온다.',
// query: searchAllBookInfosQuerySchema,
// responses: {
// 200: searchBookInfosResponseSchema,
// 400: badRequestSchema,
// },
// },
searchBookInfosByTag: {
method: 'GET',
path: '/info/tag',
description: '똑같은 내용의 태그가 달린 책의 정보를 검색하여 가져온다.',
query: searchBookInfosByTagQuerySchema,
responses: {
200: searchBookInfosResponseSchema,
400: badRequestSchema,
},
},
searchBookInfosSorted: {
method: 'GET',
path: '/info/sorted',
description:
'책 정보를 기준에 따라 정렬한다. 정렬기준이 popular일 경우 당일으로부터 42일간 인기순으로 한다.',
query: searchBookInfosSortedQuerySchema,
responses: {
200: searchBookInfosSortedResponseSchema,
400: badRequestSchema,
},
},
searchBookInfoById: {
getById: {
method: 'GET',
path: '/info/:id',
pathParams: searchBookInfoByIdPathSchema,
description: 'book_info테이블의 ID기준으로 책 한 종류의 정보를 가져온다.',
description: '도서 정보를 조회합니다.',
responses: {
200: searchBookInfoByIdResponseSchema,
404: bookInfoNotFoundSchema,
},
},
searchAllBooks: {
method: 'GET',
path: '/search',
description: '개별 책 정보(book)를 검색하여 가져온다. 책이 대출할 수 있는지 확인 할 수 있음',
query: searchAllBooksQuerySchema,
responses: {
200: searchAllBooksResponseSchema,
400: badRequestSchema,
},
},
searchBookInfoForCreate: {
method: 'GET',
path: '/create',
description: '책 생성을 위해 국립중앙도서관에서 ISBN으로 검색한 뒤에 책정보를 반환',
query: searchBookInfoCreateQuerySchema,
responses: {
200: searchBookInfoCreateResponseSchema,
303: isbnNotFoundSchema,
310: naverBookNotFoundSchema,
},
},
searchBookById: {
},
{ pathPrefix: '/bookmetas' },
);

export const booksContract = c.router(
{
getById: {
method: 'GET',
path: '/:id',
description: 'book테이블의 ID기준으로 책 한 종류의 정보를 가져온다.',
Expand All @@ -109,22 +44,20 @@ export const booksContract = c.router(
404: bookNotFoundSchema,
},
},
// createBook: {
// method: 'POST',
// path: '/create',
// description: '책 정보를 생성한다. bookInfo가 있으면 book에만 insert한다.',
// body: createBookBodySchema,
// responses: {
// 200: createBookResponseSchema,
// 308: insertionFailureSchema,
// 309: categoryNotFoundSchema,
// 311: pubdateFormatErrorSchema,
// },
// },
updateBook: {
get: {
method: 'GET',
path: '/',
description: '개별 책 정보(book)를 검색하여 가져온다. 책이 대출할 수 있는지 확인 할 수 있음',
query: searchAllBooksQuerySchema,
responses: {
200: searchAllBooksResponseSchema,
400: badRequestSchema,
},
},
patch: {
method: 'PATCH',
path: '/update',
description: '책 정보를 수정합니다. book_info table or book table',
description: '책 정보 하나를 수정합니다.',
body: updateBookBodySchema,
responses: {
204: updateBookResponseSchema,
Expand All @@ -133,16 +66,6 @@ export const booksContract = c.router(
311: pubdateFormatErrorSchema,
},
},
updateDonator: {
method: 'PATCH',
path: '/donator',
description: '기부자 정보를 수정합니다.',
body: updateDonatorBodySchema,
responses: {
204: updateDonatorResponseSchema,
404: bookNotFoundSchema,
},
},
},
{ pathPrefix: '/books' },
);
68 changes: 0 additions & 68 deletions contracts/src/books/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,12 @@ export const searchAllBookInfosQuerySchema = commonQuerySchema.extend({
category: z.string().optional(),
});

export const searchBookInfosByTagQuerySchema = commonQuerySchema.extend({
query: z.string(),
sort: z.enum(['new', 'popular', 'title']).default('new'),
category: z.string().optional(),
});

export const searchBookInfosSortedQuerySchema = z.object({
sort: z.enum(['new', 'popular']),
limit: nonNegativeInt.default(10).openapi({ example: 10 }),
});

export const searchBookInfoByIdPathSchema = z.object({
id: nonNegativeInt,
});

export const searchAllBooksQuerySchema = commonQuerySchema;

export const searchBookInfoCreateQuerySchema = z.object({
isbnQuery: z.string().openapi({ example: '9791191114225' }),
});

export const createBookBodySchema = z.object({
title: z.string(),
isbn: z.string(),
Expand Down Expand Up @@ -68,10 +53,6 @@ export const updateBookBodySchema = z.object({
status: statusSchema.optional(),
});

export const updateDonatorBodySchema = z.object({
bookId: nonNegativeInt,
nickname: z.string(),
});

export const bookInfoSchema = z.object({
id: nonNegativeInt,
Expand All @@ -86,31 +67,6 @@ export const bookInfoSchema = z.object({
updatedAt: dateLike,
});

export const searchBookInfosResponseSchema = metaPaginatedSchema(
bookInfoSchema
.extend({
publishedAt: dateLike,
})
.omit({
publisher: true,
}),
).extend({
categories: z.array(
z.object({
name: z.string(),
count: nonNegativeInt,
}),
),
});

export const searchBookInfosSortedResponseSchema = z.object({
items: z.array(
bookInfoSchema.extend({
publishedAt: dateLike,
lendingCnt: nonNegativeInt,
}),
),
});

export const searchBookInfoByIdResponseSchema = bookInfoSchema.extend({
books: z.array(
Expand Down Expand Up @@ -147,20 +103,6 @@ export const searchAllBooksResponseSchema = metaPaginatedSchema(
}),
);

export const searchBookInfoCreateResponseSchema = z.object({
bookInfo: z.object({
title: z.string().openapi({ example: '작별인사' }),
image: z.string().openapi({
example: 'http://image.kyobobook.co.kr/images/book/xlarge/225/x9791191114225.jpg',
}),
author: z.string().openapi({ example: '지은이: 김영하' }),
category: z.string().openapi({ example: '8' }),
isbn: z.string().openapi({ example: '9791191114225' }),
publisher: z.string().openapi({ example: '복복서가' }),
pubdate: z.string().openapi({ example: '20220502' }),
}),
});

export const searchBookByIdResponseSchema = z.object({
id: nonNegativeInt.openapi({ example: 3 }),
bookId: nonNegativeInt.openapi({ example: 3 }),
Expand All @@ -185,20 +127,10 @@ export const searchBookByIdResponseSchema = z.object({

export const updateBookResponseSchema = z.literal('책 정보가 수정되었습니다.');

export const updateDonatorResponseSchema = z.literal('기부자 정보가 수정되었습니다.');

export const createBookResponseSchema = z.object({
callSign: z.string().openapi({ example: 'K23.17.v1.c1' }),
});

export const isbnNotFoundSchema = mkErrorMessageSchema('ISBN_NOT_FOUND').describe(
'국립중앙도서관 API에서 ISBN 검색이 실패하였습니다.',
);

export const naverBookNotFoundSchema = mkErrorMessageSchema('NAVER_BOOK_NOT_FOUND').describe(
'네이버 책검색 API에서 ISBN 검색이 실패',
);

export const insertionFailureSchema = mkErrorMessageSchema('INSERT_FAILURE').describe(
'예상치 못한 에러로 책 정보 insert에 실패함.',
);
Expand Down

0 comments on commit c95d0de

Please sign in to comment.