From ab2e4021ac46bbfa6132540fc01f7c2a3aba04f4 Mon Sep 17 00:00:00 2001 From: hyejungg Date: Tue, 10 Sep 2024 22:10:02 +0900 Subject: [PATCH] =?UTF-8?q?version=20=EC=A0=95=EB=B3=B4=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/versionController.js | 47 +++++++++++++++++++++++++++ src/models/version.js | 48 ++++++++++++++++++++++++++++ src/routes/index.js | 1 + src/routes/versionRoutes.js | 9 ++++++ src/utils/response.js | 4 +++ 5 files changed, 109 insertions(+) create mode 100644 src/controllers/versionController.js create mode 100644 src/models/version.js create mode 100644 src/routes/versionRoutes.js diff --git a/src/controllers/versionController.js b/src/controllers/versionController.js new file mode 100644 index 0000000..f710403 --- /dev/null +++ b/src/controllers/versionController.js @@ -0,0 +1,47 @@ +const Version = require('../models/version'); +const { BadRequest } = require('../utils/errors'); +const { + StatusCode, + SuccessMessage, + ErrorMessage, +} = require('../utils/response'); + +module.exports = { + checkVersion: async function (req, res, next) { + try { + if (!req.query.osType) { + throw new BadRequest(ErrorMessage.BadRequestMeg); + } + await Version.checkVersion(req).then((result) => { + return res.status(StatusCode.OK).json(result); + }); + } catch (err) { + next(err); + } + }, + updateVersion: async function (req, res, next) { + try { + if ( + !req.body.osType || + !req.body.minVersion || + !req.body.recommendedVersion + ) { + throw new BadRequest(ErrorMessage.BadRequestMeg); + } + await Version.updateVersion(req).then((result) => { + return res.status(StatusCode.OK).json(result); + }); + } catch (err) { + next(err); + } + }, + getVersions: async function (req, res, next) { + try { + await Version.getVersions(req).then((result) => { + return res.status(StatusCode.OK).json(result); + }); + } catch (err) { + next(err); + } + }, +}; diff --git a/src/models/version.js b/src/models/version.js new file mode 100644 index 0000000..49c69d4 --- /dev/null +++ b/src/models/version.js @@ -0,0 +1,48 @@ +const db = require('../config/db'); +const { NotFound } = require('../utils/errors'); +const { ErrorMessage } = require('../utils/response'); + +module.exports = { + checkVersion: async function (req) { + const osType = req.query.osType; + + const sqlSelect = + 'SELECT min_version, recommended_version FROM deploy WHERE platform = ?'; + const [rows] = await db.query(sqlSelect, osType); + + if (rows.length < 1) { + throw new NotFound(ErrorMessage.versionInfoNotFound); + } + + return { + platform: osType, + minVersion: rows[0].min_version, + recommendedVersion: rows[0].recommended_version, + }; + }, + updateVersion: async function (req) { + const osType = req.body.osType; + const minVersion = req.body.minVersion; + const recommendedVersion = req.body.recommendedVersion; + + const sqlUpdate = + 'UPDATE deploy SET min_version = ?, recommended_version = ? WHERE platform = ?'; + const params = [minVersion, recommendedVersion, osType]; + const [updated] = await db.queryWithTransaction(sqlUpdate, params); + + if (updated.affectedRows < 1) { + throw new NotFound(ErrorMessage.versionUpdatedFailed); + } + return true; + }, + getVersions: async function (req) { + const sqlSelect = 'SELECT * FROM deploy'; + const [rows] = await db.query(sqlSelect, null); + + if (rows.length < 1) { + throw new NotFound(ErrorMessage.versionInfoNotFound); + } + + return Object.setPrototypeOf(rows, []); + }, +}; diff --git a/src/routes/index.js b/src/routes/index.js index 9ed028a..4252fa9 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -3,6 +3,7 @@ const router = new express.Router(); router.get('/', (req, res) => res.send('Welcome to WishBoard!!')); +router.use('/version', require('./versionRoutes')); router.use('/auth', require('./authRoutes')); router.use('/user', require('./userRoutes')); router.use('/item', require('./itemRoutes')); diff --git a/src/routes/versionRoutes.js b/src/routes/versionRoutes.js new file mode 100644 index 0000000..61dbe70 --- /dev/null +++ b/src/routes/versionRoutes.js @@ -0,0 +1,9 @@ +const versionController = require('../controllers/versionController'); +const express = require('express'); +const router = new express.Router(); + +router.get('/check', versionController.checkVersion); +router.get('/', versionController.getVersions); +router.put('/', versionController.updateVersion); + +module.exports = router; diff --git a/src/utils/response.js b/src/utils/response.js index 26b7074..cde68ab 100644 --- a/src/utils/response.js +++ b/src/utils/response.js @@ -129,6 +129,10 @@ const ErrorMessage = { failedCreateToken: 'token 생성 실패', userIdNotFound: '토큰을 생성하기 위한 userId data 없음', + /* 버전 */ + versionInfoNotFound: '버전 정보 없음', + versionUpdatedFailed: '버전 정보 수정 실패', + /* 공통*/ BadRequestMeg: '잘못된 요청', ApiUrlIsInvalid: '잘못된 경로',