后端功能增强:全局异常处理、API控制器、JSP视图和单元测试
- 添加 GlobalExceptionHandler 全局异常处理 - 添加 ApiController REST API 控制器 - 更新 WebConfig 跨域配置和 ProductRepository 查询方法 - 新增 monitor/product-detail/profile JSP 视图页面 - 添加 FlashSaleServiceTest 秒杀服务单元测试 - 更新 application.yml 配置
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
package com.org.flashsalesystem.controller;
|
||||
|
||||
import com.org.flashsalesystem.dto.FlashSaleDTO;
|
||||
import com.org.flashsalesystem.dto.ProductDTO;
|
||||
import com.org.flashsalesystem.entity.Product;
|
||||
import com.org.flashsalesystem.repository.ProductRepository;
|
||||
import com.org.flashsalesystem.service.FlashSaleService;
|
||||
import com.org.flashsalesystem.service.ProductService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* API控制器 - 为Vue前端提供REST接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@Tag(name = "API接口", description = "前端API接口")
|
||||
public class ApiController {
|
||||
|
||||
private final ProductRepository productRepository;
|
||||
private final ProductService productService;
|
||||
private final FlashSaleService flashSaleService;
|
||||
|
||||
/**
|
||||
* 获取热门商品
|
||||
*/
|
||||
@GetMapping("/products/hot")
|
||||
@Operation(summary = "获取热门商品")
|
||||
public ResponseEntity<Map<String, Object>> getHotProducts(
|
||||
@RequestParam(defaultValue = "8") int limit) {
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try {
|
||||
// 获取前N个商品作为热门商品
|
||||
Pageable pageable = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "id"));
|
||||
Page<Product> productPage = productRepository.findAll(pageable);
|
||||
|
||||
List<Map<String, Object>> products = new ArrayList<>();
|
||||
for (Product product : productPage.getContent()) {
|
||||
Map<String, Object> item = new HashMap<>();
|
||||
item.put("id", product.getId());
|
||||
item.put("name", product.getName());
|
||||
item.put("description", product.getDescription());
|
||||
item.put("price", product.getPrice());
|
||||
item.put("stock", product.getStock());
|
||||
item.put("image", product.getImageUrl());
|
||||
item.put("category", "");
|
||||
products.add(item);
|
||||
}
|
||||
|
||||
response.put("success", true);
|
||||
response.put("data", products);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取热门商品失败", e);
|
||||
response.put("success", false);
|
||||
response.put("message", "获取热门商品失败");
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取活跃的秒杀活动
|
||||
*/
|
||||
@GetMapping("/flashsales/active")
|
||||
@Operation(summary = "获取活跃的秒杀活动")
|
||||
public ResponseEntity<Map<String, Object>> getActiveFlashSales() {
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try {
|
||||
List<FlashSaleDTO> flashSales = flashSaleService.getActiveFlashSales();
|
||||
|
||||
// 转换数据格式
|
||||
List<Map<String, Object>> result = new ArrayList<>();
|
||||
for (FlashSaleDTO flashSale : flashSales) {
|
||||
Map<String, Object> item = new HashMap<>();
|
||||
item.put("id", flashSale.getId());
|
||||
item.put("productId", flashSale.getProductId());
|
||||
item.put("productName", flashSale.getProductName());
|
||||
item.put("productImage", "");
|
||||
item.put("originalPrice", flashSale.getOriginalPrice());
|
||||
item.put("flashPrice", flashSale.getFlashPrice());
|
||||
item.put("flashStock", flashSale.getFlashStock());
|
||||
item.put("startTime", flashSale.getStartTime());
|
||||
item.put("endTime", flashSale.getEndTime());
|
||||
item.put("status", flashSale.getStatus());
|
||||
result.add(item);
|
||||
}
|
||||
|
||||
response.put("success", true);
|
||||
response.put("data", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取活跃秒杀活动失败", e);
|
||||
response.put("success", false);
|
||||
response.put("message", "获取活跃秒杀活动失败");
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 参与秒杀
|
||||
*/
|
||||
@PostMapping("/flashsales/participate")
|
||||
@Operation(summary = "参与秒杀")
|
||||
public ResponseEntity<Map<String, Object>> participate(
|
||||
@RequestBody Map<String, Object> request,
|
||||
HttpServletRequest httpRequest) {
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try {
|
||||
// 从session获取用户ID
|
||||
Long userId = (Long) httpRequest.getSession().getAttribute("userId");
|
||||
if (userId == null) {
|
||||
response.put("success", false);
|
||||
response.put("message", "请先登录");
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
Long flashSaleId = Long.valueOf(request.get("flashSaleId").toString());
|
||||
Integer quantity = request.containsKey("quantity") ?
|
||||
Integer.valueOf(request.get("quantity").toString()) : 1;
|
||||
|
||||
// 创建参与DTO
|
||||
FlashSaleDTO.ParticipateDTO participateDTO = new FlashSaleDTO.ParticipateDTO();
|
||||
participateDTO.setFlashSaleId(flashSaleId);
|
||||
participateDTO.setQuantity(quantity);
|
||||
|
||||
// 调用秒杀服务
|
||||
FlashSaleDTO.ResultDTO result = flashSaleService.participateFlashSale(userId, participateDTO);
|
||||
|
||||
response.put("success", result.getSuccess());
|
||||
response.put("message", result.getMessage());
|
||||
if (result.getOrderId() != null) {
|
||||
response.put("orderId", result.getOrderId());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("参与秒杀失败", e);
|
||||
response.put("success", false);
|
||||
response.put("message", e.getMessage());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品列表
|
||||
*/
|
||||
@GetMapping("/products")
|
||||
@Operation(summary = "获取商品列表")
|
||||
public ResponseEntity<Map<String, Object>> getProducts(
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "12") int size) {
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try {
|
||||
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
|
||||
Page<Product> productPage = productRepository.findAll(pageable);
|
||||
|
||||
List<Map<String, Object>> products = new ArrayList<>();
|
||||
for (Product product : productPage.getContent()) {
|
||||
Map<String, Object> item = new HashMap<>();
|
||||
item.put("id", product.getId());
|
||||
item.put("name", product.getName());
|
||||
item.put("description", product.getDescription());
|
||||
item.put("price", product.getPrice());
|
||||
item.put("stock", product.getStock());
|
||||
item.put("image", product.getImageUrl());
|
||||
item.put("category", "");
|
||||
products.add(item);
|
||||
}
|
||||
|
||||
response.put("success", true);
|
||||
response.put("list", products);
|
||||
response.put("total", productPage.getTotalElements());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取商品列表失败", e);
|
||||
response.put("success", false);
|
||||
response.put("message", "获取商品列表失败");
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取秒杀活动列表
|
||||
*/
|
||||
@GetMapping("/flashsales")
|
||||
@Operation(summary = "获取秒杀活动列表")
|
||||
public ResponseEntity<Map<String, Object>> getFlashSales() {
|
||||
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try {
|
||||
List<FlashSaleDTO> flashSales = flashSaleService.getActiveFlashSales();
|
||||
|
||||
response.put("success", true);
|
||||
response.put("list", flashSales);
|
||||
response.put("total", flashSales.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取秒杀活动列表失败", e);
|
||||
response.put("success", false);
|
||||
response.put("message", "获取秒杀活动列表失败");
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user