TemplateGroupController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.gitee.gen.controller;
  2. import com.gitee.gen.common.Action;
  3. import com.gitee.gen.common.Result;
  4. import com.gitee.gen.entity.TemplateGroup;
  5. import com.gitee.gen.service.TemplateGroupService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.List;
  12. import java.util.Objects;
  13. /**
  14. * @author : zsljava
  15. * @date Date : 2020-12-15 9:51
  16. * @Description: TODO
  17. */
  18. @RestController
  19. @RequestMapping("group")
  20. public class TemplateGroupController {
  21. @Autowired
  22. private TemplateGroupService templateGroupService;
  23. /**
  24. * 查询所有记录
  25. *
  26. * @return 返回集合,没有返回空List
  27. */
  28. @RequestMapping("list")
  29. public Result listAll() {
  30. List<TemplateGroup> templateGroups = templateGroupService.listAll();
  31. return Action.ok(templateGroups);
  32. }
  33. /**
  34. * 根据主键查询
  35. *
  36. * @param id 主键
  37. * @return 返回记录,没有返回null
  38. */
  39. @RequestMapping("get/{id}")
  40. public Result get(@PathVariable("id") int id) {
  41. TemplateGroup group = templateGroupService.getById(id);
  42. return Action.ok(group);
  43. }
  44. /**
  45. * 新增,忽略null字段
  46. *
  47. * @param templateGroup 新增的记录
  48. * @return 返回影响行数
  49. */
  50. @RequestMapping("add")
  51. public Result insert(@RequestBody TemplateGroup templateGroup) {
  52. TemplateGroup group = templateGroupService.getByName(templateGroup.getGroupName());
  53. if (group != null) {
  54. throw new RuntimeException(templateGroup.getGroupName() + " 已存在");
  55. }
  56. templateGroupService.insertIgnoreNull(templateGroup);
  57. return Action.ok(templateGroup);
  58. }
  59. /**
  60. * 修改,忽略null字段
  61. *
  62. * @param templateGroup 修改的记录
  63. * @return 返回影响行数
  64. */
  65. @RequestMapping("update")
  66. public Result update(@RequestBody TemplateGroup templateGroup) {
  67. TemplateGroup group = templateGroupService.getByName(templateGroup.getGroupName());
  68. if (group != null && !Objects.equals(group.getId(), templateGroup.getId())) {
  69. throw new RuntimeException(templateGroup.getGroupName() + " 已存在");
  70. }
  71. templateGroupService.updateIgnoreNull(templateGroup);
  72. return Action.ok();
  73. }
  74. /**
  75. * 删除记录
  76. *
  77. * @param templateGroup 待删除的记录
  78. * @return 返回影响行数
  79. */
  80. @RequestMapping("del")
  81. public Result delete(@RequestBody TemplateGroup templateGroup) {
  82. templateGroupService.deleteGroup(templateGroup);
  83. return Action.ok();
  84. }
  85. }