创建 APIController.java
parent
9f18c47514
commit
7134e6c5ff
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.gao.finalhw.controller;
|
||||||
|
|
||||||
|
import com.gao.finalhw.model.Address;
|
||||||
|
import com.gao.finalhw.model.Orders;
|
||||||
|
import com.gao.finalhw.model.UserInfo;
|
||||||
|
import com.gao.finalhw.response.ServerResponseEntity;
|
||||||
|
import com.gao.finalhw.service.AddrService;
|
||||||
|
import com.gao.finalhw.service.CartService;
|
||||||
|
import com.gao.finalhw.service.OrderService;
|
||||||
|
import com.gao.finalhw.service.UserInfoService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api")
|
||||||
|
public class APIController {
|
||||||
|
@Autowired
|
||||||
|
private CartService cartService;
|
||||||
|
@Autowired
|
||||||
|
private UserInfoService userInfoService;
|
||||||
|
@Autowired
|
||||||
|
private OrderService orderService;
|
||||||
|
@Autowired
|
||||||
|
private AddrService addrService;
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/get_cart_count")
|
||||||
|
public ServerResponseEntity<Integer> getCartCount(@RequestParam("user_id") int userId) {
|
||||||
|
try {
|
||||||
|
int cartCount = cartService.getCartCount(userId);
|
||||||
|
return ServerResponseEntity.success(cartCount);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 处理异常,例如用户 ID 不存在的情况
|
||||||
|
return ServerResponseEntity.fail("server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@GetMapping("/get_user_info")
|
||||||
|
public ServerResponseEntity<UserInfo> get_userInfo(@RequestParam("user_id") int userId){
|
||||||
|
UserInfo userInfo = userInfoService.getUserInfo(userId);
|
||||||
|
if (userInfo != null){
|
||||||
|
return ServerResponseEntity.success(userInfo);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("don't query user info by id.");
|
||||||
|
}
|
||||||
|
@GetMapping("/get_order_info")
|
||||||
|
public ServerResponseEntity<Orders> get_OrderInfo(@RequestParam("order_id") int orderId){
|
||||||
|
Orders order = orderService.getOrderInfo(orderId);
|
||||||
|
if (order != null){
|
||||||
|
return ServerResponseEntity.success(order);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("don't query order info by id.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get_order_info/user")
|
||||||
|
public ServerResponseEntity<Orders> get_OrderInfoByUserId(@RequestParam("user_id") int userId){
|
||||||
|
Orders order = orderService.getOrderInfoByUserId(userId);
|
||||||
|
if (order != null){
|
||||||
|
return ServerResponseEntity.success(order);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("don't query order info by id.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get_addr_info")
|
||||||
|
public ServerResponseEntity<Address> get_AddrInfo(@RequestParam("user_id") int userId){
|
||||||
|
Address addressInfo = addrService.getAddrById(userId);
|
||||||
|
if (addressInfo != null){
|
||||||
|
return ServerResponseEntity.success(addressInfo);
|
||||||
|
}
|
||||||
|
return ServerResponseEntity.fail("don't query addr info by id.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue