From accf4a70916444684c2a9b6cfc9955027715d5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E5=BA=94=E5=9D=A4?= Date: Tue, 15 Oct 2024 10:38:14 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E7=A4=BE=E5=8C=BA=E5=88=86=E4=BA=ABAPI?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 闫应坤 --- SocialShare.java | 25 ++++++++++++ SocialShareController.java | 82 ++++++++++++++++++++++++++++++++++++++ SocialShareMapper.java | 11 +++++ SocialShareService.java | 40 +++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 SocialShare.java create mode 100644 SocialShareController.java create mode 100644 SocialShareMapper.java create mode 100644 SocialShareService.java diff --git a/SocialShare.java b/SocialShare.java new file mode 100644 index 00000000..52c29feb --- /dev/null +++ b/SocialShare.java @@ -0,0 +1,25 @@ +package com.yanyingkun.surprisemeal.model; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.util.Date; + +/** + * 社交分享实体类 + */ +@Data +@TableName("socialshare") +public class SocialShare { + + @TableId(value = "shareid", type = IdType.AUTO) + private Integer shareid; + + private Integer userid; + + private String content; + + private Date sharetime; +} diff --git a/SocialShareController.java b/SocialShareController.java new file mode 100644 index 00000000..14a88ee0 --- /dev/null +++ b/SocialShareController.java @@ -0,0 +1,82 @@ +package com.yanyingkun.surprisemeal.controller; + +import com.yanyingkun.surprisemeal.model.SocialShare; +import com.yanyingkun.surprisemeal.service.SocialShareService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/social-shares") +public class SocialShareController { + + @Autowired + private SocialShareService socialShareService; + + @PostMapping + public ResponseEntity addSocialShare(@RequestBody SocialShare socialShare) { + try { + if (socialShare.getContent() == null || socialShare.getContent().trim().isEmpty()) { + return ResponseEntity.badRequest().body("内容不能为空"); + } + int result = socialShareService.addSocialShare(socialShare); + if (result > 0) { + return ResponseEntity.ok("社交分享添加成功"); + } else { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加社交分享失败"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } + + @GetMapping + public ResponseEntity> getAllSocialShares() { + List socialShares = socialShareService.getAllSocialShares(); + return ResponseEntity.ok(socialShares); + } + + @GetMapping("/{id}") + public ResponseEntity getSocialShareById(@PathVariable int id) { + SocialShare socialShare = socialShareService.getSocialShareById(id); + if (socialShare != null) { + return ResponseEntity.ok(socialShare); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + } + + @PutMapping + public ResponseEntity updateSocialShare(@RequestBody SocialShare socialShare) { + try { + if (socialShare.getContent() == null || socialShare.getContent().trim().isEmpty()) { + return ResponseEntity.badRequest().body("内容不能为空"); + } + int result = socialShareService.updateSocialShare(socialShare); + if (result > 0) { + return ResponseEntity.ok("社交分享更新成功"); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("社交分享未找到"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteSocialShareById(@PathVariable int id) { + try { + int result = socialShareService.deleteSocialShareById(id); + if (result > 0) { + return ResponseEntity.ok("社交分享删除成功"); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("社交分享未找到"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } +} diff --git a/SocialShareMapper.java b/SocialShareMapper.java new file mode 100644 index 00000000..f09d946a --- /dev/null +++ b/SocialShareMapper.java @@ -0,0 +1,11 @@ +package com.yanyingkun.surprisemeal.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.yanyingkun.surprisemeal.model.SocialShare; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface SocialShareMapper extends BaseMapper { + // 这里可以添加自定义方法,例如: + // List selectByUserId(String userID); +} diff --git a/SocialShareService.java b/SocialShareService.java new file mode 100644 index 00000000..44f7128e --- /dev/null +++ b/SocialShareService.java @@ -0,0 +1,40 @@ +package com.yanyingkun.surprisemeal.service; + +import com.yanyingkun.surprisemeal.mapper.SocialShareMapper; +import com.yanyingkun.surprisemeal.model.SocialShare; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class SocialShareService { + + @Autowired + private SocialShareMapper socialShareMapper; + + // 添加社交分享 + public int addSocialShare(SocialShare socialShare) { + return socialShareMapper.insert(socialShare); + } + + // 获取所有社交分享 + public List getAllSocialShares() { + return socialShareMapper.selectList(null); + } + + // 根据ID获取社交分享 + public SocialShare getSocialShareById(int id) { + return socialShareMapper.selectById(id); + } + + // 更新社交分享信息 + public int updateSocialShare(SocialShare socialShare) { + return socialShareMapper.updateById(socialShare); + } + + // 根据ID删除社交分享 + public int deleteSocialShareById(int id) { + return socialShareMapper.deleteById(id); + } +} -- Gitee From 856c47de425dd8a50f7177f437647763f441ec9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E5=BA=94=E5=9D=A4?= Date: Sun, 27 Oct 2024 15:49:53 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E9=A4=90=E5=8E=85=E6=94=B6=E8=97=8F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0=20?= =?UTF-8?q?=E6=A6=82=E8=BF=B0=20=EF=BC=881=EF=BC=89=E6=9C=AC=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E6=8F=90=E4=BE=9B=E4=BA=86=E5=85=B3=E4=BA=8E=E2=80=9C?= =?UTF-8?q?=E6=83=8A=E5=96=9C=E4=B8=80=E9=A4=90=E2=80=9D=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=B8=AD=E9=A4=90=E5=8E=85=E6=94=B6=E8=97=8F=E5=8A=9F=E8=83=BD?= =?UTF-8?q?API=E7=9A=84=E8=AF=A6=E7=BB=86=E8=AF=B4=E6=98=8E=E3=80=82?= =?UTF-8?q?=E8=BF=99=E4=BA=9BAPI=E5=85=81=E8=AE=B8=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E9=A4=90=E5=8E=85=E6=94=B6=E8=97=8F=E7=9A=84?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E3=80=81=E6=A3=80=E7=B4=A2=E3=80=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=92=8C=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C=E3=80=82?= =?UTF-8?q?=E6=89=80=E6=9C=89API=E5=9D=87=E9=80=9A=E8=BF=87RESTful?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AE=BE=E8=AE=A1=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E4=BA=86=E6=93=8D=E4=BD=9C=E7=9A=84=E7=9B=B4=E8=A7=82=E6=80=A7?= =?UTF-8?q?=E5=92=8C=E9=AB=98=E6=95=88=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基本信息 (1)项目名称:惊喜一餐 (2)API版本:1.0.0 (3)基础路径:/restaurant-favorites API详细说明 1. 创建餐厅收藏(POST请求) (1)路径:/restaurant-favorites (2)方法:POST (3)请求体: json { "userid": 1, "restaurantid": 101 } (4)描述:创建一个新的餐厅收藏记录。 (5)成功响应: 代码:200 内容示例: json { "success": true, "data": { "favoriteid": 1, "userid": 1, "restaurantid": 101 }, "message": "餐厅收藏添加成功", "code": 200 } (6)错误响应: 代码:400 内容示例: json { "success": false, "message": "用户ID或餐厅ID不能为空", "code": 400 } 2. 获取所有餐厅收藏(GET请求) (1)路径:/restaurant-favorites (2)方法:GET (3)描述:检索系统中所有餐厅收藏的列表。 (4)成功响应: 代码:200 内容示例: json { "success": true, "data": [ { "favoriteid": 1, "userid": 1, "restaurantid": 101 }, ... ], "message": "操作成功", "code": 200 } (5)错误响应: 代码:404 内容示例: json { "success": false, "message": "餐厅收藏列表为空", "code": 404 } 3. 根据ID获取餐厅收藏(GET请求) (1)路径:/restaurant-favorites/{id} (2)方法:GET (3)参数: id:path参数,餐厅收藏的唯一标识符。 (4)描述:根据餐厅收藏ID获取特定餐厅收藏的详细信息。 (5)成功响应: 代码:200 内容示例: json { "success": true, "data": { "favoriteid": 1, "userid": 1, "restaurantid": 101 }, "message": "操作成功", "code": 200 } (6)错误响应: 代码:404 内容示例: json { "success": false, "message": "餐厅收藏不存在", "code": 404 } 4. 更新餐厅收藏信息(PUT请求) (1)路径:/restaurant-favorites (2)方法:PUT (3)请求体: json { "favoriteid": 1, "userid": 1, "restaurantid": 102 } (4)描述:根据餐厅收藏ID更新餐厅收藏信息。 (5)成功响应: 代码:200 内容示例: json { "success": true, "data": { "favoriteid": 1, "userid": 1, "restaurantid": 102 }, "message": "操作成功", "code": 200 } (6)错误响应: 代码:500 内容示例: json { "success": false, "message": "更新餐厅收藏失败", "code": 500 } 5. 删除餐厅收藏(DELETE请求) (1)路径:/restaurant-favorites/{id} (2)方法:DELETE (3)参数: id:path参数,餐厅收藏的唯一标识符。 (4)描述:根据餐厅收藏ID删除餐厅收藏。 (5)成功响应: 代码:200 内容示例: json { "success": true, "data": true, "message": "操作成功", "code": 200 } (6)错误响应: 代码:500 内容示例: json { "success": false, "message": "删除餐厅收藏失败", "code": 500 } 总结:以上API提供了完整的餐厅收藏管理功能,包括创建、检索、更新和删除餐厅收藏。这些API的设计遵循RESTful原则,确保了操作的直观性。 Signed-off-by: 闫应坤 --- RestaurantFavorite.java | 21 ++++++ RestaurantFavoriteController.java | 81 ++++++++++++++++++++++ RestaurantFavoriteControllerTest.java | 98 +++++++++++++++++++++++++++ RestaurantFavoriteMapper.java | 11 +++ RestaurantFavoriteService.java | 40 +++++++++++ 5 files changed, 251 insertions(+) create mode 100644 RestaurantFavorite.java create mode 100644 RestaurantFavoriteController.java create mode 100644 RestaurantFavoriteControllerTest.java create mode 100644 RestaurantFavoriteMapper.java create mode 100644 RestaurantFavoriteService.java diff --git a/RestaurantFavorite.java b/RestaurantFavorite.java new file mode 100644 index 00000000..bf4421ff --- /dev/null +++ b/RestaurantFavorite.java @@ -0,0 +1,21 @@ +package com.yanyingkun.surprisemeal.model; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +/** + * 餐厅收藏实体类 + */ +@Data +@TableName("restaurant_favorites") +public class RestaurantFavorite { + + @TableId(value = "favorite_id", type = IdType.AUTO) + private Integer favoriteId; + + private Integer userId; + + private Integer restaurantId; +} diff --git a/RestaurantFavoriteController.java b/RestaurantFavoriteController.java new file mode 100644 index 00000000..3180fd6f --- /dev/null +++ b/RestaurantFavoriteController.java @@ -0,0 +1,81 @@ +package com.yanyingkun.surprisemeal.controller; + +import com.yanyingkun.surprisemeal.model.RestaurantFavorite; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import com.yanyingkun.surprisemeal.service.RestaurantFavoriteService; +import java.util.List; + +@RestController +@RequestMapping("/restaurant-favorites") +public class RestaurantFavoriteController { + + @Autowired + private RestaurantFavoriteService restaurantFavoriteService; + + @PostMapping + public ResponseEntity addRestaurantFavorite(@RequestBody RestaurantFavorite restaurantFavorite) { + try { + if (restaurantFavorite.getUserId() == null || restaurantFavorite.getRestaurantId() == null) { + return ResponseEntity.badRequest().body("用户ID和餐厅ID不能为空"); + } + int result = restaurantFavoriteService.addRestaurantFavorite(restaurantFavorite); + if (result > 0) { + return ResponseEntity.ok("餐厅收藏添加成功"); + } else { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加餐厅收藏失败"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } + + @GetMapping + public ResponseEntity> getAllRestaurantFavorites(@RequestParam int userId) { + List restaurantFavorites = restaurantFavoriteService.getAllRestaurantFavorites(); + return ResponseEntity.ok(restaurantFavorites); + } + + @GetMapping("/{id}") + public ResponseEntity getRestaurantFavoriteById(@PathVariable int id) { + RestaurantFavorite restaurantFavorite = restaurantFavoriteService.getRestaurantFavoriteById(id); + if (restaurantFavorite != null) { + return ResponseEntity.ok(restaurantFavorite); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + } + + @PutMapping + public ResponseEntity updateRestaurantFavorite(@RequestBody RestaurantFavorite restaurantFavorite) { + try { + if (restaurantFavorite.getUserId() == null || restaurantFavorite.getRestaurantId() == null) { + return ResponseEntity.badRequest().body("用户ID和餐厅ID不能为空"); + } + int result = restaurantFavoriteService.updateRestaurantFavorite(restaurantFavorite); + if (result > 0) { + return ResponseEntity.ok("餐厅收藏更新成功"); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("餐厅收藏未找到"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteRestaurantFavoriteById(@PathVariable int id) { + try { + int result = restaurantFavoriteService.deleteRestaurantFavoriteById(id); + if (result > 0) { + return ResponseEntity.ok("餐厅收藏删除成功"); + } else { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("餐厅收藏未找到"); + } + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("内部服务器错误:" + e.getMessage()); + } + } +} diff --git a/RestaurantFavoriteControllerTest.java b/RestaurantFavoriteControllerTest.java new file mode 100644 index 00000000..8d0117a9 --- /dev/null +++ b/RestaurantFavoriteControllerTest.java @@ -0,0 +1,98 @@ +package com.yanyingkun.surprisemeal; + + +import com.yanyingkun.surprisemeal.model.RestaurantFavorite; +import com.yanyingkun.surprisemeal.service.RestaurantFavoriteService; +import com.yanyingkun.surprisemeal.controller.RestaurantFavoriteController; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.ResponseEntity; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class RestaurantFavoriteControllerTest { + + @Mock + private RestaurantFavoriteService restaurantFavoriteService; + + @InjectMocks + private RestaurantFavoriteController restaurantFavoriteController; + + @Test + public void testAddRestaurantFavoriteSuccess() { + RestaurantFavorite restaurantFavorite = new RestaurantFavorite(); + restaurantFavorite.setUserId(1); + restaurantFavorite.setRestaurantId(1); + + when(restaurantFavoriteService.addRestaurantFavorite(any(RestaurantFavorite.class))).thenReturn(1); + + ResponseEntity response = restaurantFavoriteController.addRestaurantFavorite(restaurantFavorite); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("餐厅收藏添加成功", response.getBody()); + verify(restaurantFavoriteService, times(1)).addRestaurantFavorite(any(RestaurantFavorite.class)); + } + + @Test + public void testGetAllRestaurantFavorites() { + List favorites = Arrays.asList( + new RestaurantFavorite(), + new RestaurantFavorite() + ); + + when(restaurantFavoriteService.getAllRestaurantFavorites()).thenReturn(favorites); + + ResponseEntity> response = restaurantFavoriteController.getAllRestaurantFavorites(1); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals(favorites, response.getBody()); + verify(restaurantFavoriteService, times(1)).getAllRestaurantFavorites(); + } + + @Test + public void testGetRestaurantFavoriteById() { + RestaurantFavorite restaurantFavorite = new RestaurantFavorite(); + + when(restaurantFavoriteService.getRestaurantFavoriteById(1)).thenReturn(restaurantFavorite); + + ResponseEntity response = restaurantFavoriteController.getRestaurantFavoriteById(1); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals(restaurantFavorite, response.getBody()); + verify(restaurantFavoriteService, times(1)).getRestaurantFavoriteById(1); + } + + @Test + public void testUpdateRestaurantFavoriteSuccess() { + RestaurantFavorite restaurantFavorite = new RestaurantFavorite(); + restaurantFavorite.setUserId(1); + restaurantFavorite.setRestaurantId(1); + + when(restaurantFavoriteService.updateRestaurantFavorite(any(RestaurantFavorite.class))).thenReturn(1); + + ResponseEntity response = restaurantFavoriteController.updateRestaurantFavorite(restaurantFavorite); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("餐厅收藏更新成功", response.getBody()); + verify(restaurantFavoriteService, times(1)).updateRestaurantFavorite(any(RestaurantFavorite.class)); + } + + @Test + public void testDeleteRestaurantFavoriteByIdSuccess() { + when(restaurantFavoriteService.deleteRestaurantFavoriteById(1)).thenReturn(1); + + ResponseEntity response = restaurantFavoriteController.deleteRestaurantFavoriteById(1); + + assertEquals(200, response.getStatusCodeValue()); + assertEquals("餐厅收藏删除成功", response.getBody()); + verify(restaurantFavoriteService, times(1)).deleteRestaurantFavoriteById(1); + } +} diff --git a/RestaurantFavoriteMapper.java b/RestaurantFavoriteMapper.java new file mode 100644 index 00000000..c24b5ae8 --- /dev/null +++ b/RestaurantFavoriteMapper.java @@ -0,0 +1,11 @@ +package com.yanyingkun.surprisemeal.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.yanyingkun.surprisemeal.model.RestaurantFavorite; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface RestaurantFavoriteMapper extends BaseMapper { + // 这里可以添加自定义方法,例如: + // List selectByUserId(String userID); +} diff --git a/RestaurantFavoriteService.java b/RestaurantFavoriteService.java new file mode 100644 index 00000000..8dced02a --- /dev/null +++ b/RestaurantFavoriteService.java @@ -0,0 +1,40 @@ +package com.yanyingkun.surprisemeal.service; + +import com.yanyingkun.surprisemeal.mapper.RestaurantFavoriteMapper; +import com.yanyingkun.surprisemeal.model.RestaurantFavorite; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class RestaurantFavoriteService { + + @Autowired + private RestaurantFavoriteMapper restaurantFavoriteMapper; + + // 添加餐厅收藏 + public int addRestaurantFavorite(RestaurantFavorite restaurantFavorite) { + return restaurantFavoriteMapper.insert(restaurantFavorite); + } + + // 获取所有餐厅收藏 + public List getAllRestaurantFavorites() { + return restaurantFavoriteMapper.selectList(null); + } + + // 根据ID获取餐厅收藏 + public RestaurantFavorite getRestaurantFavoriteById(int id) { + return restaurantFavoriteMapper.selectById(id); + } + + // 更新餐厅收藏信息 + public int updateRestaurantFavorite(RestaurantFavorite restaurantFavorite) { + return restaurantFavoriteMapper.updateById(restaurantFavorite); + } + + // 根据ID删除餐厅收藏 + public int deleteRestaurantFavoriteById(int id) { + return restaurantFavoriteMapper.deleteById(id); + } +} -- Gitee