11import { Request , Response } from 'express' ;
22import Visitor from '../../service/visitor' ;
33import VisitorRepository from '../../model/visitorRepository' ;
4- import { BadRequestError } from '../../service/error' ;
4+ import { BadRequestError , ServerError } from '../../service/error' ;
55import errorResposne from '../module/error' ;
6-
6+ import { VisitorCmtDto } from './visitor' ;
7+
8+ /**
9+ * Visitor increase and lookup API
10+ * @url /apis/visitor/count
11+ * @method patch
12+ * @resBody `{ statusCode: number, todayCount: number, totalCount: number }`
13+ * success response body
14+ * @resBody `{ statusCode: number, msg: string }` fail response body
15+ */
716const updateAndGetVisitor = async ( req : Request , res : Response ) => {
817 try {
918 const visitor = new Visitor ( new VisitorRepository ( ) ) ;
@@ -16,75 +25,119 @@ const updateAndGetVisitor = async (req: Request, res: Response) => {
1625 }
1726} ;
1827
28+ /**
29+ * Visit comment generation API
30+ * @url /apis/visitor/comment
31+ * @method post
32+ * @reqBody `{ nickname: string, password: string, description: string }`
33+ * @resBody `{ statusCode: number, commendId: number, msg: string }` success response body
34+ * @resBody `{ statusCode: number, msg: string }` fail response body
35+ */
1936const createVisitComment = async ( req : Request , res : Response ) => {
20- const RequestVisitorComment = Object . assign ( req . body ) ;
37+ const RequestVisitorComment : VisitorCmtDto = Object . assign ( req . body ) ;
2138
2239 try {
23- if ( RequestVisitorComment . nickname ?. length === 0 )
40+ if ( RequestVisitorComment . nickname ?. length === 0 ) {
2441 req . body . nickname = '익명' ;
42+ }
2543
2644 const visitor = new Visitor ( new VisitorRepository ( ) , RequestVisitorComment ) ;
2745
2846 const response = await visitor . createComment ( ) ;
2947
30- if ( response )
48+ if ( response ) {
3149 return res . status ( 201 ) . json ( {
3250 statusCode : 201 ,
3351 commentId : response ,
3452 msg : 'Successful visitor comment creation' ,
3553 } ) ;
54+ }
55+ throw new ServerError ( 'Interver Server Error' )
3656 } catch ( err ) {
3757 return errorResposne ( err , res ) ;
3858 }
3959} ;
4060
61+ /**
62+ * API for editing visited comments
63+ * @url /apis/visitor/comment/:id
64+ * @method patch
65+ * @reqParams `{ id: number }` Unique number of edit comment
66+ * @reqBody `{ password: string, description: string }`
67+ * @resBody `{ statusCode: number, msg: string }` success or fail response body
68+ */
4169const updateVisitCommentById = async ( req : Request , res : Response ) => {
4270 const { id : visitorCommentId } = req . params ;
4371
44- const requestVisitorComment = Object . assign ( req . body ) ;
72+ const requestVisitorComment : VisitorCmtDto = Object . assign ( req . body ) ;
4573
4674 try {
4775 const visitor = new Visitor ( new VisitorRepository ( ) , requestVisitorComment ) ;
4876
4977 const response = await visitor . updateCommentById ( Number ( visitorCommentId ) ) ;
5078
51- if ( ! response . success )
79+ if ( ! response . success ) {
5280 return res . status ( 401 ) . json ( { statusCode : 401 , msg : response . msg } ) ;
81+ }
5382 return res . status ( 200 ) . json ( { statusCode : 200 , msg : response . msg } ) ;
5483 } catch ( err ) {
5584 return errorResposne ( err , res ) ;
5685 }
5786} ;
5887
88+ /**
89+ * All visit comment lookup APIs
90+ * @url /apis/visitor/comments
91+ * @method get
92+ * @resBody `{ statusCode: number, visitorComments: [{id: number, nickname: string,
93+ description: string, date: string }] }` success response body
94+ * @resBody `{ statusCode: number, mesg: string }` fail response body
95+ */
5996const getVisitorComments = async ( req : Request , res : Response ) => {
6097 try {
6198 const visitor = new Visitor ( new VisitorRepository ( ) ) ;
6299
63- const response = await visitor . getVisitorComments ( ) ;
100+ const { visitorComments } = await visitor . getVisitorComments ( ) ;
64101
65- return res . status ( 200 ) . json ( { statusCode : 200 , ... response } ) ;
102+ return res . status ( 200 ) . json ( { statusCode : 200 , visitorComments } ) ;
66103 } catch ( err ) {
67104 return errorResposne ( err , res ) ;
68105 }
69106} ;
70107
108+ /**
109+ * @typedef {Object } ResBody
110+ * @property {number } statusCode
111+ * @property {string } msg
112+ */
113+
114+ /**
115+ * Visit comment delete API
116+ * @url /apis/visitor/comment/:id
117+ * @method delete
118+ * @reqParams `{ id: number }` Unique ID of the target to be deleted
119+ * @resBody `{ statusCode: number, msg: string }` success response body
120+ * @resBody `{ statusCode: number, msg: string }` fail response body
121+ */
71122const deleteVisitorCommentById = async ( req : Request , res : Response ) => {
72123 try {
73124 const visitorCommentId = req . params . id ;
74125
75- if ( ! visitorCommentId ) throw new BadRequestError ( 'id params is undefined' ) ;
76-
126+ if ( ! visitorCommentId ) {
127+ throw new BadRequestError ( 'id params is undefined' ) ;
128+ }
77129 const visitor = new Visitor ( new VisitorRepository ( ) ) ;
78130
79131 const response = await visitor . deleteVisitorCommentById (
80132 Number ( visitorCommentId )
81133 ) ;
82134
83- if ( response )
135+ if ( response ) {
84136 return res . status ( 200 ) . json ( {
85137 statusCode : 200 ,
86138 msg : 'Successful deletion of visitor comment' ,
87139 } ) ;
140+ }
88141 } catch ( err ) {
89142 return errorResposne ( err , res ) ;
90143 }
0 commit comments