Skip to content

Commit f2c5348

Browse files
authored
Merge pull request #5 from Bookmark-Oneday/main
syncup
2 parents 0b96c58 + 2c3ed73 commit f2c5348

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1626
-177
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
}

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ npm install --save
4141
docker-compose -f ./docker/DB-docker-compose.yml --env-file ./deploy/env/postgresql.env down
4242
```
4343
<br />
44+
45+
- DB Migration
46+
- install migration tool
47+
```bash
48+
npm install knex -g
49+
```
50+
- Run migration
51+
```bash
52+
knex migrate:latest
53+
```
4454
<br />
4555

4656
## Test Commands

deploy/config/constant.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const constants = {
2+
oauthType: ['google', 'kakao']
3+
}
4+
5+
module.exports = {
6+
constants,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Test accounts
3+
insert into tbl_account values('31a1c45d-4026-4533-a63d-b44d58408556','test1','test1-1','nickname1','[email protected]','010-1235-5678','KR','google','72be7001-1711-4f01-bbfd-ecaf5d90832f',null,7200,null,false,null,false,null,null,'[]','{}',now(),now(),null);
4+
insert into tbl_account values('db77428c-0e01-49be-bcfe-e3f30361e72b','test2','test2-1','nickname2','[email protected]','210-1235-5678','KR','google','1ded124b-af4a-4306-8c3e-4c781b925d76',null,3600,null,false,null,false,null,null,'[]','{}',now(),now(),null);
5+
insert into tbl_account values('47ac1567-b916-4947-b9e5-4ae4f849af73','test3','test3-1','nickname3','[email protected]','310-1235-5678','KR','google','618f40a7-aa47-4d46-b40e-47bc9fc256a4',null,7200,null,false,null,false,null,null,'[]','{}',now(),now(),null);
6+
*/
7+
8+
9+
exports.up = (knex) => {
10+
return knex.schema.dropTableIfExists('tbl_account').createTable('tbl_account', (table)=>{
11+
table.uuid('id').notNullable().primary().index('tbl_account_id_index')
12+
table.string('firstname', 50)
13+
table.string('lastname', 50)
14+
table.string('nickname', 50)
15+
table.string('email', 64)
16+
table.string('mobile', 64)
17+
table.string('country', 2).defaultTo('KR')
18+
table.string('oauth_type', 16).notNullable().index('tbl_account_oauth_type_index')
19+
table.string('oauth_id', 512).index('tbl_account_oauth_id_index')
20+
table.string('pin', 64).defaultTo('0000')
21+
table.integer('target_read_time').notNullable().defaultTo(3600)
22+
table.timestamp('pin_created_at')
23+
table.boolean('email_verification').defaultTo(false)
24+
table.timestamp('email_verification_date')
25+
table.boolean('age_verification').defaultTo(false)
26+
table.timestamp('age_verification_date')
27+
table.string('profile_image_url', 2048)
28+
table.jsonb('bg_image_urls', 2048).notNullable().defaultTo(JSON.stringify([]))
29+
table.jsonb('meta').notNullable().defaultTo(JSON.stringify({}))
30+
table.timestamp('created_at').defaultTo(knex.fn.now())
31+
table.timestamp('updated_at').defaultTo(knex.fn.now())
32+
table.timestamp('deleted_at')
33+
})
34+
}
35+
36+
exports.down = (knex)=>{
37+
return knex.schema.dropTable('tbl_account')
38+
}

deploy/dbMigrations/20230301144748_init_tables.js

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/*
3+
test data
4+
insert into tbl_mybook values('38d44dba-910a-49cb-88b0-440353164777', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook1', 'testboo1 contents', '["author1", "author2"]', '["trans1", "trans2"]', 'publisher1', 'thumbnail url', '00000000', 0, 512, '["image1", "image2"]', false, false, '{}', now(), now(), null);
5+
insert into tbl_mybook values('4b4acc85-c0de-41ed-8077-fa9d8a1807e9', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook2', 'testboo2 contents', '["author2-1", "author2-2"]', '["trans2-1", "trans2-2"]', 'publisher2', 'thumbnail url 2', '11111111', 0, 600, '["image2-1", "image2-2"]', false, false, '{}', now(), now(), null);
6+
insert into tbl_mybook values('84e65492-0bc3-4507-9f14-36edf07ba9a0', '31a1c45d-4026-4533-a63d-b44d58408556', 'testbook2', 'testboo2 contents', '["author3-1", "author3-2"]', '["trans3-1", "trans3-2"]', 'publisher3', 'thumbnail url 3', '22222222', 0, 812, '["image3-1", "image3-2"]', false, false, '{}', now(), now(), null);
7+
*/
8+
exports.up = (knex) => {
9+
return knex.schema.dropTableIfExists('tbl_mybook').createTable('tbl_mybook', (table)=>{
10+
table.uuid('id').notNullable().primary().index('tbl_mybook_id_index')
11+
table.uuid('user_id').notNullable().index('tbl_mybook_user_id_index')
12+
table.string('title', 255).notNullable()
13+
table.text('content')
14+
table.jsonb('authors').defaultTo(JSON.stringify([]))
15+
table.jsonb('translators').defaultTo(JSON.stringify([]))
16+
table.string('publisher')
17+
table.string('thumbnail_url')
18+
table.string('isbn').notNullable()
19+
table.integer('current_page').defaultTo(0)
20+
table.integer('total_page').defaultTo(1)
21+
table.jsonb('images').notNullable().defaultTo(JSON.stringify([]))
22+
table.boolean('reading').defaultTo(false)
23+
table.boolean('favorite').defaultTo(false).index('tbl_mybook_favorite_index')
24+
table.jsonb('meta').notNullable().defaultTo(JSON.stringify({}))
25+
table.timestamp('created_at').defaultTo(knex.fn.now())
26+
table.timestamp('updated_at').defaultTo(knex.fn.now())
27+
table.timestamp('deleted_at')
28+
})
29+
}
30+
31+
exports.down = (knex)=>{
32+
return knex.schema.dropTable('tbl_mybook')
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* test data
3+
insert into tbl_myhistory values('75d332e1-3de0-44ee-95df-265f0cacdbf0', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 360, '2023-03-05 18:34:33', '2023-03-05 18:34:33', null);
4+
insert into tbl_myhistory values('5be35a18-fd1a-4136-9837-50ac6243a88e', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 237, '2023-03-06 21:30:00', '2023-03-06 21:30:00', null);
5+
insert into tbl_myhistory values('94b71146-ec35-4f00-ab5a-20551a17017f', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 245, '2023-03-07 03:30:00', '2023-03-07 03:30:00', null);
6+
insert into tbl_myhistory values('27245397-22ea-4af5-b5c2-8f1dcc4a2947', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 3678, '2023-03-12 01:30:00', '2023-03-12 01:30:00', null);
7+
insert into tbl_myhistory values('c7d2e046-df3b-4cb4-b1c4-e9c1fdf2eb36', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 341, '2023-03-17 13:30:00', '2023-03-17 13:30:00', null);
8+
insert into tbl_myhistory values('7fb01edb-e29e-48bc-a5cf-70d977ddd5de', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 741, '2023-03-17 17:30:00', '2023-03-17 17:30:00', null);
9+
insert into tbl_myhistory values('8d3b0fd6-afe9-4c6a-b6f6-50a23fbc71e4', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 861, '2023-03-17 22:30:00', '2023-03-17 22:30:00', null);
10+
insert into tbl_myhistory values('cfb5a016-8d5f-410c-9497-e7be1a0a19db', '31a1c45d-4026-4533-a63d-b44d58408556', '38d44dba-910a-49cb-88b0-440353164777', 341, '2023-03-19 10:30:00', '2023-03-19 10:30:00', null);
11+
*/
12+
exports.up = (knex) => {
13+
return knex.schema.dropTableIfExists('tbl_myhistory').createTable('tbl_myhistory', (table)=>{
14+
table.uuid('id').notNullable().primary().index('tbl_myhistory_id_index')
15+
table.uuid('user_id').notNullable().index('tbl_myhistory_user_id_index')
16+
table.uuid('mybook_id').notNullable().index('tbl_myhistory_mybook_id_index')
17+
table.integer('reading_time').notNullable().defaultTo(0)
18+
table.timestamp('created_at').defaultTo(knex.fn.now()).index('tbl_myhistory_created_at_index')
19+
table.timestamp('updated_at').defaultTo(knex.fn.now()).index('tbl_myhistory_updated_at_index')
20+
table.timestamp('deleted_at')
21+
})
22+
}
23+
24+
exports.down = (knex)=>{
25+
return knex.schema.dropTable('tbl_myhistory')
26+
}

package-lock.json

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"test:controllers": "jest --forceExit --detectOpenHandles ./src/controllers/",
1212
"test:routes": "jest --forceExit --detectOpenHandles ./src/routes/",
1313
"test:services": "jest --forceExit --detectOpenHandles ./src/services/",
14-
"start:local": "APP_ENV=local node ./src/app.js",
14+
"start:local": "APP_ENV=local nodemon ./src/app.js",
1515
"start:dev": "APP_ENV=dev node ./src/app.js",
1616
"start:prod": "APP_ENV=prod node ./src/app.js",
1717
"lint": "eslint ./src"
@@ -51,7 +51,7 @@
5151
"jest": "^29.4.3",
5252
"mocha": "^10.2.0",
5353
"mockdate": "^3.0.5",
54-
"nodemon": "^2.0.20",
54+
"nodemon": "^2.0.21",
5555
"sinon": "^15.0.1",
5656
"supertest": "^6.3.3",
5757
"supertest-as-promised": "^4.0.2"

src/app.js

+6-47
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,19 @@
11
const koa = require('koa')
22
const koaBody = require('koa-body')
3-
const uuid = require('uuid')
43

54
const { config } = require('../deploy/config/config')
65
const { initRouter } = require('./routes')
7-
const { log, requestLogger } = require('./utils/logging')
6+
const { log } = require('./utils/logging')
7+
8+
const { corsMiddleware, loggingMiddleware } = require('./middleware')
89

910
const app = new koa()
1011
app.use(koaBody.koaBody())
1112

12-
// Set CORS
13-
app.use(async (ctx, next)=>{
14-
if(ctx.method === 'OPTIONS'){
15-
if (!ctx.get('Access-Control-Requrest-Method')){
16-
await next()
17-
}
18-
ctx.set('Access-Control-Allow-Origin', '*')
19-
ctx.set('Access-Control-Allow-Credentials', 'true')
20-
ctx.set('Access-Control-Allow-Methods', 'GET')
21-
ctx.set('Access-Control-Allow-Headers', '*')
22-
ctx.status = 204
23-
}else{
24-
ctx.set('Access-Control-Allow-Origin', '*')
25-
ctx.set('Access-Control-Allow-Credentials', 'true')
26-
ctx.set('Vary', 'Origin')
27-
await next();
28-
}
29-
})
30-
31-
// error logging
32-
app.use(async (ctx, next)=>{
33-
ctx.state.requestId = uuid.v4()
34-
try{
35-
await next()
36-
}catch(err){
37-
const errorObject = {
38-
...err,
39-
message: err.message,
40-
trace: err.stack,
41-
}
42-
43-
ctx.status = err.status || err.statusCode || 500
44-
ctx.body = {
45-
error: errorObject,
46-
meta: {
47-
requestId: ctx.state.requestId,
48-
now: +new Date(),
49-
},
50-
}
5113

52-
requestLogger(ctx, {
53-
message: err.message,
54-
trace: err.stack,
55-
})
56-
}
57-
})
14+
// set middleware
15+
app.use(corsMiddleware)
16+
app.use(loggingMiddleware)
5817

5918
const router = initRouter()
6019
app.use(router.routes())

src/controllers/accountController.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
const { AccountService } = require('../services')
1+
const { AccountService, InvalidRequestParameter } = require('../services')
22

33
const getMe = async (ctx)=>{
44
const {
55
headers: {
6-
userid
6+
userid,
7+
oauth_id,
8+
oauth_type,
79
}
810
} = ctx
911

1012
const inst = new AccountService()
1113
if(userid){
1214
ctx.body = await inst.getAccountByUserId(userid)
15+
}if(oauth_id && oauth_type){
16+
ctx.body = await inst.getAccountByOauthId(oauth_id, oauth_type)
17+
console.log('ctx : ', ctx.body)
18+
}else{
19+
throw new InvalidRequestParameter('oauth_id', 'oauth_type')
20+
}
21+
22+
ctx.body.meta = {
23+
requestId: ctx.state.requestId,
24+
now: +new Date(),
1325
}
1426
}
1527

1628
const joinMe = async (ctx)=>{
1729
const {
1830
query,
1931
headers,
20-
request: { body }
2132
} = ctx
2233
const inst = new AccountService()
2334
inst.addNewAccount()

0 commit comments

Comments
 (0)