Skip to content
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
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
})
Expand Down
28 changes: 28 additions & 0 deletions src/controllers/complain.controller.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
51 changes: 51 additions & 0 deletions src/models/complain.dao.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions src/models/complain.sql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const insertPlaceComplain = `
INSERT INTO complain (user_id, place_id, content) VALUES (?, ?, ?);`

export const getComplain = `
SELECT * FROM complain LIMIT ?, 10;
`
10 changes: 10 additions & 0 deletions src/routes/complain.route.js
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions src/services/complain.service.js
Original file line number Diff line number Diff line change
@@ -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)
}
}