diff --git a/src/main/java/cc/bnblogs/config/ExceptionConfig.java b/src/main/java/cc/bnblogs/config/ExceptionConfig.java new file mode 100644 index 0000000..499ed68 --- /dev/null +++ b/src/main/java/cc/bnblogs/config/ExceptionConfig.java @@ -0,0 +1,20 @@ +package cc.bnblogs.config; + +import cc.bnblogs.common.Result; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/19 + */ +@ControllerAdvice +public class ExceptionConfig { + + @ResponseBody + @ExceptionHandler(Exception.class) + public Result defaultException() { + return Result.error(); + } +} diff --git a/src/main/java/cc/bnblogs/controller/AdminController.java b/src/main/java/cc/bnblogs/controller/AdminController.java index d982159..3c2c9db 100644 --- a/src/main/java/cc/bnblogs/controller/AdminController.java +++ b/src/main/java/cc/bnblogs/controller/AdminController.java @@ -4,7 +4,6 @@ import cc.bnblogs.pojo.Article; import cc.bnblogs.pojo.Category; import cc.bnblogs.service.ArticleService; import cc.bnblogs.service.CategoryService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -61,6 +60,11 @@ public class AdminController { return "admin/comment"; } + @GetMapping("/navigation.html") + public String navigation() { + return "admin/navigation"; + } + // 打开编辑文章界面可以带一个id参数 @GetMapping("/write.html") public String write(@RequestParam(required = false)Integer id, Model model) { diff --git a/src/main/java/cc/bnblogs/controller/BannerController.java b/src/main/java/cc/bnblogs/controller/BannerController.java index 5f4fac6..c3940a6 100644 --- a/src/main/java/cc/bnblogs/controller/BannerController.java +++ b/src/main/java/cc/bnblogs/controller/BannerController.java @@ -73,6 +73,11 @@ public class BannerController { return Result.success(); } + /** + * 对轮播图排序 + * @param ids 编码后的轮播图的id和顺序值:id----ordered; + * @return 重新排序后的轮播图 + */ @PostMapping("/order") public Result ordered(String ids) { Arrays.stream(ids.split(";")).map(x -> x.split("----")). diff --git a/src/main/java/cc/bnblogs/controller/CategoryController.java b/src/main/java/cc/bnblogs/controller/CategoryController.java index 37bdab3..44e3467 100644 --- a/src/main/java/cc/bnblogs/controller/CategoryController.java +++ b/src/main/java/cc/bnblogs/controller/CategoryController.java @@ -67,11 +67,4 @@ public class CategoryController { categoryService.delete(id); return Result.success(); } - - - - - - - } diff --git a/src/main/java/cc/bnblogs/controller/NavigationController.java b/src/main/java/cc/bnblogs/controller/NavigationController.java new file mode 100644 index 0000000..7df1a3d --- /dev/null +++ b/src/main/java/cc/bnblogs/controller/NavigationController.java @@ -0,0 +1,83 @@ +package cc.bnblogs.controller; + +import cc.bnblogs.common.Result; +import cc.bnblogs.enums.ResultEnum; +import cc.bnblogs.pojo.Navigation; +import cc.bnblogs.service.NavigationService; +import org.springframework.web.bind.annotation.*; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/19 + */ +@RestController +@RequestMapping("/admin/navigation") +public class NavigationController { + private final NavigationService navigationService; + + public NavigationController(NavigationService navigationService) { + this.navigationService = navigationService; + } + + /** + * 请求所有导航条的接口 + * @return 返回所有导航条 + */ + @GetMapping("/") + public Result> list() { + return Result.success(navigationService.list()); + } + + /** + * 请求导航条详情的接口 + * @param id 导航条id + * @return 返回导航条的详情 + */ + @GetMapping("/{id}") + public Result detail(@PathVariable Integer id) { + Navigation navigation = navigationService.detail(id); + if (Objects.isNull(navigation)) { + return Result.error(ResultEnum.RESULT_NOT_FOUND); + } else { + return Result.success(navigation); + } + } + + /** + * 保存修改后的导航条数据 + * @param navigation 修改的导航条 + * @return 修改结果 + */ + @PostMapping("/") + public Result save(Navigation navigation) { + navigationService.save(navigation); + return Result.success(); + } + + /** + * 删除一个导航条 + * @param id 删除的导航条id + * @return 删除结果 + */ + @DeleteMapping("/{id}") + public Result delete(@PathVariable Integer id) { + navigationService.delete(id); + return Result.success(); + } + + /** + * 对导航条排序 + * @param ids 编码后的导航条的id和顺序值:id----ordered; + * @return 排序后的导航条 + */ + @PostMapping("/order") + public Result ordered(String ids) { + Arrays.stream(ids.split(";")).map(x -> x.split("----")). + forEach(x -> navigationService.order(Integer.parseInt(x[0]), Integer.parseInt(x[1]))); + return Result.success(); + } +} diff --git a/src/main/java/cc/bnblogs/mapper/BannerMapper.java b/src/main/java/cc/bnblogs/mapper/BannerMapper.java index ede97c4..35834b6 100644 --- a/src/main/java/cc/bnblogs/mapper/BannerMapper.java +++ b/src/main/java/cc/bnblogs/mapper/BannerMapper.java @@ -17,7 +17,7 @@ public interface BannerMapper extends JpaRepository { * @param id 轮播图id * @param ordered 更新后的轮播图顺序值 */ - @Transactional + @Transactional(rollbackOn = {Exception.class}) @Modifying @Query("update Banner set ordered = ?2 where id = ?1") void ordered(Integer id, Integer ordered); diff --git a/src/main/java/cc/bnblogs/mapper/NavigationMapper.java b/src/main/java/cc/bnblogs/mapper/NavigationMapper.java new file mode 100644 index 0000000..08d43ac --- /dev/null +++ b/src/main/java/cc/bnblogs/mapper/NavigationMapper.java @@ -0,0 +1,24 @@ +package cc.bnblogs.mapper; + +import cc.bnblogs.pojo.Navigation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; + +import javax.transaction.Transactional; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/19 + */ +public interface NavigationMapper extends JpaRepository { + /** + * 更新对应id的导航栏的顺序值 + * @param id 导航栏id + * @param ordered 更新后的导航栏顺序值 + */ + @Transactional(rollbackOn = {Exception.class}) + @Modifying + @Query("update Navigation set ordered = ?2 where id = ?1") + void ordered(Integer id, Integer ordered); +} diff --git a/src/main/java/cc/bnblogs/pojo/Navigation.java b/src/main/java/cc/bnblogs/pojo/Navigation.java new file mode 100644 index 0000000..d32e1dc --- /dev/null +++ b/src/main/java/cc/bnblogs/pojo/Navigation.java @@ -0,0 +1,37 @@ +package cc.bnblogs.pojo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/19 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +@Table(name = "blog_navigation") +public class Navigation { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + @Column(unique = true) + // 名称 + private String name; + // 图标 + private String icon; + // 跳转链接 + private String link; + // true: 新标签页打开 false: 当前页打开 + private Boolean linkMode; + // 是否启用 + private Boolean enable; + // 顺序 + private Integer ordered; +} diff --git a/src/main/java/cc/bnblogs/service/ArticleService.java b/src/main/java/cc/bnblogs/service/ArticleService.java index 15dbc0c..a91016f 100644 --- a/src/main/java/cc/bnblogs/service/ArticleService.java +++ b/src/main/java/cc/bnblogs/service/ArticleService.java @@ -67,7 +67,7 @@ public class ArticleService { * 保存文章 * @param article 待保存的文章对象 */ - @Transactional + @Transactional(rollbackFor = {Exception.class}) public void save(Article article) { // 修改更新时间 article.setUpdated(new Date()); diff --git a/src/main/java/cc/bnblogs/service/CategoryService.java b/src/main/java/cc/bnblogs/service/CategoryService.java index cdcfdb9..3c1e1f9 100644 --- a/src/main/java/cc/bnblogs/service/CategoryService.java +++ b/src/main/java/cc/bnblogs/service/CategoryService.java @@ -49,6 +49,7 @@ public class CategoryService { * @param category 待保存的分类对象 */ public void save(Category category) { + System.out.println(category); categoryMapper.save(category); } diff --git a/src/main/java/cc/bnblogs/service/NavigationService.java b/src/main/java/cc/bnblogs/service/NavigationService.java new file mode 100644 index 0000000..5a9134a --- /dev/null +++ b/src/main/java/cc/bnblogs/service/NavigationService.java @@ -0,0 +1,72 @@ +package cc.bnblogs.service; + +import cc.bnblogs.mapper.NavigationMapper; +import cc.bnblogs.pojo.Navigation; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @author zfp@bnblogs.cc + * @createTime: 2022/10/19 + */ +@Service +public class NavigationService { + + private final NavigationMapper navigationMapper; + + public NavigationService(NavigationMapper navigationMapper) { + this.navigationMapper = navigationMapper; + } + + /** + * 获取所有导航条 + * @return 导航条列表 + */ + public List list() { + return navigationMapper.findAll(Sort.by(Sort.Direction.ASC,"ordered")); + } + + /** + * 获取导航条总数 + * @return 导航条总数 + */ + public long count() { + return navigationMapper.count(); + } + + /** + * 根据id查询导航条对象 + * @param id 导航条id + * @return id对应的导航条对象 + */ + public Navigation detail(Integer id) { + return navigationMapper.findById(id).orElse(null); + } + + /** + * 保存导航条 + * @param navigation 待保存的导航条对象 + */ + public void save(Navigation navigation) { + navigationMapper.save(navigation); + } + + /** + * 根据id删除导航条对象 + * @param id 待删除导航条的id + */ + public void delete(Integer id) { + navigationMapper.deleteById(id); + } + + /** + * 对导航条进行排序 + * @param id 导航条id + * @param ordered 导航条顺序值 + */ + public void order(Integer id, Integer ordered) { + navigationMapper.ordered(id,ordered); + } +} diff --git a/src/main/resources/templates/admin/banner.html b/src/main/resources/templates/admin/banner.html index c312095..c1a4c9c 100644 --- a/src/main/resources/templates/admin/banner.html +++ b/src/main/resources/templates/admin/banner.html @@ -58,7 +58,7 @@
-
@@ -120,7 +120,7 @@ align: "center", field: "ordered", formatter: (value, row) => { - return `` } }, diff --git a/src/main/resources/templates/admin/common.html b/src/main/resources/templates/admin/common.html index a692002..8155a02 100644 --- a/src/main/resources/templates/admin/common.html +++ b/src/main/resources/templates/admin/common.html @@ -41,9 +41,9 @@
  • 文章管理
  • 分类管理
  • 轮播图管理
  • -
  • 页面管理
  • 友链管理
  • 评论管理
  • +
  • 导航管理