diff --git a/src/main/java/com/snhanyue/course/controller/api/NewsController.java b/src/main/java/com/snhanyue/course/controller/api/NewsController.java new file mode 100644 index 0000000000000000000000000000000000000000..acd4c5d4ebcd01214ddbe94d8e490a355348ffa8 --- /dev/null +++ b/src/main/java/com/snhanyue/course/controller/api/NewsController.java @@ -0,0 +1,43 @@ +package com.snhanyue.course.controller.api; + +import com.snhanyue.course.dto.NewsDTO; +import com.snhanyue.course.service.api.INewsService; +import com.snhanyue.course.util.json.JsonResult; +import com.snhanyue.course.vo.NewsVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping(value = "/api") +public class NewsController { + + @Autowired + INewsService iNewsService; + + @PostMapping("/News/addNews") + public JsonResult addNews(@Validated NewsDTO newsDTO){ + iNewsService.addNews(newsDTO); + return JsonResult.ok(); + } + + @PostMapping("/News/DeleteNews") + public JsonResult deleteNews(int id){ + iNewsService.deleteNews(id); + return JsonResult.ok("已成功删除"+id); + } + + @PostMapping("/News/UpdateNews") + public JsonResult updateNews(int id,String title){ + + int i = iNewsService.updateNews(id, title); + return JsonResult.ok("修改完成"+i+"处"); + } + + @GetMapping("/News/SelectNews") + public NewsVO selectNews(String title){ + return iNewsService.selectNews(title); + } + + +} diff --git a/src/main/java/com/snhanyue/course/dto/NewsDTO.java b/src/main/java/com/snhanyue/course/dto/NewsDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..7f83f0e5cc3237bf5da341d22afba4a814631831 --- /dev/null +++ b/src/main/java/com/snhanyue/course/dto/NewsDTO.java @@ -0,0 +1,22 @@ +package com.snhanyue.course.dto; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class NewsDTO implements Serializable { + private int id; + private String title; + private String createTime; + private String alterTime; + private int isEnable; + private int clickTimes; + private int updateStatus; + private int adminId; + private String summaryArticle; + private String author; + private String articleBody; + private String releaseTime; + private int forwardingNumber; +} diff --git a/src/main/java/com/snhanyue/course/entity/News.java b/src/main/java/com/snhanyue/course/entity/News.java new file mode 100644 index 0000000000000000000000000000000000000000..0470f162e59e7bd711eea2b9922b7978dec5584c --- /dev/null +++ b/src/main/java/com/snhanyue/course/entity/News.java @@ -0,0 +1,22 @@ +package com.snhanyue.course.entity; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class News implements Serializable { + private int id; + private String title; + private String createTime; + private String alterTime; + private int isEnable; + private int clickTimes; + private int updateStatus; + private int adminId; + private String summaryArticle; + private String author; + private String articleBody; + private String releaseTime; + private int forwardingNumber; +} diff --git a/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java b/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..77d9c5a5ee1c2c426192213ecd01a753c512d9d6 --- /dev/null +++ b/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java @@ -0,0 +1,24 @@ +package com.snhanyue.course.mapper.database; + +import com.snhanyue.course.entity.News; +import com.snhanyue.course.vo.NewsVO; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface NewsMapper { + + /** + * 插入数据 + * @param news + * @return + */ + int insertNews(News news); + + NewsVO selectNewsAuthor(String author); + + int deleteByTitleInt(int id); + + int updateNews(@Param("id")int id,@Param("title")String title); + +} diff --git a/src/main/java/com/snhanyue/course/service/api/INewsService.java b/src/main/java/com/snhanyue/course/service/api/INewsService.java new file mode 100644 index 0000000000000000000000000000000000000000..7e97b4ccb4414dc9814f9d1af302eba09c404edb --- /dev/null +++ b/src/main/java/com/snhanyue/course/service/api/INewsService.java @@ -0,0 +1,15 @@ +package com.snhanyue.course.service.api; + +import com.snhanyue.course.dto.NewsDTO; +import com.snhanyue.course.vo.NewsVO; + +public interface INewsService { + void addNews(NewsDTO newsDTO); + + int deleteNews(int id); + + int updateNews(int id,String title); + + NewsVO selectNews(String title); + +} diff --git a/src/main/java/com/snhanyue/course/service/api/impl/NewsServiceImpl.java b/src/main/java/com/snhanyue/course/service/api/impl/NewsServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..54039303164f3abae72fa2630d9e603d08fa9a00 --- /dev/null +++ b/src/main/java/com/snhanyue/course/service/api/impl/NewsServiceImpl.java @@ -0,0 +1,56 @@ +package com.snhanyue.course.service.api.impl; + +import com.snhanyue.course.dto.NewsDTO; +import com.snhanyue.course.emum.State; +import com.snhanyue.course.entity.News; +import com.snhanyue.course.exception.ServiceException; +import com.snhanyue.course.mapper.database.NewsMapper; +import com.snhanyue.course.service.api.INewsService; +import com.snhanyue.course.vo.NewsVO; +import org.jetbrains.annotations.NotNull; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service + +public class NewsServiceImpl implements INewsService { + + @Autowired + NewsMapper newsMapper; + + //取出文章作者名 + @Override + public void addNews(NewsDTO newsDTO) { + //获取用户输入的名字 + String author = newsDTO.getAuthor(); + //从数据库进行比对 + NewsVO newsVO = newsMapper.selectNewsAuthor(author); + if (newsVO!=null){ + throw new ServiceException(State.ERR_CATEGORY_NAME_DUPLICATE,"用户名已存在"); + } + News news = new News(); + BeanUtils.copyProperties(newsDTO,news); + // 补全状态 + int i = newsMapper.insertNews(news); + if (i!=1){ + throw new ServiceException(State.ERR_INSERT,"插入数据错误请稍后重试"); + } + } + + @Override + public int deleteNews(int id) { + return newsMapper.deleteByTitleInt(id); + } + + @Override + public int updateNews(int id, String title) { + return newsMapper.updateNews(id, title); + } + + @Override + public NewsVO selectNews(String title) { + return newsMapper.selectNewsAuthor(title); + } + +} diff --git a/src/main/java/com/snhanyue/course/vo/NewsVO.java b/src/main/java/com/snhanyue/course/vo/NewsVO.java new file mode 100644 index 0000000000000000000000000000000000000000..73c4d53d4daf4c9e5df671ac3e90651b72196619 --- /dev/null +++ b/src/main/java/com/snhanyue/course/vo/NewsVO.java @@ -0,0 +1,22 @@ +package com.snhanyue.course.vo; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class NewsVO implements Serializable { + private int id; + private String title; + private String createTime; + private String alterTime; + private int isEnable; + private int clickTimes; + private int updateStatus; + private int adminId; + private String summaryArticle; + private String author; + private String articleBody; + private String releaseTime; + private int forwardingNumber; +} diff --git a/src/main/resources/mybatis/mapper/NewsMapper.xml b/src/main/resources/mybatis/mapper/NewsMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..838dca4338f921f047367d7d758189c46d03c9b5 --- /dev/null +++ b/src/main/resources/mybatis/mapper/NewsMapper.xml @@ -0,0 +1,62 @@ + + + + + + + insert into news(id, + title, + create_time, + alter_time, + is_enable, + click_times, + update_status, + admin_id, + summary_article, + author, + article_body, + release_time, + forwarding_number) + values (#{id}, + #{title}, + #{createTime}, + #{alterTime}, + #{isEnable}, + #{clickTimes}, + #{updateStatus}, + #{adminId}, + #{summaryArticle}, + #{author}, + #{articleBody}, + #{releaseTime}, + #{forwardingNumber}) + + + update news set title = #{title} where id = #{id} + + + + delete from news where id=#{id} + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/snhanyue/mapper/NewsMapperTest.java b/src/test/java/com/snhanyue/mapper/NewsMapperTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0c00de950b12ae131cdd7982c5e509de979b28a1 --- /dev/null +++ b/src/test/java/com/snhanyue/mapper/NewsMapperTest.java @@ -0,0 +1,37 @@ +package com.snhanyue.mapper; + +import com.snhanyue.course.entity.News; +import com.snhanyue.course.mapper.database.NewsMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class NewsMapperTest { + + @Autowired + NewsMapper newsMapper; + + @Test + public void insertNews(){ + News news = new News(); + news.setId(3); + news.setTitle("古筝"); + news.setCreateTime("2021-11-5-2"); + news.setAlterTime("2022-07-6-1"); + news.setIsEnable(1); + news.setClickTimes(123); + news.setUpdateStatus(1); + news.setAdminId(192891237); + news.setSummaryArticle("论一个古筝大师怎么培养的"); + news.setAuthor("季大师"); + news.setArticleBody("要想练好古筝,你首先得下足够大的功夫"); + news.setReleaseTime("2022-08-7-4"); + news.setForwardingNumber(3123); + int i = newsMapper.insertNews(news); + System.out.println(i); + } + + + +}