-
Notifications
You must be signed in to change notification settings - Fork 3
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 #21 from yymin1022/dev/be-book-api
[DEV_BE] 도서검색 관련 API 구성
- Loading branch information
Showing
8 changed files
with
1,237 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,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); | ||
} |
This file was deleted.
Oops, something went wrong.
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,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); | ||
} |
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,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 | ||
} |
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,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; | ||
}; |