Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV_BE] 도서검색 관련 API 구성 #21

Merged
merged 19 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,063 changes: 1,063 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
"lint": "next lint"
},
"dependencies": {
"@types/axios": "^0.14.0",
"@types/dotenv": "^8.2.0",
"@types/firebase": "^3.2.1",
"@types/node": "20.3.3",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"autoprefixer": "10.4.14",
"axios": "^1.4.0",
"dotenv": "^16.3.1",
"eslint": "8.44.0",
"eslint-config-next": "13.4.7",
"firebase": "^9.23.0",
"next": "13.4.7",
"postcss": "8.4.24",
"react": "18.2.0",
Expand Down
13 changes: 0 additions & 13 deletions src/pages/api/book/getMilLibraryBook.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/pages/api/book/getMilLibraryBook/[keyword].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import {getMilLibraryBookList, initFirebase} from "@/utils/FirebaseUtil";
import {API_DATA} from "@/utils/DataClass";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<API_DATA>
) {
const { keyword } = req.query;

initFirebase();
let data = await getMilLibraryBookList(keyword!.toString());
res.send(data);
}
13 changes: 0 additions & 13 deletions src/pages/api/book/getNaverBook.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/pages/api/book/getNaverBook/[keyword].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import {API_DATA, BOOK_DATA} from "@/utils/DataClass";

import dotenv from "dotenv";
import axios from "axios";

dotenv.config();

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<API_DATA>
) {
const { keyword } = req.query;

const RESULT_DATA: API_DATA = {
RESULT_CODE: 0,
RESULT_MSG: "Ready",
RESULT_DATA: {}
}

const NAVER_API_URL = `https://openapi.naver.com/v1/search/book.json?display=30&start=1&query=${keyword}`
const NAVER_API_HEADER = {
"X-Naver-Client-Id": process.env.NEXT_PUBLIC_NAVER_API_ID,
"X-Naver-Client-Secret": process.env.NEXT_PUBLIC_NAVER_API_SECRET
};

await axios.get(NAVER_API_URL, {
headers: NAVER_API_HEADER
}).then((axiosRes)=>{
let listBook: Array<BOOK_DATA> = [];

axiosRes.data["items"].forEach((book: BOOK_DATA) => {
let tmpBook: BOOK_DATA = {
author: book["author"],
publisher: book["publisher"],
title: book["title"]
}
listBook.push(tmpBook);
});

RESULT_DATA.RESULT_CODE = 200;
RESULT_DATA.RESULT_MSG = "Success";
RESULT_DATA.RESULT_DATA = {data: listBook};
}).catch((error)=>{
RESULT_DATA.RESULT_CODE = 100;
RESULT_DATA.RESULT_MSG = error as string;
});

res.send(RESULT_DATA);
}
15 changes: 15 additions & 0 deletions src/utils/DataClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface API_DATA{
RESULT_CODE: number,
RESULT_MSG: string,
RESULT_DATA: LIST_DATA | object
}

export interface LIST_DATA{
data: Array<BOOK_DATA>
}

export interface BOOK_DATA{
author: string,
publisher: string,
title: string
}
89 changes: 89 additions & 0 deletions src/utils/FirebaseUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {collection, doc, Firestore, getDoc, getDocs, getFirestore} from "@firebase/firestore";
import {FirebaseApp, FirebaseOptions, initializeApp} from "@firebase/app";
import {API_DATA, BOOK_DATA, LIST_DATA} from "@/utils/DataClass";

import dotenv from "dotenv";

dotenv.config();
const firebaseConfig: FirebaseOptions = {
apiKey: process.env.NEXT_PUBLIC_FB_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FB_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FB_PROJECT_ID,
appId: process.env.NEXT_PUBLIC_FB_APP_ID
};

let firebaseApp: FirebaseApp;
let firebaseDB: Firestore;

export const initFirebase = () => {
if(firebaseApp === undefined || firebaseDB === undefined) {
firebaseApp = initializeApp(firebaseConfig);
firebaseDB = getFirestore();
}
};

export const getMilLibraryBookList = async (keyword: string) => {
const RESULT_DATA: API_DATA = {
RESULT_CODE: 0,
RESULT_MSG: "Ready",
RESULT_DATA: {}
}

const fbDocument = await getDocs(collection(firebaseDB, "Book"));
if(fbDocument.empty){
RESULT_DATA.RESULT_CODE = 100;
RESULT_DATA.RESULT_MSG = "No Such Database";
return RESULT_DATA;
}

try{
let cntData = 0;
let listBook: Array<BOOK_DATA> = [];

fbDocument.forEach((curDoc) => {
if(curDoc.get("marc_title").indexOf(keyword) > 0) {
cntData++;
listBook.push({
author: curDoc.get("marc_author"),
publisher: curDoc.get("marc_publisher"),
title: curDoc.get("marc_title")
});
}
});

RESULT_DATA.RESULT_CODE = 200;
RESULT_DATA.RESULT_MSG = "Success";
RESULT_DATA.RESULT_DATA = {data: listBook};
}catch(error){
RESULT_DATA.RESULT_CODE = 100;
RESULT_DATA.RESULT_MSG = error as string;
}

return RESULT_DATA;
}

const getFirebaseDB = async (collectionID: string, documentID: string) => {
const RESULT_DATA: API_DATA = {
RESULT_CODE: 0,
RESULT_MSG: "Ready",
RESULT_DATA: {}
}

const fbDocument = await getDoc(doc(firebaseDB, collectionID, documentID));
if(!fbDocument.exists()){
RESULT_DATA.RESULT_CODE = 100;
RESULT_DATA.RESULT_MSG = "No Such Database";
return RESULT_DATA;
}

try{
RESULT_DATA.RESULT_CODE = 200;
RESULT_DATA.RESULT_MSG = "Success";
RESULT_DATA.RESULT_DATA = fbDocument.data();
}catch(error){
RESULT_DATA.RESULT_CODE = 100;
RESULT_DATA.RESULT_MSG = error as string;
}

return RESULT_DATA;
};