From bc0822a08094ba08249ef785656f0d152930672b Mon Sep 17 00:00:00 2001 From: kgpyo Date: Tue, 10 Dec 2019 01:45:10 +0900 Subject: [PATCH] refactor: group by controller category MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기존 CROUD에서 테이블 정보를 불러오는 컨트롤러가 추가되었기 때문에, 데이터를 쓰고, 읽고, 삭제, 수정하는 컨트롤러와 단순한 정보를 불러오 는 컨트롤러를 분리 (팀원의견) - elasitc search는 비동기 이벤트가 있기 때문에, 정상적으로 테스트가 되지 않고, 추후 수정 예정 - close #29 --- .github/workflows/server-api-deploy.yml | 14 +--- apis/product/db/model/product.js | 1 + apis/product/db/seeds/product.js | 2 - apis/product/env.ssl | Bin 256 -> 256 bytes apis/product/package.json | 2 +- .../routes/controller/deleteProduct.js | 12 --- .../routes/controller/getProductInfo.js | 29 ------- apis/product/routes/controller/info.js | 8 ++ .../routes/controller/modifyProduct.js | 14 ---- .../routes/controller/productListLookup.js | 18 ---- apis/product/routes/controller/products.js | 77 ++++++++++++++++++ .../product/routes/controller/writeProduct.js | 12 --- apis/product/routes/info.js | 7 +- apis/product/routes/product.js | 11 ++- apis/product/run.sh | 5 +- apis/product/tests/unit.test.js | 4 +- 16 files changed, 102 insertions(+), 114 deletions(-) delete mode 100644 apis/product/routes/controller/deleteProduct.js delete mode 100644 apis/product/routes/controller/getProductInfo.js create mode 100644 apis/product/routes/controller/info.js delete mode 100644 apis/product/routes/controller/modifyProduct.js delete mode 100644 apis/product/routes/controller/productListLookup.js create mode 100644 apis/product/routes/controller/products.js delete mode 100644 apis/product/routes/controller/writeProduct.js diff --git a/.github/workflows/server-api-deploy.yml b/.github/workflows/server-api-deploy.yml index f5ea83e..f41ef0e 100644 --- a/.github/workflows/server-api-deploy.yml +++ b/.github/workflows/server-api-deploy.yml @@ -25,7 +25,7 @@ jobs: run: | echo "##[set-output name=list;]$(echo $(git remote -v) | cut -d' ' -f2) ${{ steps.BaseRef.outputs.branch }} $GITHUB_REPOSITORY" id: Arguments - - name: Decrypt Environment and Send Data + - name: Decrypt Environment and build env: LARGE_SECRET_PASSPHRASE: ${{ secrets.PRIVATEKEY }} host: ${{ secrets[steps.HostName.outputs.host] }} @@ -44,15 +44,9 @@ jobs: else touch ../../.env fi - - name: copy file via ssh password - uses: appleboy/scp-action@master - with: - host: ${{ secrets[steps.HostName.outputs.host] }} - username: ${{ secrets.USERNAME }} - password: ${{ secrets.PASSWORD }} - port: ${{ secrets.PORT }} - source: ".env" - target: "~/" + yarn build + cd ../../ + sshpass -p$passowrd scp -P $port -o StrictHostKeyChecking=no . $username@$password:~/ - name: deploy to server uses: appleboy/ssh-action@master env: diff --git a/apis/product/db/model/product.js b/apis/product/db/model/product.js index b9a6294..97c2f48 100644 --- a/apis/product/db/model/product.js +++ b/apis/product/db/model/product.js @@ -123,6 +123,7 @@ const productSchema = new Schema({ timestamps: { createdAt: true, updatedAt: true }, }); + productSchema.plugin(mongoosastic, { hosts: [ `${process.env.ELASTICSEARCH}`, diff --git a/apis/product/db/seeds/product.js b/apis/product/db/seeds/product.js index ac913f8..0db3241 100644 --- a/apis/product/db/seeds/product.js +++ b/apis/product/db/seeds/product.js @@ -4,8 +4,6 @@ import mock from './20191209.json'; const { product } = model; -require('dotenv').config(); - mongoose.connect(`${process.env.MONGO_URL}`, { useNewUrlParser: true, useUnifiedTopology: true, diff --git a/apis/product/env.ssl b/apis/product/env.ssl index 66ab652218e4a82458ce396be8055f88c6bb578f..a6bbe03fcb32ce4ec461056aaf904b7558e6119b 100644 GIT binary patch literal 256 zcmV+b0ssDq#{op26$ZkBrjPy=bf#tHl024-vkdkyS`$VjT$A(N9Y_g}S&s~GW`Q$^PUOl^54GXJ!X{xJ*a8K9qmqzEYUMRsU Gfp~fXjCOqh literal 256 zcmV+b0ssDg{3p~6Tum=wI^(KW0_RxO1r`ZUN>3m45)!tZE5_yQm4W`hYb1sU^!wYM z`+af~0@E1w@>ertlDXATTqMT|hXeP&N87;(GGRyhtT5wgS3~D1EsiI1X=Z8I`M4yH z=$6|@b&OOXoQsFL%En~~!mZxV3NI1)FSo;n8TW-1v!v^bsdkGILhs;0O^aM6QUj**DdoX) znfjk-$1#q}(&Lhf`F6>7iq60ebO^UWMqREHf7CN%Yo1l!6sDv$RBbF&s709Zzszhn Gdi~oqp?|Uf diff --git a/apis/product/package.json b/apis/product/package.json index 1d00e22..f130044 100644 --- a/apis/product/package.json +++ b/apis/product/package.json @@ -9,7 +9,7 @@ ] }, "scripts": { - "test": "jest --runInBand", + "test": "jest --runInBand --setupFiles dotenv/config --forceExit", "precommit": "lint-staged && yarn test", "dev:start": "babel-node bin/www.js", "build": "babel ./ -d build --copy-files '*.json'", diff --git a/apis/product/routes/controller/deleteProduct.js b/apis/product/routes/controller/deleteProduct.js deleted file mode 100644 index 9fb5d39..0000000 --- a/apis/product/routes/controller/deleteProduct.js +++ /dev/null @@ -1,12 +0,0 @@ -import { removeProduct } from '../../core'; - -const deleteProductController = async ({ params: { id } }, res, next) => { - try { - const result = await removeProduct(id, res.locals.userId); - res.json(result); - } catch (e) { - next(e); - } -}; - -export default deleteProductController; diff --git a/apis/product/routes/controller/getProductInfo.js b/apis/product/routes/controller/getProductInfo.js deleted file mode 100644 index 0cfbc71..0000000 --- a/apis/product/routes/controller/getProductInfo.js +++ /dev/null @@ -1,29 +0,0 @@ -import { getProducts, getElasticSearchResults } from '../../core'; - -const getProductListController = async (req, res, next) => { - if (res.locals.filter) { - res.locals.query = { - bool: { - must: res.locals.filter, - }, - }; - delete res.locals.filter; - } - try { - const list = await getElasticSearchResults(res.locals); - res.json(list); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -const findProductByIdController = async ({ params: { id } }, res, next) => { - try { - const result = await getProducts(1, 1, { _id: id }); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export { getProductListController, findProductByIdController }; diff --git a/apis/product/routes/controller/info.js b/apis/product/routes/controller/info.js new file mode 100644 index 0000000..e008db6 --- /dev/null +++ b/apis/product/routes/controller/info.js @@ -0,0 +1,8 @@ +import { getProductSchemaByKey } from '../../core'; + +const getProductSchemaController = (req, res) => { + const list = getProductSchemaByKey('category').enumValues; + res.json(list); +}; + +export default getProductSchemaController; diff --git a/apis/product/routes/controller/modifyProduct.js b/apis/product/routes/controller/modifyProduct.js deleted file mode 100644 index ed32a81..0000000 --- a/apis/product/routes/controller/modifyProduct.js +++ /dev/null @@ -1,14 +0,0 @@ -import { updateProduct } from '../../core'; - -const modifyProductController = async (req, res, next) => { - const { body, params: { id } } = req; - const { locals: { userId } } = res; - try { - const result = await updateProduct(id, userId, body); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default modifyProductController; diff --git a/apis/product/routes/controller/productListLookup.js b/apis/product/routes/controller/productListLookup.js deleted file mode 100644 index 2d9840e..0000000 --- a/apis/product/routes/controller/productListLookup.js +++ /dev/null @@ -1,18 +0,0 @@ -import { getProducts } from '../../core'; - -const productListLookupController = async (req, res, next) => { - const { - options = {}, - sort = { order: -1, createAt: 1 }, - page = 1, - limits = 10, - } = res.lcoals; - try { - const result = await getProducts(options, sort, page, limits); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default productListLookupController; diff --git a/apis/product/routes/controller/products.js b/apis/product/routes/controller/products.js new file mode 100644 index 0000000..edbf4f3 --- /dev/null +++ b/apis/product/routes/controller/products.js @@ -0,0 +1,77 @@ +import { + removeProduct, + getProducts, + getElasticSearchResults, + updateProduct, + insertProduct, +} from '../../core'; + +export const deleteProductController = async ({ params: { id } }, res, next) => { + try { + const result = await removeProduct(id, res.locals.userId); + res.json(result); + } catch (e) { + next(e); + } +}; + +export const getProductListController = async (req, res, next) => { + if (res.locals.filter) { + res.locals.query = { + bool: { + must: res.locals.filter, + }, + }; + delete res.locals.filter; + } + try { + const list = await getElasticSearchResults(res.locals); + res.json(list); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const findProductByIdController = async ({ params: { id } }, res, next) => { + try { + const result = await getProducts(1, 1, { _id: id }); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const modifyProductController = async (req, res, next) => { + const { body, params: { id } } = req; + const { locals: { userId } } = res; + try { + const result = await updateProduct(id, userId, body); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const productListLookupController = async (req, res, next) => { + const { + options = {}, + sort = { order: -1, createAt: 1 }, + page = 1, + limits = 10, + } = res.lcoals; + try { + const result = await getProducts(options, sort, page, limits); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; + +export const writeProductCotroller = async ({ body }, res, next) => { + try { + const result = await insertProduct(body); + res.json(result); + } catch (e) { + next({ status: 400, message: e.toString() }); + } +}; diff --git a/apis/product/routes/controller/writeProduct.js b/apis/product/routes/controller/writeProduct.js deleted file mode 100644 index ed8c7a7..0000000 --- a/apis/product/routes/controller/writeProduct.js +++ /dev/null @@ -1,12 +0,0 @@ -import { insertProduct } from '../../core'; - -const writeProductCotroller = async ({ body }, res, next) => { - try { - const result = await insertProduct(body); - res.json(result); - } catch (e) { - next({ status: 400, message: e.toString() }); - } -}; - -export default writeProductCotroller; diff --git a/apis/product/routes/info.js b/apis/product/routes/info.js index fba9558..e93460c 100644 --- a/apis/product/routes/info.js +++ b/apis/product/routes/info.js @@ -1,11 +1,8 @@ import express from 'express'; -import { getProductSchemaByKey } from '../core'; +import getProductSchemaController from './controller/info'; const router = express.Router(); -router.get('/category', (req, res) => { - const list = getProductSchemaByKey('category').enumValues; - res.json(list); -}); +router.get('/category', getProductSchemaController); export default router; diff --git a/apis/product/routes/product.js b/apis/product/routes/product.js index b5b19dc..46fee81 100644 --- a/apis/product/routes/product.js +++ b/apis/product/routes/product.js @@ -1,10 +1,13 @@ import express from 'express'; -import modifyProductController from './controller/modifyProduct'; -import deleteProductController from './controller/deleteProduct'; -import writeProductCotroller from './controller/writeProduct'; import isLoggedInUser from '../services/user'; -import { getProductListController, findProductByIdController } from './controller/getProductInfo'; import queryAnalysisMiddleware from './middleware/listView'; +import { + modifyProductController, + deleteProductController, + writeProductCotroller, + getProductListController, + findProductByIdController, +} from './controller/products'; const router = express.Router(); diff --git a/apis/product/run.sh b/apis/product/run.sh index 0c4f2c9..d8b5827 100644 --- a/apis/product/run.sh +++ b/apis/product/run.sh @@ -28,8 +28,5 @@ fi PM2INSTALL=$(npm ls -g --depth=0 | grep -w -o pm2) if [ "pm2" != "$PM2INSTALL" ]; then npm install -g pm2 + pm2 start ./build/bin/www --name server fi - -yarn build -cd ./build -pm2 start ./bin/www --name server diff --git a/apis/product/tests/unit.test.js b/apis/product/tests/unit.test.js index ffec43a..369811a 100644 --- a/apis/product/tests/unit.test.js +++ b/apis/product/tests/unit.test.js @@ -1,16 +1,14 @@ /* eslint-disable no-underscore-dangle */ // https://www.mockaroo.com/ import mongoose from 'mongoose'; -import dotenv from 'dotenv'; import model from '../db/model'; -import mockData from '../db/seeds/20191129.json'; +import mockData from '../db/seeds/20191209.json'; import message from '../core/message'; import * as Core from '../core'; import CODE from '../core/code'; const Product = model.product; -dotenv.config(); jest.setTimeout(10000); beforeAll(async () => {