diff --git a/src/main/java/com/jjyu/SurpriseMealApplication.java b/src/main/java/com/jjyu/SurpriseMealApplication.java index 5679f481e118268c1f7b529445e8d19fa12281f9..ea2af16420e1354dab9eedb2ac887dc7ccc471d0 100644 --- a/src/main/java/com/jjyu/SurpriseMealApplication.java +++ b/src/main/java/com/jjyu/SurpriseMealApplication.java @@ -1,9 +1,11 @@ package com.jjyu; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@MapperScan("com.jjyu.dao") public class SurpriseMealApplication { public static void main(String[] args) { SpringApplication.run(SurpriseMealApplication.class, args); diff --git a/src/main/java/com/jjyu/controller/FeedbackController.java b/src/main/java/com/jjyu/controller/FeedbackController.java new file mode 100644 index 0000000000000000000000000000000000000000..53c02dc887832731814b40d998ba505d484b6408 --- /dev/null +++ b/src/main/java/com/jjyu/controller/FeedbackController.java @@ -0,0 +1,76 @@ +package com.jjyu.controller; + + + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.jjyu.entity.Feedback; +import com.jjyu.entity.Result; +import com.jjyu.service.FeedbackService; +import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.io.Serializable; +import java.util.List; + +/** + * (Feedback)表控制层 + * + * @author makejava + * @since 2024-10-15 18:09:37 + */ +@RestController +@RequestMapping("feedback") +public class FeedbackController{ + /** + * 服务对象 + */ + @Autowired + private FeedbackService feedbackService; + + /** + * 通过主键查询单条数据 + * + * @param id 主键 + * @return 单条数据 + */ + @GetMapping("{id}") + public Result selectOneFeedback(@PathVariable Integer id) { + return Result.success(this.feedbackService.selectOneFeedback(id)); + } + + /** + * 新增数据 + * + * @param feedback 实体对象 + * @return 新增结果 + */ + @PostMapping + public Result insertFeedback(@RequestBody Feedback feedback) { + return Result.success(this.feedbackService.insertFeedback(feedback)); + } + + /** + * 修改数据 + * + * @param feedback 实体对象 + * @return 修改结果 + */ + @PutMapping + public Result updateFeedback(@RequestBody Feedback feedback) { + return Result.success(this.feedbackService.updateFeedback(feedback)); + } + + /** + * 删除数据 + * + * @param id 主键结合 + * @return 删除结果 + */ + @DeleteMapping("{id}") + public Result deleteFeedback(@PathVariable Integer id) { + return Result.success(this.feedbackService.deleteFeedback(id)); + } +} + diff --git a/src/main/java/com/jjyu/controller/OrderController.java b/src/main/java/com/jjyu/controller/OrderController.java index 2d77736a4fd32e7886b5f90eaa074ba6f73f68c9..4dd492862056e41ccf011eef0a15776c256a0126 100644 --- a/src/main/java/com/jjyu/controller/OrderController.java +++ b/src/main/java/com/jjyu/controller/OrderController.java @@ -15,9 +15,7 @@ public class OrderController { @PostMapping("/create") public Result createOrder(@RequestParam Integer userId, @RequestBody HashMap orderItems) { - if (orderService.createOrder(userId, orderItems)) { - return Result.success("create order success"); - } - return Result.error("create order failed"); + + return Result.success(orderService.createOrder(userId, orderItems)); } -} +} \ No newline at end of file diff --git a/src/main/java/com/jjyu/entity/Feedback.java b/src/main/java/com/jjyu/entity/Feedback.java index e787ae9945712a9cbeadaf38af49223d76605423..f27ba33f305a69a2ac5b074a8964f7b34c4133d1 100644 --- a/src/main/java/com/jjyu/entity/Feedback.java +++ b/src/main/java/com/jjyu/entity/Feedback.java @@ -1,5 +1,8 @@ package com.jjyu.entity; +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -8,12 +11,15 @@ import java.time.LocalDateTime; @Data @TableName("feedback") public class Feedback { - private int feedbackId; // 反馈ID - private int userId; // 用户ID - private int restaurantId; // 餐馆ID + @TableId + private Integer feedbackId; // 反馈ID + private Integer userId; // 用户ID + private Integer restaurantId; // 餐馆ID private String comment; // 用户反馈 - private double rating; // 用户评分 + private Double rating; // 用户评分 + @TableField(fill = FieldFill.INSERT_UPDATE) private LocalDateTime createdAt; // 创建时间 + @TableField(fill = FieldFill.UPDATE) private LocalDateTime updatedAt; // 更新时间 diff --git a/src/main/java/com/jjyu/entity/Order.java b/src/main/java/com/jjyu/entity/Order.java index baf9a579a2d7355a6b0221fde77ef807ca3aabd2..39dbdb9aaed98e79632f877965eac35297d8ec4d 100644 --- a/src/main/java/com/jjyu/entity/Order.java +++ b/src/main/java/com/jjyu/entity/Order.java @@ -13,7 +13,7 @@ public class Order { @TableId private Integer orderId; private Integer userId; - private BigDecimal totalAmount; + private Double totalAmount; private LocalDateTime createdAt; private LocalDateTime updatedAt; } \ No newline at end of file diff --git a/src/main/java/com/jjyu/exception/BusinessException.java b/src/main/java/com/jjyu/exception/BusinessException.java new file mode 100644 index 0000000000000000000000000000000000000000..860632f7926d1daa92b28ac8b1fd1cb2ed558106 --- /dev/null +++ b/src/main/java/com/jjyu/exception/BusinessException.java @@ -0,0 +1,7 @@ +package com.jjyu.exception; + +public class BusinessException extends RuntimeException { + public BusinessException(String message) { + super(message); + } +} diff --git a/src/main/java/com/jjyu/handler/GlobalExceptionHandler.java b/src/main/java/com/jjyu/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..33900800b4458f867647f4ebe5d8a14584d057b5 --- /dev/null +++ b/src/main/java/com/jjyu/handler/GlobalExceptionHandler.java @@ -0,0 +1,32 @@ +package com.jjyu.handler; + +import com.jjyu.exception.BusinessException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import com.jjyu.entity.Result; + +@ControllerAdvice +public class GlobalExceptionHandler { + + // 处理自定义的业务异常 + @ExceptionHandler(BusinessException.class) + public ResponseEntity> handleBusinessException(BusinessException ex, WebRequest request) { + Result result = Result.error(ex.getMessage()); + return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(RuntimeException.class) + public ResponseEntity> handleRuntimeException(RuntimeException ex, WebRequest request) { + Result result = Result.error(ex.getMessage()); + return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); + } + // 处理所有其他未捕获的异常 + @ExceptionHandler(Exception.class) + public ResponseEntity> handleGlobalException(Exception ex, WebRequest request) { + Result result = Result.error("服务器错误: " + ex.getMessage()); + return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR); + } +} + diff --git a/src/main/java/com/jjyu/handler/MyMetaObjectHandler.java b/src/main/java/com/jjyu/handler/MyMetaObjectHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..630e6bb7e2a27229b4371c75c4d56802dfde2161 --- /dev/null +++ b/src/main/java/com/jjyu/handler/MyMetaObjectHandler.java @@ -0,0 +1,24 @@ +package com.jjyu.handler; + +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import org.apache.ibatis.reflection.MetaObject; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +@Component +public class MyMetaObjectHandler implements MetaObjectHandler { + + // 插入时填充策略 + @Override + public void insertFill(MetaObject metaObject) { + this.strictInsertFill(metaObject, "createdAt", LocalDateTime::now, LocalDateTime.class); + this.strictInsertFill(metaObject, "updatedAt", LocalDateTime::now, LocalDateTime.class); + } + + // 更新时填充策略 + @Override + public void updateFill(MetaObject metaObject) { + this.strictUpdateFill(metaObject, "updatedAt", LocalDateTime::now, LocalDateTime.class); + } +} diff --git a/src/main/java/com/jjyu/service/FeedbackService.java b/src/main/java/com/jjyu/service/FeedbackService.java new file mode 100644 index 0000000000000000000000000000000000000000..e2e34ca16fe82cbcf5b344d4a19c273cbd548d49 --- /dev/null +++ b/src/main/java/com/jjyu/service/FeedbackService.java @@ -0,0 +1,21 @@ +package com.jjyu.service; + + +import com.baomidou.mybatisplus.extension.service.IService; +import com.jjyu.entity.Feedback; + +/** +* @author 霍如意 +* @description 针对表【feedback】的数据库操作Service +* @createDate 2024-10-15 18:04:38 +*/ +public interface FeedbackService extends IService { + + Object selectOneFeedback(Integer id); + + Object insertFeedback(Feedback feedback); + + Object updateFeedback(Feedback feedback); + + Object deleteFeedback(Integer id); +} diff --git a/src/main/java/com/jjyu/service/OrderService.java b/src/main/java/com/jjyu/service/OrderService.java index 3b9c7b3f1956875083a50a724b60d6d76a5a5847..fa6fc587f49bf502913f0e162a7f9b048451f790 100644 --- a/src/main/java/com/jjyu/service/OrderService.java +++ b/src/main/java/com/jjyu/service/OrderService.java @@ -6,5 +6,5 @@ import com.jjyu.entity.Order; import java.util.HashMap; public interface OrderService extends IService { - public boolean createOrder(Integer userId, HashMap orderItems); + public String createOrder(Integer userId, HashMap orderItems); } diff --git a/src/main/java/com/jjyu/service/impl/FeedbackServiceImpl.java b/src/main/java/com/jjyu/service/impl/FeedbackServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..52bad7c500cefc19a4fa35f75397dfcc89e18f54 --- /dev/null +++ b/src/main/java/com/jjyu/service/impl/FeedbackServiceImpl.java @@ -0,0 +1,43 @@ +package com.jjyu.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jjyu.dao.FeedbackMapper; +import com.jjyu.entity.Feedback; +import com.jjyu.service.FeedbackService; + +import org.springframework.stereotype.Service; + +/** +* @author 霍如意 +* @description 针对表【feedback】的数据库操作Service实现 +* @createDate 2024-10-15 18:04:38 +*/ +@Service +public class FeedbackServiceImpl extends ServiceImpl + implements FeedbackService{ + + + @Override + public Object selectOneFeedback(Integer id) { + return this.getById(id); + } + + @Override + public Object insertFeedback(Feedback feedback) { + return save(feedback); + } + + @Override + public Object updateFeedback(Feedback feedback) { + return updateById(feedback); + } + + @Override + public Object deleteFeedback(Integer id) { + return removeById(id); + } +} + + + + diff --git a/src/main/java/com/jjyu/service/impl/OrderServiceImpl.java b/src/main/java/com/jjyu/service/impl/OrderServiceImpl.java index 752647ac70790c4451d776bac69c0e637c9384e9..92800ebf684c74e65c470ab042f37c20bea85982 100644 --- a/src/main/java/com/jjyu/service/impl/OrderServiceImpl.java +++ b/src/main/java/com/jjyu/service/impl/OrderServiceImpl.java @@ -1,7 +1,10 @@ package com.jjyu.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jjyu.dao.InventoryMapper; +import com.jjyu.dao.OrderItemMapper; import com.jjyu.dao.OrderMapper; +import com.jjyu.dao.UserMapper; import com.jjyu.entity.Inventory; import com.jjyu.entity.Order; import com.jjyu.entity.OrderItem; @@ -17,42 +20,37 @@ import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.HashMap; -import java.util.Random; -import java.util.UUID; @Service public class OrderServiceImpl extends ServiceImpl implements OrderService { @Autowired private OrderMapper orderMapper; @Autowired - private UserService userService; + private OrderItemMapper orderItemMapper; @Autowired - private InventoryService inventoryService; + private UserMapper userMapper; @Autowired - private OrderItemService orderItemService; + private InventoryMapper inventoryMapper; - @Transactional(propagation = Propagation.REQUIRED, - rollbackFor = Exception.class, - noRollbackFor = ArithmeticException.class, - timeout = 4) - public boolean createOrder(Integer userId, HashMap orderItems) { + @Transactional + public String createOrder(Integer userId, HashMap orderItems) { // 检查用户是否存在 - User user = userService.getById(userId); + User user = userMapper.selectById(userId); if (user == null) { - return false; + throw new RuntimeException("用户不存在"); } // 验证餐品库存是否充足并计算订单项金额 Double totalPrice = 0.0; HashMap orderItemPrices = new HashMap<>(); for (Integer itemId : orderItems.keySet()) { - Inventory inventory = inventoryService.getById(itemId); + Inventory inventory = inventoryMapper.selectById(itemId); if (inventory.getQuantity() < orderItems.get(itemId)) { - return false; + throw new RuntimeException("库存不足"); } // 减少库存 inventory.setQuantity(inventory.getQuantity() - orderItems.get(itemId)); - inventoryService.updateById(inventory); + inventoryMapper.updateById(inventory); orderItemPrices.put(inventory, orderItems.get(itemId) * inventory.getPrice()); totalPrice += orderItemPrices.get(inventory); @@ -60,19 +58,9 @@ public class OrderServiceImpl extends ServiceImpl implements } // 创建订单记录 Order order = new Order(); - //随机生成一个订单ID - Integer orderId = new Random().nextInt(100000); - order.setOrderId(orderId); order.setUserId(userId); -// try{ -// Thread.sleep(3100); -// } catch (InterruptedException e) { -// throw new RuntimeException(e); -// } - - //int i = 1 / 0; - order.setTotalAmount(BigDecimal.valueOf(totalPrice)); - orderMapper.insert(order); + order.setTotalAmount(totalPrice); + int orderId = orderMapper.insert(order); // 创建订单项记录 for (Inventory inventory : orderItemPrices.keySet()) { OrderItem orderItem = new OrderItem(); @@ -81,8 +69,8 @@ public class OrderServiceImpl extends ServiceImpl implements orderItem.setRestaurantId(inventory.getRestaurantId()); orderItem.setQuantity(orderItems.get(inventory.getInventoryId())); orderItem.setPrice(BigDecimal.valueOf(orderItemPrices.get(inventory))); - orderItemService.save(orderItem); + orderItemMapper.insert(orderItem); } - return true; + return "创建订单成功"; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4ab2a40e69a59a250857d450b08b90fc058e4ddc..50e0a590ba2b676bfcbe6a56c0961d26ed8d8dab 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,7 +3,7 @@ spring: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:mysql://localhost:3306/surprise_meal?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root - password: root + password: Hry303108..@# # springdoc-openapi项目配置 springdoc: swagger-ui: diff --git a/src/main/resources/mapper/FeedbackMapper.xml b/src/main/resources/mapper/FeedbackMapper.xml deleted file mode 100644 index ed3f3cbe5a576b4bf8adce7147edbbb6ea7888e0..0000000000000000000000000000000000000000 --- a/src/main/resources/mapper/FeedbackMapper.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/src/test/java/com/jjyu/controller/FeedbackControllerTest.java b/src/test/java/com/jjyu/controller/FeedbackControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c8c0fdd6cd6ecb5b50acbd1769387da040699151 --- /dev/null +++ b/src/test/java/com/jjyu/controller/FeedbackControllerTest.java @@ -0,0 +1,76 @@ +package com.jjyu.controller; + +import com.jjyu.SurpriseMealApplication; +import com.jjyu.entity.Feedback; +import com.jjyu.entity.Result; +import com.jjyu.service.FeedbackService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(classes = SurpriseMealApplication.class) + +class FeedbackControllerIntegrationTest { + + @Autowired + private FeedbackController feedbackController; + + @Autowired + private FeedbackService feedbackService; + + private Feedback feedback; + + @BeforeEach + void setUp() { + feedback = new Feedback(); + feedback.setUserId(1); + feedback.setRestaurantId(1); + feedback.setComment("Great service"); + feedback.setRating(4.5); + } + + @Test + void testInsertFeedback() { + // 插入反馈数据 + Result result = feedbackController.insertFeedback(feedback); + System.out.println("Insert success: " + result); + } + + @Test + void testSelectOneFeedback() { + + // 根据插入的数据 ID 查询 + Result result = feedbackController.selectOneFeedback(333606913); + System.out.println("Select success: " + result); + } + + @Test + void testUpdateFeedback() { + + // 更新插入后的反馈数据 + feedback.setFeedbackId(333606913); + feedback.setComment("Updated comment"); + feedback.setRating(5.0); + + // 调用更新方法 + Result updateResult = feedbackController.updateFeedback(feedback); + System.out.println("Update success: " + updateResult); + + // 再次查询以验证修改是否生效 + Result result = feedbackController.selectOneFeedback(feedback.getFeedbackId()); + System.out.println("Updated feedback: " + result); + } + + @Test + void testDeleteFeedback() { + + // 删除插入的数据 + Result deleteResult = feedbackController.deleteFeedback(333606913); + System.out.println("Delete success: " + deleteResult); + + // 再次查询以验证是否删除成功 + Result result = feedbackController.selectOneFeedback(333606913); + System.out.println("Deleted feedback result: " + result); + } +}