3
3
import java .io .IOException ;
4
4
import java .net .URI ;
5
5
import java .sql .SQLException ;
6
+ import java .time .LocalDateTime ;
7
+ import java .time .format .DateTimeFormatter ;
6
8
import java .util .List ;
7
9
import java .util .Optional ;
8
10
9
- import org . hibernate . engine . jdbc . BlobProxy ;
11
+
10
12
import org .springframework .beans .factory .annotation .Autowired ;
13
+ import org .springframework .data .domain .PageRequest ;
14
+ import org .springframework .data .domain .Sort ;
15
+ import org .springframework .web .bind .annotation .CrossOrigin ;
16
+ import org .springframework .web .bind .annotation .GetMapping ;
17
+
18
+ import org .springframework .web .bind .annotation .RequestMapping ;
19
+ import org .springframework .web .bind .annotation .RequestParam ;
20
+ import org .springframework .web .bind .annotation .RestController ;
21
+ import org .hibernate .engine .jdbc .BlobProxy ;
11
22
import org .springframework .core .io .ClassPathResource ;
12
23
import org .springframework .core .io .InputStreamResource ;
13
24
import org .springframework .core .io .Resource ;
14
- import org .springframework .data .domain .PageRequest ;
15
25
import org .springframework .http .HttpHeaders ;
16
26
import org .springframework .http .HttpStatus ;
17
27
import org .springframework .http .ResponseEntity ;
18
- import org .springframework .web .bind .annotation .CrossOrigin ;
19
28
import org .springframework .web .bind .annotation .DeleteMapping ;
20
- import org .springframework .web .bind .annotation .GetMapping ;
21
29
import org .springframework .web .bind .annotation .PathVariable ;
22
30
import org .springframework .web .bind .annotation .PostMapping ;
23
31
import org .springframework .web .bind .annotation .PutMapping ;
24
32
import org .springframework .web .bind .annotation .RequestBody ;
25
- import org .springframework .web .bind .annotation .RequestMapping ;
26
- import org .springframework .web .bind .annotation .RequestParam ;
27
- import org .springframework .web .bind .annotation .RestController ;
28
33
import org .springframework .web .multipart .MultipartFile ;
29
34
30
35
import com .fasterxml .jackson .annotation .JsonView ;
33
38
import io .swagger .v3 .oas .annotations .Parameter ;
34
39
import io .swagger .v3 .oas .annotations .media .Content ;
35
40
import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
41
+ import webapp8 .webandtech .model .Order ;
42
+ import webapp8 .webandtech .model .Product ;
43
+ import webapp8 .webandtech .model .User ;
44
+ import webapp8 .webandtech .service .OrderService ;
45
+ import webapp8 .webandtech .service .UserService ;
36
46
import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
37
47
38
48
import static org .springframework .web .servlet .support .ServletUriComponentsBuilder .fromCurrentRequest ;
39
49
40
- import webapp8 .webandtech .model .User ;
41
- import webapp8 .webandtech .service .UserService ;
42
-
43
50
@ RestController
44
51
@ CrossOrigin
45
52
@ RequestMapping ("api/users" )
46
53
public class UserRestControler {
47
54
48
55
@ Autowired
49
56
private UserService userService ;
57
+
58
+ @ Autowired
59
+ private OrderService orderService ;
50
60
51
61
52
- @ Operation (summary = "Get a all users " )
62
+ @ Operation (summary = "Get a user by its id " )
53
63
@ ApiResponses (value = {
54
64
@ ApiResponse (
55
65
responseCode = "200" ,
56
- description = "Found the Users " ,
66
+ description = "Found the User " ,
57
67
content = {@ Content (
58
68
mediaType = "application/json"
59
69
)}
70
+ ),
71
+ @ ApiResponse (
72
+ responseCode = "404" ,
73
+ description = "User not found" ,
74
+ content = @ Content
60
75
)
61
76
})
62
77
@ JsonView (User .Detailed .class )
63
- @ GetMapping ("/" )
64
- public List <User > getAllUsers (){
65
- return userService .getAll ();
78
+ @ GetMapping ("/{id}" )
79
+ public ResponseEntity <User > getUsersById (@ Parameter (description ="id of user to be searched" ) @ PathVariable int id ){
80
+ Optional <User > user = userService .getUserId (id );
81
+ if (!user .isEmpty ()){
82
+ return ResponseEntity .ok (user .get ());
83
+ }else {
84
+ return ResponseEntity .notFound ().build ();
85
+ }
66
86
}
67
-
68
- @ Operation (summary = "Get a user by its id" )
87
+ @ Operation (summary = "Get ordes by iduser" )
69
88
@ ApiResponses (value = {
70
89
@ ApiResponse (
71
90
responseCode = "200" ,
72
- description = "Found the User " ,
91
+ description = "Found the Orders " ,
73
92
content = {@ Content (
74
93
mediaType = "application/json"
75
94
)}
@@ -80,17 +99,69 @@ public List<User> getAllUsers(){
80
99
content = @ Content
81
100
)
82
101
})
83
- @ JsonView (User .Detailed .class )
84
- @ GetMapping ("/{id}" )
85
- public ResponseEntity < User > getUsersById (@ Parameter (description ="id of user to be searched" ) @ PathVariable int id ){
102
+ @ JsonView (Order .Detailed .class )
103
+ @ GetMapping ("/{id}/orders " )
104
+ public List < Order > getOrdersUsers (@ Parameter (description ="id of user to be searched" ) @ PathVariable int id , @ Parameter ( description = "page" ) @ RequestParam ( required = false ) String page ){
86
105
Optional <User > user = userService .getUserId (id );
87
106
if (!user .isEmpty ()){
88
- return ResponseEntity .ok (user .get ());
107
+ User use = user .get ();
108
+ List <Order > orders = orderService .getMoreUserOrders (use , PageRequest .of (Integer .parseInt (page ), 10 ,Sort .by ("idorder" ).descending ())).getContent ();
109
+ System .out .println (orders );
110
+ return orders ;
89
111
}else {
90
- return ResponseEntity . notFound (). build ();
112
+ return orderService . getAll ();
91
113
}
92
114
}
93
115
116
+ @ Operation (summary = "Create a order" )
117
+ @ ApiResponses (value = {
118
+ @ ApiResponse (
119
+ responseCode = "201" ,
120
+ description = "Successful order creation" ,
121
+ content = {@ Content (
122
+ mediaType = "application/json"
123
+ )}
124
+ ),
125
+ @ ApiResponse (
126
+ responseCode = "406 " ,
127
+ description = "Not Acceptable user creation the username or email is token" ,
128
+ content = {@ Content (
129
+ mediaType = "application/json"
130
+ )}
131
+ )
132
+ })
133
+ @ JsonView (User .Detailed .class )
134
+ @ PostMapping ("/{id}/orders" )
135
+ public ResponseEntity <Order > orderUser (@ Parameter (description ="id of user to be searched" ) @ PathVariable int id , @ Parameter (description ="Object Json Type Users" ) @ RequestBody List <Product > products ) throws IOException {
136
+ Optional <User > user = userService .getUserId (id );
137
+ String idsProducts = "" ;
138
+ List <Product > carts = products ;
139
+ float price = 0 ;
140
+ DateTimeFormatter dtf = DateTimeFormatter .ofPattern ("dd/MM/yyyy" );
141
+ dtf .format (LocalDateTime .now ());
142
+ String date = dtf .format (LocalDateTime .now ());
143
+ for (Product cart : carts ) {
144
+ String idP = Integer .toString (cart .getIdproduct ());
145
+ idsProducts = idsProducts +idP +"/" ;
146
+ price = price + cart .getPrice ();
147
+ }
148
+ Order order = new Order ();
149
+ order .setPrice (price );
150
+ order .setIduser (user .get ());
151
+ order .setIdproducts (idsProducts );
152
+ order .setOrderdate (date );
153
+ orderService .saveOrder (order );
154
+
155
+ if (!user .isEmpty ()){
156
+ List <Order > orderList = orderService .getAll ();
157
+ order = orderList .get (0 );
158
+ URI location = fromCurrentRequest ().path ("/{id}" ).buildAndExpand (orderList .get (0 ).getIdorder ()).toUri ();
159
+ return ResponseEntity .created (location ).body (orderList .get (0 ));
160
+ } else {
161
+ return new ResponseEntity <>(order ,HttpStatus .NOT_ACCEPTABLE );
162
+ }
163
+
164
+ }
94
165
@ Operation (summary = "Create a user" )
95
166
@ ApiResponses (value = {
96
167
@ ApiResponse (
@@ -117,8 +188,6 @@ public ResponseEntity<User> registerUser(@Parameter(description="Object Json Typ
117
188
}
118
189
}
119
190
if (!userService .existsUser (user .getUsername ())) {
120
- Resource imagedefault = new ClassPathResource ("/static/images/avatar.png" );
121
- user .setUserimg (BlobProxy .generateProxy (imagedefault .getInputStream (), imagedefault .contentLength ()));
122
191
userService .saveUser (user );
123
192
user = userService .getUser (user .getUsername ());
124
193
URI location = fromCurrentRequest ().path ("/{id}" ).buildAndExpand (user .getIduser ()).toUri ();
@@ -214,26 +283,6 @@ public ResponseEntity<User> deleteUser(@Parameter(description="id of user to be
214
283
}
215
284
}
216
285
217
- @ Operation (summary = "Get a all users type customers" )
218
- @ ApiResponses (value = {
219
- @ ApiResponse (
220
- responseCode = "200" ,
221
- description = "Found the Users type customers" ,
222
- content = {@ Content (
223
- mediaType = "application/json"
224
- )}
225
- )
226
- })
227
- @ JsonView (User .Detailed .class )
228
- @ GetMapping ("/customers" )
229
- public List <User > getUsers ( @ Parameter (description ="page" ) @ RequestParam (required = false ) String page ){
230
- if (page != null ) {
231
- return userService .getCustomers (PageRequest .of (Integer .parseInt (page ), 5 )).getContent ();
232
- }else {
233
- return userService .getAll ();
234
- }
235
- }
236
-
237
286
@ Operation (summary = "Get a profile image user by id" )
238
287
@ ApiResponses (value = {
239
288
@ ApiResponse (
@@ -299,8 +348,9 @@ public ResponseEntity<Object> uploadImageProfile( @Parameter(description="id of
299
348
if (image != null ) {
300
349
user .get ().setUserimg (BlobProxy .generateProxy (image .getInputStream (), image .getSize ()));
301
350
userService .saveUser (user .get ());
302
- URI location = fromCurrentRequest ().build ().toUri ();
303
- return ResponseEntity .created (location ).build ();
351
+ User use = userService .getUser (user .get ().getUsername ());
352
+ URI location = fromCurrentRequest ().path ("/{id}" ).buildAndExpand (use .getIduser ()).toUri ();
353
+ return ResponseEntity .created (location ).body (use );
304
354
}else {
305
355
return ResponseEntity .noContent ().build ();
306
356
}
@@ -311,4 +361,6 @@ public ResponseEntity<Object> uploadImageProfile( @Parameter(description="id of
311
361
312
362
313
363
364
+
365
+
314
366
}
0 commit comments