Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/controllers/bookShelfController.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ const deleteBookController = async (ctx) => {
now: +new Date(),
},
};
ctx.body = await bookShelfService.deleteBook(bookData);
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 = {
Expand Down
10 changes: 4 additions & 6 deletions src/dao/bookShelfDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ class BookShelfDao {

const { user_id, book_id } = book;

const checkUser = await bookShelfRepo.getBookByUserId(user_id);
if (checkUser.length === 0) {
throw new InvalidUUID(user_id);
}

const checkBook = await bookShelfRepo.getBookByBookId(book_id);
const checkBook = await bookShelfRepo.getBookByUserIdAndBookId(
user_id,
book_id
);
if (checkBook.length === 0) {
throw new MyBookNotFound(book_id);
}
Expand Down
17 changes: 8 additions & 9 deletions src/dao/repositories/bookShelfRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@ class BookShelfRepository {
user_id: userId,
isbn: isbn,
})
.whereNull("deleted_at")
.select();
}

async getBookByUserId(userId) {
async getBookByUserIdAndBookId(userId, bookId) {
return await pgClient("tbl_mybook")
.where({
user_id: userId,
})
.select();
}

async getBookByBookId(bookId) {
return await pgClient("tbl_mybook")
.where({
id: bookId,
})
.whereNull("deleted_at")
.select();
}

Expand Down Expand Up @@ -65,12 +60,16 @@ class BookShelfRepository {

async deleteBook(book) {
const { user_id, book_id, meta } = book;
const currentTimestamp = new Date().toISOString();

const query = pgClient("tbl_mybook")
.where({
user_id: user_id,
id: book_id,
})
.del();
.update({
deleted_at: currentTimestamp,
});

return await query;
Copy link
Collaborator

Choose a reason for hiding this comment

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

위에서 언급한 것처럼 이 쿼리의 결과 값이 1이상인 경우에 성공이라면

const result = await query
return (result > 0)

요렇게 성공/실패에 대한 bool을 리턴하는것이 좋습니다

}
Comment on lines +66 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete의 경우 진짜 row를 삭제하는 경우는 거의 없습니다. (삭제한 내역도 결국 데이터이고, history기록이 되어야하니까요)
이경우 deleted_at의 시간을 업데이트 해주면 되고, get 할때 query에서 deleted_at이 null인 row는 제외 하도록 query하면 됩니다.

Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 처음엔 delete에서 row를 삭제했는데 deleted_at에 업데이트 하는 방향으로 수정했어요 ~

Expand Down