Skip to content

Commit

Permalink
Merge pull request #112 from KU-KUICS/feature/#76/boardRefactoring
Browse files Browse the repository at this point in the history
Feature/#76/boardRefactoring
  • Loading branch information
lsjbh45 authored Sep 14, 2020
2 parents 4dd5f57 + 3949536 commit b4142db
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
59 changes: 31 additions & 28 deletions src/api/boards/boards.ctrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Joi = require('@hapi/joi');
const {
boards,
boardComments,
Expand All @@ -8,19 +7,12 @@ const {
sequelize,
} = require('../../models');

const titleScheme = Joi.string().min(3).required();
const bodyScheme = Joi.string().required();
const boardLevelScheme = Joi.any().valid('1', '2').required();

const boardScheme = Joi.object({
title: titleScheme,
body: bodyScheme,
level: boardLevelScheme,
});

const commentScheme = Joi.object({
body: bodyScheme,
});
const {
boardScheme,
commentScheme,
boardListScheme,
} = require('../../lib/schemes');
/* TODO: vaildation 추가 */

const checkUser = async (userId) => {
const user = await users.findOne({
Expand Down Expand Up @@ -75,22 +67,33 @@ const recommendedComment = async (boardCommentCommentId, userUserId) => {
/* TODO: user 가져오는 방식 변경 (req.user.emails[0].value) */

/**
* 글 미리보기 정보 가져오기
* @route GET /api/board
* page에 해당하는 글 리스트 가져오기
* @route GET /api/board/page/{page}
* @group Board
* @param {number} boardId.query.required - 글 번호
* @returns {Object} 200 - 글 미리보기
* @param {number} page.path.required - 페이지 번호
* @param {number} count.query.optional - 글 개수
* @returns {Object} 200 - 글 리스트
* @returns {Error} INVALID_PARAMETERS - invalid Parameters
*/
const getBoardExcerpt = async (req, res, next) => {
const getBoardList = async (req, res, next) => {
try {
const { boardId } = req.query;
const baseCount = 10;

const board = await checkBoard(boardId);
if (!board) throw new Error('INVALID_PARAMETERS');
const { error, value } = boardListScheme.validate({
page: req.params.page,
count: req.query.count,
});
if (error) throw new Error('INVALID_PARAMETERS');

const boardExcerpt = await boards.findOne({
where: { boardId },
const { page, count } = value;

const limit = count || baseCount; // count 없는 경우 baseCount 사용
const offset = (page - 1) * limit;

const boardList = await boards.findAll({
offset,
limit,
order: [['boardId', 'DESC']],
attributes: [
'boardId',
'excerpt',
Expand All @@ -101,7 +104,7 @@ const getBoardExcerpt = async (req, res, next) => {
],
});

res.json({ boardExcerpt });
res.json({ boardList });
} catch (err) {
next(err);
}
Expand Down Expand Up @@ -201,7 +204,7 @@ const postBoard = async (req, res, next) => {
const writeAuth = level <= userLevel;
if (!writeAuth) throw new Error('NO_AUTH');

const excerpt = body.substring(0, 150);
const excerpt = body.join(' ').substring(0, 150);

/* ISSUE: 에러 발생하는 경우에 boardId 증가하지 않도록 (빈 번호 없도록) 처리 필요 */
await boards.create({
Expand Down Expand Up @@ -257,7 +260,7 @@ const reviseBoard = async (req, res, next) => {
const writeAuth = level <= userLevel;
if (!writeAuth) throw new Error('NO_AUTH');

const excerpt = body.substring(0, 150);
const excerpt = body.join(' ').substring(0, 150);

await boards.update(
{ title, body, excerpt, level },
Expand Down Expand Up @@ -631,7 +634,7 @@ const recommendComment = async (req, res, next) => {
};

module.exports = {
getBoardExcerpt,
getBoardList,
getBoard,
postBoard,
reviseBoard,
Expand Down
4 changes: 2 additions & 2 deletions src/api/boards/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Router } = require('express');
const {
getBoardExcerpt,
getBoardList,
getBoard,
postBoard,
reviseBoard,
Expand All @@ -14,7 +14,7 @@ const {

const router = Router();

router.get('/', getBoardExcerpt);
router.get('/page/:page', getBoardList);
router.get('/:boardId', getBoard);
router.post('/', postBoard);
router.put('/:boardId', reviseBoard);
Expand Down
26 changes: 25 additions & 1 deletion src/lib/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Joi = require('@hapi/joi');
*/
const contentScheme = Joi.array().items(Joi.string()).required();

const boardLevelScheme = Joi.any().valid('1', '2').required();

const emailScheme = Joi.string()
.pattern(
/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$/i,
Expand All @@ -18,7 +20,9 @@ const nameScheme = Joi.string()
.pattern(/^[가-힣]{2,4}|[a-zA-Z]{2,10}\s[a-zA-Z]{2,10}$/)
.required();

const numberScheme = Joi.number().positive();
const numberScheme = Joi.number().positive().required();

const numberSchemeOptional = Joi.number().positive().optional();

const studentIdScheme = Joi.string()
.pattern(/^[0-9]{10}$/)
Expand All @@ -34,6 +38,7 @@ const searchTargetScheme = Joi.string()
* Scheme wrappers
* 1. Admin API
* 2. Search API
* 3. Board API
*/
const userScheme = Joi.object({
userName: nameScheme,
Expand Down Expand Up @@ -63,10 +68,29 @@ const searchInputScheme = Joi.object({
target: searchTargetScheme,
});

/* Board API */
const boardScheme = Joi.object({
title: stringScheme,
body: contentScheme,
level: boardLevelScheme,
});

const commentScheme = Joi.object({
body: stringScheme,
});

const boardListScheme = Joi.object({
page: numberScheme,
count: numberSchemeOptional,
});

module.exports = {
userScheme,
introScheme,
permScheme,
updateIntroScheme,
searchInputScheme,
boardScheme,
commentScheme,
boardListScheme,
};

0 comments on commit b4142db

Please sign in to comment.