diff --git a/index.js b/index.js index ddc2169..1412347 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ import { authRouter } from './src/routes/auth.route' import { placeRouter } from './src/routes/place.route' import { archiveRouter } from './src/routes/archive.route' import { userRouter } from './src/routes/user.route' +import { complainRouter } from './src/routes/complain.route' const app = express() const port = 3000 @@ -16,6 +17,7 @@ app.use('/auth', authRouter) app.use('/place', placeRouter) app.use('/archive', archiveRouter) app.use('/user', userRouter) +app.use('/complain', complainRouter) app.use('/output', (req, res) => { res.sendFile(__dirname + '/logs/pm2/myplace.ouput-0.log') }) diff --git a/src/controllers/complain.controller.js b/src/controllers/complain.controller.js new file mode 100644 index 0000000..c966ec3 --- /dev/null +++ b/src/controllers/complain.controller.js @@ -0,0 +1,28 @@ +import { response } from '../../config/response.js' +import { status } from '../../config/response.status.js' +import { + postComplainPlaceService, + getComplainService, +} from '../services/complain.service.js' + +export const complainPlaceController = async (req, res) => { + console.log('유저가 장소 신고를 요청하였습니다') + try { + const result = await postComplainPlaceService(req, res) + res.send(response(status.SUCCESS, result)) + } catch (error) { + console.log('POST COMPLAIN CTRL ERR: ', error) + res.send(response(status.BAD_REQUEST, null)) + } +} + +export const getComplainController = async (req, res) => { + console.log('유저가 문의 정보를 요청하였습니다') + try { + const result = await getComplainService(req, res) + res.send(response(status.SUCCESS, result)) + } catch (error) { + console.log('GET COMPLAIN CTRL ERR: ', error) + res.send(response(status.BAD_REQUEST, null)) + } +} diff --git a/src/models/complain.dao.js b/src/models/complain.dao.js new file mode 100644 index 0000000..0a2da7f --- /dev/null +++ b/src/models/complain.dao.js @@ -0,0 +1,51 @@ +import { pool } from '../../config/db.config' +import { BaseError } from '../../config/error' +import { status } from '../../config/response.status' + +import { insertPlaceComplain, getComplain } from './complain.sql' +import { selectPlace } from './place.sql' + +export const postComplainPlace = async (req, res) => { + const conn = await pool.getConnection() + + const { placeId } = req.params + const { content } = req.body + const userId = 1 //임시로 1로 설정 + + // 해당 placeId가 존재하는지 확인 + const place = await conn.query(selectPlace, placeId) + if (place[0].length === 0) { + console.log('장소가 존재하지 않음') + throw new BaseError(status.PARAMETER_IS_WRONG) + } + + try { + const result = await conn.query(insertPlaceComplain, [ + userId, + placeId, + content, + ]) + conn.release() + return { placeId: parseInt(placeId) } + } catch (error) { + console.log(error) + throw new BaseError(status.PARAMETER_IS_WRONG) + } +} + +export const showComplain = async (req, res) => { + const conn = await pool.getConnection() + + try { + const page = req.query.page ? parseInt(req.query.page) : 1 // 페이지 번호, 기본값은 1 + const offset = (page - 1) * 10 // 오프셋 계산 + + const result = await conn.query(getComplain, offset) + conn.release() + + return result[0] + } catch (error) { + console.log(error) + throw new BaseError(status.PARAMETER_IS_WRONG) + } +} diff --git a/src/models/complain.sql.js b/src/models/complain.sql.js new file mode 100644 index 0000000..0f3a83c --- /dev/null +++ b/src/models/complain.sql.js @@ -0,0 +1,6 @@ +export const insertPlaceComplain = ` + INSERT INTO complain (user_id, place_id, content) VALUES (?, ?, ?);` + +export const getComplain = ` + SELECT * FROM complain LIMIT ?, 10; +` diff --git a/src/routes/complain.route.js b/src/routes/complain.route.js new file mode 100644 index 0000000..97a36f4 --- /dev/null +++ b/src/routes/complain.route.js @@ -0,0 +1,10 @@ +import express from 'express' +import { + complainPlaceController, + getComplainController, +} from '../controllers/complain.controller' + +export const complainRouter = express.Router({ mergeParams: true }) + +complainRouter.post('/place/:placeId', complainPlaceController) +complainRouter.get('/', getComplainController) diff --git a/src/services/complain.service.js b/src/services/complain.service.js new file mode 100644 index 0000000..dc552bc --- /dev/null +++ b/src/services/complain.service.js @@ -0,0 +1,22 @@ +import { BaseError } from '../../config/error' +import { status } from '../../config/response.status' + +import { postComplainPlace, showComplain } from '../models/complain.dao' + +export const postComplainPlaceService = async (req, res) => { + try { + const result = await postComplainPlace(req, res) + return result + } catch (err) { + throw new BaseError(status.PARAMETER_IS_WRONG) + } +} + +export const getComplainService = async (req, res) => { + try { + const result = await showComplain(req, res) + return result + } catch (err) { + throw new BaseError(status.PARAMETER_IS_WRONG) + } +}