1
+ import express from 'express' ;
2
+ import { orderService } from "../service/index.js" ;
3
+
4
+ /**
5
+ * @route GET /order/:id
6
+ * @desc get order data by order Id
7
+ * @access Private
8
+ */
9
+ const getOrderById = async ( req , res ) => {
10
+ const { id } = req . params ;
11
+ try {
12
+ const getOrderById = await orderService . getOrderById ( id ) ;
13
+ return res . status ( 200 ) . json ( {
14
+ status : 200 ,
15
+ success : true ,
16
+ message : "주문 정보 읽기 성공" ,
17
+ data : getOrderById ,
18
+ } ) ;
19
+ } catch ( error ) {
20
+ console . log ( error ) ;
21
+ res . status ( 400 ) . json ( {
22
+ status : 400 ,
23
+ success : false ,
24
+ message : error . message ,
25
+ } ) ;
26
+ }
27
+ } ;
28
+
29
+ /**
30
+ * @route POST /order
31
+ * @desc post order
32
+ * @access Private
33
+ */
34
+ const postOrder = async ( req , res ) => {
35
+ const data = req . body ;
36
+ try {
37
+ const postOrder = await orderService . postOrder ( data ) ;
38
+ return res . status ( 200 ) . json ( {
39
+ status : 200 ,
40
+ success : true ,
41
+ message : "주문 전송 성공" ,
42
+ data : postOrder ,
43
+ } ) ;
44
+ } catch ( error ) {
45
+ console . log ( error ) ;
46
+ res . status ( 400 ) . json ( {
47
+ status : 400 ,
48
+ success : false ,
49
+ message : error . message ,
50
+ } ) ;
51
+ }
52
+ } ;
53
+
54
+ /**
55
+ * @route GET /order/user/{id}
56
+ * @desc get order LIst data by user Id
57
+ * @access Private
58
+ */
59
+ const getOrderListByUserId = async ( req , res ) => {
60
+ const { id } = req . params ;
61
+ try {
62
+ const getOrderListByUserId = await orderService . getOrderListByUserId ( id ) ;
63
+ return res . status ( 200 ) . json ( {
64
+ status : 200 ,
65
+ success : true ,
66
+ message : "사용자별 주문 리스트 읽기 성공" ,
67
+ data : getOrderListByUserId ,
68
+ } ) ;
69
+ } catch ( error ) {
70
+ console . log ( error ) ;
71
+ res . status ( 400 ) . json ( {
72
+ status : 400 ,
73
+ success : false ,
74
+ message : error . message ,
75
+ } ) ;
76
+ }
77
+ } ;
78
+
79
+ /**
80
+ * @route GET /order/user/{id}
81
+ * @desc get order LIst data by user Id
82
+ * @access Private
83
+ */
84
+ const getOrderListByUserIdWithPage = async ( req , res ) => {
85
+ const { id } = req . params ;
86
+ try {
87
+ const page = req . query . page ;
88
+ const size = req . query . size ;
89
+ const packed = req . query . packed ;
90
+ //쿼리 값이 없으면
91
+ if ( ( page == undefined ) || ( size == undefined ) ) {
92
+ const getOrderListByUserId = await orderService . getOrderListByUserId ( id ) ;
93
+ return res . status ( 200 ) . json ( {
94
+ status : 200 ,
95
+ success : true ,
96
+ message : "사용자별 주문 리스트 전체 읽기 성공" ,
97
+ data : getOrderListByUserId ,
98
+ } ) ;
99
+ }
100
+
101
+ //쿼리 값이 있으면
102
+ if ( page < 1 ) { //0이하면 에러
103
+ return res . status ( 400 ) . json ( {
104
+ status : 400 ,
105
+ success : false ,
106
+ message : '잘못된 요청. 페이지는 1 이상부터 가능' ,
107
+ } ) ;
108
+ }
109
+ if ( ( packed != undefined ) && ( packed != 1 ) ) {
110
+ return res . status ( 400 ) . json ( {
111
+ status : 400 ,
112
+ success : false ,
113
+ message : '잘못된 요청. packed는 1 혹은 undifined 값만을 가짐' ,
114
+ } ) ;
115
+ }
116
+ const getOrderListByUserIdWithPage = await orderService . getOrderListByUserIdWithPage ( id , page , size , packed ) ;
117
+ return res . status ( 200 ) . json ( {
118
+ status : 200 ,
119
+ success : true ,
120
+ message : "페이지 형태로 사용자별 주문 리스트 읽기 성공" ,
121
+ data : getOrderListByUserIdWithPage ,
122
+ } ) ;
123
+ } catch ( error ) {
124
+ console . log ( error ) ;
125
+ res . status ( 400 ) . json ( {
126
+ status : 400 ,
127
+ success : false ,
128
+ message : error . message ,
129
+ } ) ;
130
+ }
131
+ } ;
132
+
133
+ export default {
134
+ getOrderById,
135
+ postOrder,
136
+ getOrderListByUserIdWithPage
137
+ } ;
0 commit comments