Skip to content

Commit 033ae7e

Browse files
pokerspokers
and
pokers
authored
[BMOL-25] fixed bugs and refactorying. (#14)
* BMOL-25 fixed bugs and refactorying * BMOL-25 rollback * BMOL-25 align lines, space --------- Co-authored-by: pokers <[email protected]>
1 parent a71c291 commit 033ae7e

File tree

5 files changed

+55
-78
lines changed

5 files changed

+55
-78
lines changed

.eslintrc.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,19 @@ module.exports = {
1111
"ecmaVersion": 2020,
1212
},
1313
"rules": {
14-
}
14+
"max-len": [
15+
"error",
16+
200,
17+
2,
18+
{
19+
"ignoreComments": true,
20+
"ignoreUrls": true,
21+
"ignoreTemplateLiterals": true,
22+
"ignoreRegExpLiterals": true
23+
}
24+
],
25+
"max-classes-per-file": "off",
26+
"default-param-last": "warn",
27+
"no-unused-vars": 0
28+
},
1529
}
+31-43
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
const { BookTimerService } = require('../services')
22
const { MissingRequestParameter, InvalidRequestParameter } = require('../services/errorService')
33

4-
5-
64
const getBookTimer = async (ctx)=>{
75
const {
86
params: {
97
bookId
108
}
119
} = ctx
1210

13-
const inst = new BookTimerService()
14-
15-
if(bookId){
16-
ctx.body = await inst.getBookTimerByBookId(bookId)
11+
if (!bookId){
12+
throw new InvalidRequestParameter('bookId')
1713
}
1814

19-
const deleteReadingTime = async (ctx)=>{
20-
const {
21-
params: {
22-
bookId
23-
}
24-
} = ctx
25-
26-
let bookHistoryId;
15+
const inst = new BookTimerService()
16+
ctx.body = await inst.getBookTimerByBookId(bookId)
17+
}
2718

28-
if (ctx.request.query && ctx.request.query.historyId ){
29-
bookHistoryId = ctx.query.historyId
30-
}
31-
32-
const inst = new BookTimerService()
33-
34-
if(bookId){
35-
ctx.body = await inst.removeReadingTime(bookId, bookHistoryId)
36-
ctx.body.meta = {
37-
requestId: ctx.state.requestId,
38-
now: +new Date(),
39-
}
19+
const deleteReadingTime = async (ctx)=>{
20+
const {
21+
params: {
22+
bookId
4023
}
41-
42-
24+
} = ctx
25+
26+
if (!bookId){
27+
throw new InvalidRequestParameter('bookId')
28+
}
29+
if (!ctx.request.query || !ctx.request.query.historyId ){
30+
throw new InvalidRequestParameter('historyId')
31+
}
32+
33+
const bookHistoryId = ctx.request.query.historyId
34+
const inst = new BookTimerService()
35+
ctx.body = await inst.removeReadingTime(bookId, bookHistoryId)
36+
ctx.body.meta = {
37+
requestId: ctx.state.requestId,
38+
now: +new Date(),
39+
}
4340
}
4441

4542
const postReadingTime = async (ctx)=>{
@@ -49,32 +46,23 @@ const postReadingTime = async (ctx)=>{
4946
}
5047
} = ctx
5148

52-
49+
if (!bookId){
50+
throw new InvalidRequestParameter('bookId')
51+
}
5352
if (!ctx.request.body || (!ctx.request.body.reading_time && ctx.request.body.reading_time != 0)){
5453
throw new MissingRequestParameter("reading_time")
5554
}
56-
57-
const readingTime = ctx.request.body.reading_time
58-
55+
const readingTime = 0 + ctx.request.body.reading_time
5956
if (readingTime < 0){
6057
throw new InvalidRequestParameter("readingTime")
6158
}
6259

6360
const inst = new BookTimerService()
64-
65-
if(bookId){
66-
ctx.body = await inst.addReadingTime(bookId, readingTime)
67-
}
61+
ctx.body = await inst.addReadingTime(bookId, readingTime)
6862
}
6963

70-
71-
72-
73-
74-
7564
module.exports = {
7665
getBookTimer,
7766
postReadingTime,
78-
deleteReadingTime
79-
67+
deleteReadingTime,
8068
}

src/dao/bookTimerDao.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { BookHistoryRepository } = require('./repositories/bookHistoryRepository')
2-
const { MyBookRepository } = require('./repositories/myBookRepository')
32
const { AccountRepository } = require('./repositories/accountRepository')
43
const { BookTimerDecorator } = require('./bookTimerDecorator')
54

@@ -53,7 +52,7 @@ class BookTimerDao {
5352
const postResult = await bookHistoryRepo.insertReadingTimeByBookId(accountInfo.user_id, bookId, reading_time)
5453

5554
// postResult check
56-
if (!postResult || !postResult[0].hasOwnProperty('id')){
55+
if (!postResult || !postResult.length < 0){
5756
throw new InternalServerError()
5857
}
5958

src/dao/repositories/bookHistoryRepository.js

+7-21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
const pgClient = require('../connections/postgresql')
22

3-
43
class BookHistoryRepository {
54
constructor(){
65
this._repositoryName = 'BookHistoryRepository'
76
}
87
get name(){
98
return this._repositoryName
109
}
11-
10+
1211
async getBookHistoryListByBookId(bookId){
1312
const query = pgClient.select('*')
1413
.from('tbl_mybook as tb')
@@ -18,8 +17,6 @@ class BookHistoryRepository {
1817
.whereNull('th.deleted_at')
1918
.orderBy('th.updated_at', 'desc')
2019
.limit(100)
21-
22-
2320
return await query
2421
}
2522

@@ -29,9 +26,6 @@ class BookHistoryRepository {
2926
.where('th.id', bookHistoryId)
3027
.whereNull('th.deleted_at')
3128
.first()
32-
33-
34-
3529
return await query
3630
}
3731

@@ -45,7 +39,6 @@ class BookHistoryRepository {
4539
}
4640

4741
async insertReadingTimeByBookId(userId, bookId, readingTime){
48-
4942
const query = pgClient.transaction(function(trx) {
5043
pgClient.insert({id:pgClient.raw('gen_random_uuid()') ,
5144
user_id : userId,
@@ -54,24 +47,19 @@ class BookHistoryRepository {
5447
.into('tbl_myhistory')
5548
.returning('id')
5649
.then(trx.commit)
57-
.catch(trx.rollback)});
58-
50+
.catch(trx.rollback)
51+
});
5952
return await query
60-
61-
6253
}
6354
async removeReadingTimeByBookHistoryId(bookHistoryId){
64-
6555
const query = pgClient.transaction(function(trx) {
6656
pgClient.table('tbl_myhistory')
6757
.update({deleted_at : pgClient.raw('now()')})
6858
.where('id', bookHistoryId)
6959
.then(trx.commit)
70-
.catch(trx.rollback)});
71-
60+
.catch(trx.rollback)
61+
});
7262
return await query
73-
74-
7563
}
7664

7765
async removeReadingTimeByBookId(bookId){
@@ -80,12 +68,10 @@ class BookHistoryRepository {
8068
.update({deleted_at : pgClient.raw('now()')})
8169
.where('mybook_id', bookId)
8270
.then(trx.commit)
83-
.catch(trx.rollback)});
71+
.catch(trx.rollback)
72+
});
8473
return await query
85-
86-
8774
}
88-
8975
}
9076

9177
module.exports = {

src/services/bookTimerService.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const validator = require('validator')
2-
const { InvalidUUID, InternalServerError } = require('./errorService')
2+
const { InvalidUUID, InvalidINT } = require('./errorService')
33
const { BookTimerDao } = require('../dao/bookTimerDao')
44

55
class BookTimerService {
@@ -12,21 +12,18 @@ class BookTimerService {
1212
}
1313

1414
async getBookTimerByBookId(bookId){
15-
1615
// uuid type check
1716
if (!validator.isUUID(bookId)){
1817
throw new InvalidUUID(bookId)
1918
}
2019

2120
const bookTimerDao = new BookTimerDao()
22-
2321
const bookTimerInfo = await bookTimerDao.getBookTimerInfoByBookId(bookId)
2422

2523
return bookTimerInfo
2624
}
2725

2826
async addReadingTime(bookId, readingTime){
29-
3027
// uuid type check
3128
if (!validator.isUUID(bookId)){
3229
throw new InvalidUUID(bookId)
@@ -41,14 +38,12 @@ class BookTimerService {
4138

4239
// add new timer history
4340
const addedHistory = await bookTimerDao.postReadingTimeInfo(bookId, readingTime)
44-
4541
const bookTimerInfo = await bookTimerDao.getBookTimerInfoByBookId(bookId)
4642

4743
return bookTimerInfo
4844
}
4945

5046
async removeReadingTime(bookId, bookHistoryId){
51-
5247
// uuid type check
5348
if (!validator.isUUID(bookId)){
5449
throw new InvalidUUID(bookId)
@@ -61,20 +56,15 @@ class BookTimerService {
6156
}
6257

6358
const bookTimerDao = new BookTimerDao()
64-
6559
const bookTimerInfo = await bookTimerDao.getBookTimerInfoByBookId(bookId)
6660

6761
let deleteHistory;
68-
69-
7062
if (bookHistoryId){
7163
deleteHistory = await bookTimerDao.deleteReadingTimeByHistoryId(bookId, bookHistoryId)
7264
}
7365
else{
7466
deleteHistory = await bookTimerDao.deleteReadingTimeByBookId(bookId)
7567
}
76-
77-
7868
return bookTimerInfo
7969
}
8070

0 commit comments

Comments
 (0)