diff --git a/RestaurantFavorite.java b/RestaurantFavorite.java new file mode 100644 index 0000000000000000000000000000000000000000..bf4421ffd3a6cbd93f7a221b4649fb8ebede60d5 --- /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 0000000000000000000000000000000000000000..3180fd6f03a49337eb3e636ad727f9ded96081e2 --- /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 0000000000000000000000000000000000000000..8d0117a9ba6ac21bb4bdcbe047b17969e979b700 --- /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 0000000000000000000000000000000000000000..c24b5ae89502b55945d3edec274e197a0662577b --- /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 0000000000000000000000000000000000000000..8dced02a399317f2acc77c275ddf2d2dda7498c9 --- /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); + } +} diff --git a/SocialShare.java b/SocialShare.java new file mode 100644 index 0000000000000000000000000000000000000000..52c29febaa67517b558fd21bffda6f69edf25237 --- /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 0000000000000000000000000000000000000000..14a88ee07c78adc23531e4c9189b2eed5253c56a --- /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 0000000000000000000000000000000000000000..f09d946a2c46ef881f7ea578002f319e888c49b2 --- /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 0000000000000000000000000000000000000000..44f7128e6bd06a5a8f6d9d50642caf49552bf3fe --- /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); + } +}