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
5 changes: 3 additions & 2 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const bookTimerController = require("./bookTimerController");
const libraryController = require('./libraryController')
const lastPageController = require("./lastPageController");
const bookShelfController = require("./bookShelfController");

const onelinerController = require("./onelinerController");

module.exports = {
...healthcheckController,
Expand All @@ -16,5 +16,6 @@ module.exports = {
...bookTimerController,
...bookShelfController,
...lastPageController,
...libraryController
...libraryController,
...onelinerController
}
54 changes: 54 additions & 0 deletions src/controllers/onelinerController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { OnelinerService } = require('../services')


const postOneliner = async (ctx) => {
const userId = ctx.request.headers.user_id;

const {
book_id,
title,
authors,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url
} = ctx.request.body;

const onelinerService = new OnelinerService();

const result = await onelinerService.postOneliner(userId, book_id,
title,
authors,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url);
Copy link
Collaborator

Choose a reason for hiding this comment

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

이렇게 함수 parameter가 많은경우 object로 묶어서 전달하는게 좋아요

const result = await onelinerService.postOneliner({userId, book_id, title, authors, oneliner, color, top, left, font, font_size, bg_image_url});


const data = {
oneliner:
result.data

}
const meta = {
sortType: "latest",
continuousToken: "0",
currentPage: "0",
requestId: ctx.state.requestId,
now: +new Date(),
};

ctx.body = {
data: data,
meta
};
};

module.exports = {
postOneliner
}
5 changes: 3 additions & 2 deletions src/dao/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const readingHistoryDao = require('./bookTimerDao')
const mylistDao = require('./mylistDao')
const lastPageDao = require("./lastPageDao");
const bookShelfDao = require("./bookShelfDao");

const onelinerDao = require("./onelinerDao");

module.exports = {
...accountDao,
...postingDao,
...readingHistoryDao,
...bookShelfDao,
...lastPageDao,
...mylistDao
...mylistDao,
...onelinerDao
}
46 changes: 46 additions & 0 deletions src/dao/onelinerDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { OnelinerRepository } = require('./repositories/onelinerRepository')

class OnelinerDao {
constructor(){
this._daoName = 'OnelinerDao'
}

get daoName(){
return this._daoName
}


async postOneliner(userId, book_id, title, authors,oneliner, color,top,left,font,font_size,bg_image_url){

const OnelinerRepo = new OnelinerRepository();
const userInfo = await OnelinerRepo.getUserInfoByUserId(userId);

const profileImageUrlJSON = JSON.stringify(userInfo[0].profile_image_url);
const nicknameJSON = JSON.stringify(userInfo[0].nickname);
const authorsJSON = JSON.stringify(authors);

const onelinerInfo = await OnelinerRepo.postOneliner(
profileImageUrlJSON,
nicknameJSON,
userId,
book_id,
title,
authorsJSON,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url)

const onelinerInfoResult = await OnelinerRepo.getOnelinerInfoByBookId(onelinerInfo.book_id)

const result = {
data : onelinerInfoResult,
}
return result
}
}

module.exports = { OnelinerDao }
65 changes: 65 additions & 0 deletions src/dao/repositories/onelinerRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const pgClient = require('../connections/postgresql')

class OnelinerRepository {
constructor(){
this._repositoryName = 'OnelinerRepository'
}
get name(){
return this._repositoryName
}
// 결과 호출을 위한 쿼리
async getOnelinerInfoByBookId(book_id){
const query = pgClient.select('*')
.from('tbl_oneliner')
.where('tbl_oneliner.book_id', book_id)
return query
}
// UserInfo를 가져오기 위한 쿼리 (profile_image, nickname)
async getUserInfoByUserId(user_id){
const query = pgClient.select('profile_image_url','nickname')
.from('tbl_account')
.where('tbl_account.id', user_id)
return query
}

async postOneliner(
profile_image_url,
nickname,
userId,
book_id,
title,
authors,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url) {

const query = pgClient('tbl_oneliner').insert({
id: pgClient.raw("gen_random_uuid()"),
user_id: userId,
profile_image: profile_image_url,
nickname: nickname,
book_id: book_id,
title: title,
authors: authors,
oneliner: oneliner,
color: color,
top: top,
left: left,
font: font,
font_size: font_size,
bg_image_url: bg_image_url
}).returning('book_id');
const result = await query; // 쿼리 실행 및 결과 받기

return result[0]; // book_id 반환
}

}

module.exports = {
OnelinerRepository,
};
3 changes: 2 additions & 1 deletion src/routes/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { lastPageRouteV1 } = require("./lastPageRouteV1");
const { libraryRouteV1 } = require('./libraryRouteV1')
const { bookShelfRouteV1 } = require("./bookShelfRouteV1");

const { onelinerRouteV1 } = require("./onelinerRouteV1");

const V1 = () => {
const router = Router();
Expand All @@ -18,7 +19,7 @@ const V1 = () => {
lastPageRouteV1(router);
libraryRouteV1(router);
bookShelfRouteV1(router);

onelinerRouteV1(router);
return router;
};

Expand Down
15 changes: 15 additions & 0 deletions src/routes/v1/onelinerRouteV1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Router = require("koa-router");
const onelinerController = require("../../controllers/onelinerController");

const onelinerRouteAPIV1 = (root)=>{
const router = Router();

router.post('/', onelinerController.postOneliner);

root.use('/oneliner', router.routes())
}

const onelinerRouteV1 = (root)=>{
onelinerRouteAPIV1(root)
}
module.exports = { onelinerRouteV1 }
3 changes: 2 additions & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DecoratorService = require("../dao/bookTimerDecorator");
const bookShelfService = require("./bookShelfService");
const lastPageService = require("./lastPageService");
const mylistService = require('./mylistService')

const onelinerService = require('./onelinerService');

module.exports = {
...healthcheckService,
Expand All @@ -23,4 +23,5 @@ module.exports = {
...DecoratorService,
...bookShelfService,
...lastPageService,
...onelinerService,
};
38 changes: 38 additions & 0 deletions src/services/onelinerService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const {
OnelinerDao
} = require("../dao/onelinerDao");

const validator = require('validator')
const { InvalidUUID, AccountNotFound, MyBookNotFound } = require('./errorService');

class OnelinerService {
constructor(){
this._serviceName = 'onelinerService'
}

get serviceName(){
return this._serviceName
}

async postOneliner(userId, book_id, title, authors, oneliner, color, top, left, font, font_size, bg_image_url){
Copy link
Collaborator

Choose a reason for hiding this comment

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

parameter 의 validation 체크가 없네요

const onelinerDao = new OnelinerDao()
const onelinerRows = await onelinerDao.postOneliner(
userId,
book_id,
title,
authors,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url)

return onelinerRows
}


}

module.exports = { OnelinerService };