Skip to content

Commit 656e71e

Browse files
committed
Update api
1 parent d5045a4 commit 656e71e

22 files changed

+1925
-2391
lines changed

backend/webandtech/src/main/java/webapp8/webandtech/controller/AjaxController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import webapp8.webandtech.model.Order;
1818
import webapp8.webandtech.model.Product;
1919
import webapp8.webandtech.model.User;
20+
import webapp8.webandtech.service.OrderService;
2021
import webapp8.webandtech.service.ProductService;
2122
import webapp8.webandtech.service.UserService;
22-
import webapp8.webandtech.service.OrderService;
2323

2424
@RestController
2525
@CrossOrigin

backend/webandtech/src/main/java/webapp8/webandtech/controller/NavigationController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import webapp8.webandtech.model.Product;
2525
import webapp8.webandtech.model.Rating;
2626
import webapp8.webandtech.model.User;
27-
// import webapp8.webandtech.service.AdminService;
2827
import webapp8.webandtech.service.LoaderService;
2928
import webapp8.webandtech.service.OrderService;
3029
import webapp8.webandtech.service.ProductService;

backend/webandtech/src/main/java/webapp8/webandtech/controller/StoreController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public void createNewOrders(HttpServletResponse response, HttpServletRequest req
101101
order.setOrderdate(date);
102102
orderService.saveOrder(order);
103103
carShop.getCarShop().clear();
104-
//new pdf
105104
response.sendRedirect("/index");
106105

107106
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package webapp8.webandtech.controller.api.admins;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.PageRequest;
7+
import org.springframework.data.domain.Sort;
8+
import org.springframework.web.bind.annotation.CrossOrigin;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import com.fasterxml.jackson.annotation.JsonView;
16+
17+
import io.swagger.v3.oas.annotations.Operation;
18+
import io.swagger.v3.oas.annotations.Parameter;
19+
import io.swagger.v3.oas.annotations.media.Content;
20+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
21+
import webapp8.webandtech.model.Order;
22+
import webapp8.webandtech.model.Statistics;
23+
import webapp8.webandtech.model.User;
24+
import webapp8.webandtech.service.AdminService;
25+
import webapp8.webandtech.service.OrderService;
26+
import webapp8.webandtech.service.UserService;
27+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
28+
29+
@RestController
30+
@CrossOrigin
31+
@RequestMapping("api/admins")
32+
public class AdminRestController {
33+
34+
@Autowired
35+
private UserService userService;
36+
37+
@Autowired
38+
private OrderService orderService;
39+
40+
@Autowired
41+
private AdminService adminService;
42+
43+
44+
@Operation(summary = "Get a all users type customers")
45+
@ApiResponses(value = {
46+
@ApiResponse(
47+
responseCode = "200",
48+
description = "Found the Users type customers",
49+
content = {@Content(
50+
mediaType = "application/json"
51+
)}
52+
)
53+
})
54+
@JsonView(User.Detailed.class)
55+
@GetMapping("/customers")
56+
public List<User> getUsers( @Parameter(description="page") @RequestParam(required = false) String page){
57+
if(page != null) {
58+
return userService.getCustomers(PageRequest.of(Integer.parseInt(page), 10,Sort.by("username").ascending())).getContent();
59+
}else {
60+
return userService.getAll();
61+
}
62+
}
63+
@Operation(summary = "Get a all orders")
64+
@ApiResponses(value = {
65+
@ApiResponse(
66+
responseCode = "200",
67+
description = "Found the orders",
68+
content = {@Content(
69+
mediaType = "application/json"
70+
)}
71+
)
72+
})
73+
@JsonView(Order.Detailed.class)
74+
@GetMapping("/orders")
75+
public List<Order> getOrders( @Parameter(description="page") @RequestParam(required = false) String page){
76+
if(page != null) {
77+
78+
return orderService.getMoreAllOrders(PageRequest.of(Integer.parseInt(page), 10,Sort.by("idorder").ascending())).getContent();
79+
}else {
80+
return orderService.getAll();
81+
}
82+
}
83+
84+
@Operation(summary = "Get a all Statistics")
85+
@ApiResponses(value = {
86+
@ApiResponse(
87+
responseCode = "200",
88+
description = "Found the statistics",
89+
content = {@Content(
90+
mediaType = "application/json"
91+
)}
92+
)
93+
})
94+
@GetMapping("/statistics")
95+
public Statistics getStatistics() {
96+
return adminService.getStatics();
97+
}
98+
99+
}

backend/webandtech/src/main/java/webapp8/webandtech/controller/api/users/UserRestControler.java

Lines changed: 99 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@
33
import java.io.IOException;
44
import java.net.URI;
55
import java.sql.SQLException;
6+
import java.time.LocalDateTime;
7+
import java.time.format.DateTimeFormatter;
68
import java.util.List;
79
import java.util.Optional;
810

9-
import org.hibernate.engine.jdbc.BlobProxy;
11+
1012
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;
1122
import org.springframework.core.io.ClassPathResource;
1223
import org.springframework.core.io.InputStreamResource;
1324
import org.springframework.core.io.Resource;
14-
import org.springframework.data.domain.PageRequest;
1525
import org.springframework.http.HttpHeaders;
1626
import org.springframework.http.HttpStatus;
1727
import org.springframework.http.ResponseEntity;
18-
import org.springframework.web.bind.annotation.CrossOrigin;
1928
import org.springframework.web.bind.annotation.DeleteMapping;
20-
import org.springframework.web.bind.annotation.GetMapping;
2129
import org.springframework.web.bind.annotation.PathVariable;
2230
import org.springframework.web.bind.annotation.PostMapping;
2331
import org.springframework.web.bind.annotation.PutMapping;
2432
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;
2833
import org.springframework.web.multipart.MultipartFile;
2934

3035
import com.fasterxml.jackson.annotation.JsonView;
@@ -33,43 +38,57 @@
3338
import io.swagger.v3.oas.annotations.Parameter;
3439
import io.swagger.v3.oas.annotations.media.Content;
3540
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;
3646
import io.swagger.v3.oas.annotations.responses.ApiResponse;
3747

3848
import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest;
3949

40-
import webapp8.webandtech.model.User;
41-
import webapp8.webandtech.service.UserService;
42-
4350
@RestController
4451
@CrossOrigin
4552
@RequestMapping("api/users")
4653
public class UserRestControler {
4754

4855
@Autowired
4956
private UserService userService;
57+
58+
@Autowired
59+
private OrderService orderService;
5060

5161

52-
@Operation(summary = "Get a all users")
62+
@Operation(summary = "Get a user by its id")
5363
@ApiResponses(value = {
5464
@ApiResponse(
5565
responseCode = "200",
56-
description = "Found the Users",
66+
description = "Found the User",
5767
content = {@Content(
5868
mediaType = "application/json"
5969
)}
70+
),
71+
@ApiResponse(
72+
responseCode = "404",
73+
description = "User not found",
74+
content = @Content
6075
)
6176
})
6277
@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+
}
6686
}
67-
68-
@Operation(summary = "Get a user by its id")
87+
@Operation(summary = "Get ordes by iduser")
6988
@ApiResponses(value = {
7089
@ApiResponse(
7190
responseCode = "200",
72-
description = "Found the User",
91+
description = "Found the Orders",
7392
content = {@Content(
7493
mediaType = "application/json"
7594
)}
@@ -80,17 +99,69 @@ public List<User> getAllUsers(){
8099
content = @Content
81100
)
82101
})
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){
86105
Optional<User> user = userService.getUserId(id);
87106
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;
89111
}else {
90-
return ResponseEntity.notFound().build();
112+
return orderService.getAll();
91113
}
92114
}
93115

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+
}
94165
@Operation(summary = "Create a user")
95166
@ApiResponses(value = {
96167
@ApiResponse(
@@ -117,8 +188,6 @@ public ResponseEntity<User> registerUser(@Parameter(description="Object Json Typ
117188
}
118189
}
119190
if(!userService.existsUser(user.getUsername())) {
120-
Resource imagedefault = new ClassPathResource("/static/images/avatar.png");
121-
user.setUserimg(BlobProxy.generateProxy(imagedefault.getInputStream(), imagedefault.contentLength()));
122191
userService.saveUser(user);
123192
user = userService.getUser(user.getUsername());
124193
URI location = fromCurrentRequest().path("/{id}").buildAndExpand(user.getIduser()).toUri();
@@ -214,26 +283,6 @@ public ResponseEntity<User> deleteUser(@Parameter(description="id of user to be
214283
}
215284
}
216285

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-
237286
@Operation(summary = "Get a profile image user by id")
238287
@ApiResponses(value = {
239288
@ApiResponse(
@@ -299,8 +348,9 @@ public ResponseEntity<Object> uploadImageProfile( @Parameter(description="id of
299348
if(image != null) {
300349
user.get().setUserimg(BlobProxy.generateProxy(image.getInputStream(), image.getSize()));
301350
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);
304354
}else {
305355
return ResponseEntity.noContent().build();
306356
}
@@ -311,4 +361,6 @@ public ResponseEntity<Object> uploadImageProfile( @Parameter(description="id of
311361

312362

313363

364+
365+
314366
}

0 commit comments

Comments
 (0)