Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 13 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"knex": "^2.4.2",
"koa": "^2.14.1",
"koa": "^2.14.2",
"koa-body": "^6.0.1",
"koa-compress": "^5.1.0",
"koa-router": "^12.0.0",
Expand Down
40 changes: 38 additions & 2 deletions src/controllers/bookShelfController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { BookShelfService, MissingRequestParameter } = require("../services");

const bookShelfController = async (ctx) => {
// 책 등록
const registerBookController = async (ctx) => {
const bookShelfService = new BookShelfService();

const {
Expand Down Expand Up @@ -56,6 +57,41 @@ const bookShelfController = async (ctx) => {
ctx.body = await bookShelfService.registerBook(bookData);
};

// 책 삭제
const deleteBookController = async (ctx) => {
const bookShelfService = new BookShelfService();

const { bookId } = ctx.params;

// check if book_id is missing
if (!bookId) {
throw new MissingRequestParameter("bookId");
}

const user_id = ctx.request.headers.user_id;

// check user_id value
if (!user_id) {
throw new MissingRequestParameter("user_id");
}

const bookData = {
user_id,
book_id: bookId,
meta: {
requestId: ctx.state.requestId,
now: +new Date(),
},
};
const result = await bookShelfService.deleteBook(bookData);
if (result === 1) {
ctx.body = "SUCCESS";
} else {
ctx.body = "FAIL";
}
Comment on lines +86 to +91
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 이라는 것은 실패인지 성공인지 직관적으로 알수 없는거라서요렇게 하는 것 보다는 bookShelfService.deleteBook(bookData) 이 method가 성공 했을때는 true 실패시 false를 리턴하도록하고 bool을 비교 하는것이 좋아요.

};

module.exports = {
bookShelfController,
registerBookController,
deleteBookController,
};
27 changes: 26 additions & 1 deletion src/dao/bookShelfDao.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const { DuplicateBook } = require("../services/errorService");
const {
DuplicateBook,
InvalidUUID,
MyBookNotFound,
} = require("../services/errorService");
const { BookShelfRepository } = require("./repositories/bookShelfRepository");

class BookShelfDao {
Expand Down Expand Up @@ -36,6 +40,27 @@ class BookShelfDao {

return updatedRows;
}

async deleteBook(book) {
const bookShelfRepo = new BookShelfRepository();

const { user_id, book_id } = book;

const checkBook = await bookShelfRepo.getBookByUserIdAndBookId(
user_id,
book_id
);
if (checkBook.length === 0) {
throw new MyBookNotFound(book_id);
}

const updatedRows = await bookShelfRepo.deleteBook({
user_id,
book_id,
});

return updatedRows;
}
}

module.exports = { BookShelfDao };
168 changes: 93 additions & 75 deletions src/dao/bookTimerDao.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,155 @@
const { BookHistoryRepository } = require('./repositories/bookHistoryRepository')
const { AccountRepository } = require('./repositories/accountRepository')
const { BookTimerDecorator } = require('./bookTimerDecorator')

const { MyBookNotFound, InternalServerError, BookHistoryNotFound } = require('../services/errorService')
const {
BookHistoryRepository,
} = require("./repositories/bookHistoryRepository");
const { AccountRepository } = require("./repositories/accountRepository");
const { BookTimerDecorator } = require("./bookTimerDecorator");

const {
MyBookNotFound,
InternalServerError,
BookHistoryNotFound,
} = require("../services/errorService");

class BookTimerDao {
constructor(){
this._daoName = 'BookTimerDao'
constructor() {
this._daoName = "BookTimerDao";
this._repo = {
bookHistory: new BookHistoryRepository(),
account: new AccountRepository(),
}
};
}

get daoName(){
return this._daoName
get daoName() {
return this._daoName;
}

async getBookTimerInfoByBookId(bookId){
const bookTimerDecorator = new BookTimerDecorator()
async getBookTimerInfoByBookId(bookId) {
const bookTimerDecorator = new BookTimerDecorator();

const [accountInfo, bookHistoryInfo] = await Promise.all([
this._repo.account.getAccountByBookId(bookId),
this._repo.bookHistory.getBookHistoryListByBookId(bookId)
])
this._repo.bookHistory.getBookHistoryListByBookId(bookId),
]);

// deleted book check
if (!accountInfo || !bookHistoryInfo){
throw new MyBookNotFound(bookId)
if (!accountInfo || !bookHistoryInfo) {
throw new MyBookNotFound(bookId);
}

const bookTimerInfo = bookTimerDecorator.decorateBookTimer(bookId, {accountInfo, bookHistoryInfo})
const bookTimerInfo = bookTimerDecorator.decorateBookTimer(bookId, {
accountInfo,
bookHistoryInfo,
});

const result = {
data : bookTimerInfo
}
return result
data: bookTimerInfo,
};
return result;
}

async postReadingTimeInfo(bookId, reading_time){
const accountRepo = new AccountRepository()
const bookHistoryRepo = new BookHistoryRepository()
async postReadingTimeInfo(bookId, reading_time) {
const accountRepo = new AccountRepository();
const bookHistoryRepo = new BookHistoryRepository();

const accountInfo = await accountRepo.getAccountByBookId(bookId)
const accountInfo = await accountRepo.getAccountByBookId(bookId);
// deleted book check
if (!accountInfo){
throw new MyBookNotFound(bookId)
if (!accountInfo) {
throw new MyBookNotFound(bookId);
}

const postResult = await bookHistoryRepo.insertReadingTimeByBookId(accountInfo.user_id, bookId, reading_time)
const postResult = await bookHistoryRepo.insertReadingTimeByBookId(
accountInfo.user_id,
bookId,
reading_time
);

// postResult check
if (!postResult || !postResult.length < 0){
throw new InternalServerError()
if (!postResult || !postResult.length < 0) {
throw new InternalServerError();
}

const addedBookHistoryResult = await bookHistoryRepo.getBookHistoryByBookHistoryId(postResult[0].id)
const addedBookHistoryResult =
await bookHistoryRepo.getBookHistoryByBookHistoryId(
postResult[0].id
);

// if id of added book history is not in table, throw error
if (!addedBookHistoryResult){
throw new InternalServerError()
if (!addedBookHistoryResult) {
throw new InternalServerError();
}

const result = {
data : addedBookHistoryResult
}
return result
data: addedBookHistoryResult,
};
return result;
}

async deleteReadingTimeByHistoryId(bookId, bookHistoryId) {
const accountRepo = new AccountRepository();
const bookHistoryRepo = new BookHistoryRepository();

async deleteReadingTimeByHistoryId(bookId, bookHistoryId){
const accountRepo = new AccountRepository()
const bookHistoryRepo = new BookHistoryRepository()
const accountInfo = await accountRepo.getAccountByBookId(bookId);
const bookHistoryInfo =
await bookHistoryRepo.getBookHistoryByBookHistoryId(bookHistoryId);

const accountInfo = await accountRepo.getAccountByBookId(bookId)
const bookHistoryInfo = await bookHistoryRepo.getBookHistoryByBookHistoryId(bookHistoryId)

// deleted book check
if (!accountInfo){
throw new MyBookNotFound(bookId)
if (!accountInfo) {
throw new MyBookNotFound(bookId);
}

// deleted book history check
if (!bookHistoryInfo){
throw new BookHistoryNotFound(bookId)
if (!bookHistoryInfo) {
throw new BookHistoryNotFound(bookId);
}

const deleteResults = await bookHistoryRepo.removeReadingTimeByBookHistoryId(bookHistoryId)
const deleteResults =
await bookHistoryRepo.removeReadingTimeByBookHistoryId(
bookHistoryId
);

const removedBookHistoryResult =
await bookHistoryRepo.getBookHistoryByBookHistoryId(bookHistoryId);

const removedBookHistoryResult =
await bookHistoryRepo.getBookHistoryByBookHistoryId(bookHistoryId)

// if id of added book history is in table, throw error
if (removedBookHistoryResult){
throw new InternalServerError()
if (removedBookHistoryResult) {
throw new InternalServerError();
}

const result = {
data : deleteResults
}
return result
data: deleteResults,
};
return result;
}

async deleteReadingTimeByBookId(bookId){
const accountRepo = new AccountRepository()
const bookHistoryRepo = new BookHistoryRepository()
async deleteReadingTimeByBookId(bookId) {
const accountRepo = new AccountRepository();
const bookHistoryRepo = new BookHistoryRepository();

const accountInfo = await accountRepo.getAccountByBookId(bookId);

const accountInfo = await accountRepo.getAccountByBookId(bookId)

// deleted book check
if (!accountInfo){
throw new MyBookNotFound(bookId)
if (!accountInfo) {
throw new MyBookNotFound(bookId);
}

const deleteResults = await bookHistoryRepo.removeReadingTimeByBookId(bookId)

const deleteResults = await bookHistoryRepo.removeReadingTimeByBookId(
bookId
);

//id check
const removedBookResult =
await bookHistoryRepo.getBookHistoryListByBookId(bookId)
const removedBookResult =
await bookHistoryRepo.getBookHistoryListByBookId(bookId);


// if id of added book history is in table, throw error
if(removedBookResult && removedBookResult.length > 0)
throw new InternalServerError()
if (removedBookResult && removedBookResult.length > 0) {
throw new InternalServerError();
}


const result = {
data : deleteResults
}
return result
}

data: deleteResults,
};
return result;
}
}

module.exports = { BookTimerDao }
module.exports = { BookTimerDao };
Loading