-
Notifications
You must be signed in to change notification settings - Fork 2
[BMOL-30] 책 삭제 기능 - DELETE v1/library/mylist/:bookId 구현 #24
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
29254f2
a9a5523
77a6cb6
c0a5617
1ba9722
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,22 @@ class BookShelfRepository { | |
| .select(); | ||
| } | ||
|
|
||
| async getBookByUserId(userId) { | ||
| return await pgClient("tbl_mybook") | ||
| .where({ | ||
| user_id: userId, | ||
| }) | ||
| .select(); | ||
| } | ||
|
|
||
| async getBookByBookId(bookId) { | ||
| return await pgClient("tbl_mybook") | ||
| .where({ | ||
| id: bookId, | ||
| }) | ||
| .select(); | ||
| } | ||
|
|
||
| async registerBook(book) { | ||
| const { | ||
| user_id, | ||
|
|
@@ -46,6 +62,18 @@ class BookShelfRepository { | |
|
|
||
| return await query; | ||
| } | ||
|
|
||
| async deleteBook(book) { | ||
| const { user_id, book_id, meta } = book; | ||
| const query = pgClient("tbl_mybook") | ||
| .where({ | ||
| user_id: user_id, | ||
| id: book_id, | ||
| }) | ||
| .del(); | ||
|
|
||
| return await query; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에서 언급한 것처럼 이 쿼리의 결과 값이 1이상인 경우에 성공이라면 요렇게 성공/실패에 대한 bool을 리턴하는것이 좋습니다 |
||
| } | ||
|
Comment on lines
+66
to
+75
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete의 경우 진짜 row를 삭제하는 경우는 거의 없습니다. (삭제한 내역도 결국 데이터이고, history기록이 되어야하니까요)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 처음엔 delete에서 row를 삭제했는데 deleted_at에 업데이트 하는 방향으로 수정했어요 ~ |
||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 두번으로 나눠서 할 필요없이 where절에 둘다 넣어서 한번의 query로 찾으면 될것 같네요..