Skip to content

[BMOL-28] POST v1/oneliner 구현 #22

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions src/controllers/onelinerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,57 @@ const getOneliner = async (ctx) => {
};
};

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: userId,
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
});

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
};
};

const deleteOneliner = async (ctx)=>{
const {
params: {
Expand All @@ -56,5 +107,6 @@ const deleteOneliner = async (ctx)=>{

module.exports = {
getOneliner,
postOneliner,
deleteOneliner
}
32 changes: 32 additions & 0 deletions src/dao/onelinerDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,39 @@ class OnelinerDao {
}
return result
}

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
}

async deleteOnelinerByOnelinerId(onelinerId){
const OnelinerRepo = new OnelinerRepository()
const onelinerIdIsValid = await OnelinerRepo.onelinerIdIsValid(onelinerId)
Expand Down
55 changes: 53 additions & 2 deletions src/dao/repositories/onelinerRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class OnelinerRepository {
const countQuery = pgClient('tbl_oneliner').count();

const query = pgClient('tbl_oneliner')
.select('id', 'user_id', 'profile_image', 'nickname', 'book_id', 'title', 'authors', 'oneliner', 'color', 'top', 'left', 'font', 'font_size', 'bg_image_url', 'created_at')
.whereNull('deleted_at'); // deleted_at이 null인 것만 가져오도록 추가
.select('id', 'user_id', 'profile_image', 'nickname', 'book_id', 'title', 'authors', 'oneliner', 'color', 'top', 'left', 'font', 'font_size', 'bg_image_url', 'created_at');

// sortType 별로 출력 (일단은 latest만), limit: 조회할 데이터의 개수 지정, offset: 조회할 데이터의 시작 위치를 지정
// -> offset번째 데이터부터 perPage 개수만큼의 데이터를 조회

Expand All @@ -30,6 +30,57 @@ class OnelinerRepository {
dataResult
};
}
// 결과 호출을 위한 쿼리
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 반환
}

async onelinerIdIsValid(onelinerId){
const query = pgClient('tbl_oneliner')
.where('id', onelinerId)
Expand Down
5 changes: 4 additions & 1 deletion src/routes/v1/onelinerRouteV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ const onelinerController = require("../../controllers/onelinerController");

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


router.post('/', onelinerController.postOneliner);
router.get('/', onelinerController.getOneliner);
router.delete('/:onelinerId', onelinerController.deleteOneliner)

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

const onelinerRouteV1 = (root)=>{
onelinerRouteAPIV1(root)
}

module.exports = { onelinerRouteV1 }
50 changes: 43 additions & 7 deletions src/services/onelinerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,50 @@ class OnelinerService {
get serviceName(){
return this._serviceName
}

async getOneliner(userId, sortType, perPage, continuousToken){

async postOneliner(data) {
const {
userId,
book_id,
title,
authors,
oneliner,
color,
top,
left,
font,
font_size,
bg_image_url
} = data;

// uuid type check
if (!validator.isUUID(userId)){
throw new InvalidUUID(userId)
// UUID type check
if (!validator.isUUID(userId)) {
throw new InvalidUUID(userId);
}

if (!validator.isUUID(book_id)) {
throw new InvalidUUID(book_id);
}

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;
}
async getOneliner(userId, sortType, perPage, continuousToken){

const onelinerDao = new OnelinerDao()

const oneliner = await onelinerDao.getOneliner(userId, sortType, perPage, continuousToken)
Expand All @@ -40,7 +76,7 @@ class OnelinerService {

return oneliner
}

}

module.exports = { OnelinerService }
module.exports = { OnelinerService };