Skip to content

Commit

Permalink
Merge pull request #139 from TeamBookTez/feat/book-exist
Browse files Browse the repository at this point in the history
[feat] Add a method if a review already exists
  • Loading branch information
holmir97 authored Apr 25, 2022
2 parents e9fb302 + ea8ebff commit 264acda
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/controller/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,65 @@ const getBookPostController = async (req: Request, res: Response) => {
}
};

/**
* @서재 중복검사
* @route GET /book/exist/:isbn
* @access private
*/
const getBookExistController = async (req: Request, res: Response) => {
try {
const resData = await bookService.getBookExistService(
req.user.id,
req.params.isbn
);

if (resData === constant.NULL_VALUE) {
return response.basicResponse(
res,
returnCode.BAD_REQUEST,
false,
"필요한 값이 없습니다."
);
}

if (resData === constant.VALUE_ALREADY_EXIST) {
return response.dataResponse(
res,
returnCode.OK,
"이미 추가된 책입니다.",
true,
{ isExist: true }
);
}

if (resData === constant.SUCCESS) {
return response.dataResponse(
res,
returnCode.OK,
"추가할 수 있는 책입니다.",
true,
{ isExist: false }
);
}
} catch (err) {
slack.slackWebhook(req, err.message);
console.error(err.message);
return response.basicResponse(
res,
returnCode.INTERNAL_SERVER_ERROR,
false,
"서버 오류"
);
}
};

const bookController = {
postBookController,
getBookController,
getBookPreController,
getBookPeriController,
getBookPostController,
getBookExistController,
};

export default bookController;
1 change: 1 addition & 0 deletions src/router/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ router.get("/", auth, bookController.getBookController);
router.get("/pre", auth, bookController.getBookPreController);
router.get("/peri", auth, bookController.getBookPeriController);
router.get("/post", auth, bookController.getBookPostController);
router.get("/exist/:isbn", auth, bookController.getBookExistController);

module.exports = router;
57 changes: 56 additions & 1 deletion src/service/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import mongoose from "mongoose";

// library
import constant from "../library/constant";
import { keysToSnake, keysToCamel } from "../library/convertSnakeToCamel";
import {
keysToSnake,
keysToCamel,
toSnakeString,
} from "../library/convertSnakeToCamel";
import { isValidObjectId } from "mongoose";

// model
import User from "../models/User";
Expand Down Expand Up @@ -250,12 +255,62 @@ const getBookPostService = async (userId: string) => {
return { books: books };
};

/**
* @서재 중복검사
* @route GET /book/exist/:isbn
* @access private
*/
// TODO: 책 검사하는 과정에서 isbn이 2개 들어오는지 클라랑 이야기해보기
// TODO: isbn 형식값 검사 필요 고민
const getBookExistService = async (userId: string, isbn: string) => {
// 필요한 값이 없는 경우
if (!userId || !isbn) {
return constant.NULL_VALUE;
}

isbn = isbn.trim();
let isbnOne: string, isbnTwo: string;

// isbn이 2개일 경우, 1개일 경우
if (/\s/.test(isbn)) {
[isbnOne, isbnTwo] = isbn.split(" ");
} else {
isbnOne = isbn;
isbnTwo = "";
}

const reviews = await Review.find(
keysToSnake({
userId,
isDeleted: false,
})
).populate(toSnakeString("bookId"));

const existReview = reviews.filter((review) => {
if (
review.book_id.isbn === isbnOne ||
review.book_id.isbn_sub === isbnOne ||
review.book_id.isbn === isbnTwo ||
review.book_id.isbn_sub === isbnTwo
) {
return review;
}
});

if (existReview.length > 0) {
return constant.VALUE_ALREADY_EXIST;
}

return constant.SUCCESS;
};

const bookService = {
postBookService,
getBookService,
getBookPreService,
getBookPeriService,
getBookPostService,
getBookExistService,
};

export default bookService;

0 comments on commit 264acda

Please sign in to comment.