diff --git a/src/main/java/com/snhanyue/course/controller/api/CourseController.java b/src/main/java/com/snhanyue/course/controller/api/CourseController.java new file mode 100644 index 0000000000000000000000000000000000000000..6d283772cab83ad643b90ea770db80b508a46fbb --- /dev/null +++ b/src/main/java/com/snhanyue/course/controller/api/CourseController.java @@ -0,0 +1,78 @@ +package com.snhanyue.course.controller.api; + +import com.snhanyue.course.dto.CourseDTO; +import com.snhanyue.course.emum.State; +import com.snhanyue.course.service.api.ICourseService; +import com.snhanyue.course.util.json.JsonResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/api") +public class CourseController { + + @Autowired + ICourseService courseService; + + /** + * 插入课程信息 + * @param courseDTO + * @return + */ + @PostMapping("AddCourse") + public JsonResult AddCourse(CourseDTO courseDTO){ + int i = courseService.AddCourse(courseDTO); + if (i!=1){ + return JsonResult.fail(State.ERR_INSERT,"插入失败请稍后重试"); + } + return JsonResult.ok(); + } + + /** + * 删除id对应课程信息 + * @param id + * @return + */ + @PostMapping("DeleteCourse") + public JsonResult DeleteCourse(Integer id){ + int i = courseService.DeleteCourse(id); + if (i!=1){ + return JsonResult.fail(State.ERR_USER_NAME_DOES_NOT_EXIST,"删除失败请稍后重试"); + } + return JsonResult.ok(); + } + + /** + * 更新id对应课程信息 + * @param id + * @param course + * @return + */ + @PostMapping("UpdateCourse") + public JsonResult UpdateCourse(Integer id, String course){ + int i = 0; + if (id!=0||!course.isEmpty()) { + i = courseService.UpdateCourse(id, course); + } + if (i!=1||course.isEmpty()){ + return JsonResult.fail(State.ERR_CATEGORY_NAME_DUPLICATE,"更新失败请稍后重试"); + } + return JsonResult.ok(); + } + + /** + * 查询id对应课程信息 + * @param id + * @return + */ + @PostMapping("SelectCourse") + public Object SelectCourse(Integer id){ + if (id==0||id!=courseService.SelectCourse(id).getId()){ + return JsonResult.fail(State.ERR_UNKNOWN_ERROR,"查询出错或id不存在请稍后重试"); + } + return courseService.SelectCourse(id); + } + +} diff --git a/src/main/java/com/snhanyue/course/controller/api/NewsController.java b/src/main/java/com/snhanyue/course/controller/api/NewsController.java index acd4c5d4ebcd01214ddbe94d8e490a355348ffa8..0251bc514b12a46e75c13f2183c82db490c3be3d 100644 --- a/src/main/java/com/snhanyue/course/controller/api/NewsController.java +++ b/src/main/java/com/snhanyue/course/controller/api/NewsController.java @@ -1,6 +1,7 @@ package com.snhanyue.course.controller.api; import com.snhanyue.course.dto.NewsDTO; +import com.snhanyue.course.emum.State; import com.snhanyue.course.service.api.INewsService; import com.snhanyue.course.util.json.JsonResult; import com.snhanyue.course.vo.NewsVO; @@ -15,28 +16,59 @@ public class NewsController { @Autowired INewsService iNewsService; + /** + * 添加信息 + * + */ @PostMapping("/News/addNews") - public JsonResult addNews(@Validated NewsDTO newsDTO){ - iNewsService.addNews(newsDTO); + public JsonResult addNews(@Validated NewsDTO newsDTO) { + int result = iNewsService.addNews(newsDTO); + if (result != 1) { + return JsonResult.fail(State.ERR_INSERT, "添加失败!请稍后重试"); + } return JsonResult.ok(); + } - @PostMapping("/News/DeleteNews") - public JsonResult deleteNews(int id){ - iNewsService.deleteNews(id); - return JsonResult.ok("已成功删除"+id); + /** + * 删除id对应文章信息 + * + */ + @PostMapping("/News/DeleteNewsById") + public JsonResult deleteNews(int id) { + int ids = new NewsVO().getId(); + if (id == 0 || id != ids) { + return JsonResult.fail(State.ERR_USER_NAME_DOES_NOT_EXIST, id + "作者id不存在"); + } else { + iNewsService.deleteNewsById(id); + return JsonResult.ok(); + } } - @PostMapping("/News/UpdateNews") - public JsonResult updateNews(int id,String title){ + /** + * 更改id对应文章标题 + * + */ + @PostMapping("/News/UpdateNewsById") + public JsonResult updateNews(int id, String title) { + if (id == 0 || title.isEmpty()) { + return JsonResult.fail(State.ERR_UPDATE, "id或标题不存在"); + } + iNewsService.updateNewsById(id, title); + return JsonResult.ok(); - int i = iNewsService.updateNews(id, title); - return JsonResult.ok("修改完成"+i+"处"); } - @GetMapping("/News/SelectNews") - public NewsVO selectNews(String title){ - return iNewsService.selectNews(title); + /** + * 获取标题对应文章信息 + * + */ + @GetMapping("/News/GetNewsByTitle") + public Object selectNews(String title) { + if (title.isEmpty() || !title.equals(iNewsService.getNewsByTitle(title).getTitle())) { + return JsonResult.fail(State.ERR_UNKNOWN_ERROR, "查询出错或标题不存在"); + } + return iNewsService.getNewsByTitle(title); } diff --git a/src/main/java/com/snhanyue/course/dto/CourseDTO.java b/src/main/java/com/snhanyue/course/dto/CourseDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..758c7a4e9894930f4c511dc4825e38cd05be7ca1 --- /dev/null +++ b/src/main/java/com/snhanyue/course/dto/CourseDTO.java @@ -0,0 +1,17 @@ +package com.snhanyue.course.dto; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class CourseDTO implements Serializable { + private int id; + private int isEnable; + private String course; + private String startTime; + private String updateTime; + private String endTime; + private int studentId; + private int teacherId; +} diff --git a/src/main/java/com/snhanyue/course/entity/Course.java b/src/main/java/com/snhanyue/course/entity/Course.java new file mode 100644 index 0000000000000000000000000000000000000000..8c681d7f856f8c4992696bee11b597f61f0ee81d --- /dev/null +++ b/src/main/java/com/snhanyue/course/entity/Course.java @@ -0,0 +1,17 @@ +package com.snhanyue.course.entity; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class Course implements Serializable { + private int id; + private int isEnable; + private String course; + private String startTime; + private String updateTime; + private String endTime; + private int studentId; + private int teacherId; +} diff --git a/src/main/java/com/snhanyue/course/entity/News.java b/src/main/java/com/snhanyue/course/entity/News.java index 0470f162e59e7bd711eea2b9922b7978dec5584c..b108dccb4eae9321f165ddc6b7057c5e8b9a8b3d 100644 --- a/src/main/java/com/snhanyue/course/entity/News.java +++ b/src/main/java/com/snhanyue/course/entity/News.java @@ -2,11 +2,15 @@ package com.snhanyue.course.entity; import lombok.Data; +import javax.validation.constraints.NotNull; import java.io.Serializable; @Data public class News implements Serializable { + + @NotNull private int id; + private String title; private String createTime; private String alterTime; diff --git a/src/main/java/com/snhanyue/course/mapper/database/CourseMapper.java b/src/main/java/com/snhanyue/course/mapper/database/CourseMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..c0e67504e9f8defb43e9feb4e11038f68a8f7c45 --- /dev/null +++ b/src/main/java/com/snhanyue/course/mapper/database/CourseMapper.java @@ -0,0 +1,46 @@ +package com.snhanyue.course.mapper.database; + +import com.snhanyue.course.dto.CourseDTO; +import com.snhanyue.course.vo.CourseVO; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface CourseMapper { + + /** + * 添加课程信息 + * @param courseDTO + * @return + */ + int AddCourse(CourseDTO courseDTO); + + /** + * 删除课程信息 + * @param id + * @return + */ + int DeleteCourse(Integer id); + + /** + * 根据id更改课程信息 + * @param id + * @return + */ + int UpdateCourse(@Param("id") Integer id,@Param("course") String course); + + /** + * 根据id查询课程新息 + * @param id + * @return + */ + CourseVO SelectCourse(Integer id); + + /** + * 查询课程名 + * @param course + * @return + */ + String SelectCourseByCourse(String course); + +} diff --git a/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java b/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java index 77d9c5a5ee1c2c426192213ecd01a753c512d9d6..b2e241f5e700bc3dfaeac65e460718f653a667c9 100644 --- a/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java +++ b/src/main/java/com/snhanyue/course/mapper/database/NewsMapper.java @@ -15,10 +15,33 @@ public interface NewsMapper { */ int insertNews(News news); - NewsVO selectNewsAuthor(String author); + /** + *删除id对应数据 + * @param id + * @return + */ + int deleteByNewsId(int id); - int deleteByTitleInt(int id); + /** + *更改id对应标题 + * @param id + * @param title + * @return + */ + int updateNewsTitle(@Param("id")int id,@Param("title")String title); - int updateNews(@Param("id")int id,@Param("title")String title); + /** + *获取id对应标题 + * @param Title + * @return + */ + NewsVO getBayNewsTitle(String Title); + + /** + * 根据id查询 + * @param id + * @return + */ + NewsVO getNewsById(Integer id); } diff --git a/src/main/java/com/snhanyue/course/service/api/ICourseService.java b/src/main/java/com/snhanyue/course/service/api/ICourseService.java new file mode 100644 index 0000000000000000000000000000000000000000..c1b0442f9f2f0d812410d4c77ec33cb5541ed5ec --- /dev/null +++ b/src/main/java/com/snhanyue/course/service/api/ICourseService.java @@ -0,0 +1,39 @@ +package com.snhanyue.course.service.api; + +import com.snhanyue.course.dto.CourseDTO; +import com.snhanyue.course.vo.CourseVO; + +public interface ICourseService { + + /** + * 插入课程信息 + * @param courseDTO + * @return + */ + int AddCourse(CourseDTO courseDTO); + + /** + * 根据id删除课程信息 + * @param id + * @return + */ + int DeleteCourse(Integer id); + + /** + * 根据id更改课程信息 + * @param id + * @param course + * @return + */ + int UpdateCourse(Integer id,String course); + + /** + * 根据id查询课程信息 + * @param id + * @return + */ + CourseVO SelectCourse(Integer id); + + + +} diff --git a/src/main/java/com/snhanyue/course/service/api/INewsService.java b/src/main/java/com/snhanyue/course/service/api/INewsService.java index 7e97b4ccb4414dc9814f9d1af302eba09c404edb..e4751c84cee63f518fdbce392431dbdab60615e9 100644 --- a/src/main/java/com/snhanyue/course/service/api/INewsService.java +++ b/src/main/java/com/snhanyue/course/service/api/INewsService.java @@ -4,12 +4,32 @@ import com.snhanyue.course.dto.NewsDTO; import com.snhanyue.course.vo.NewsVO; public interface INewsService { - void addNews(NewsDTO newsDTO); + /** + * 给Dto插入获取的数据 + * @param newsDTO + */ + int addNews(NewsDTO newsDTO); - int deleteNews(int id); + /** + * 删除id对应信息 + * @param id + */ + void deleteNewsById(int id); + + /** + * 更新id对应标题 + * @param id + * @param title + */ + void updateNewsById(int id, String title); + + /** + * 获取标题 + * @param title + * @return + */ + NewsVO getNewsByTitle(String title); - int updateNews(int id,String title); - NewsVO selectNews(String title); } diff --git a/src/main/java/com/snhanyue/course/service/api/impl/CourseServiceImpl.java b/src/main/java/com/snhanyue/course/service/api/impl/CourseServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..b48269c13096fa8dbfb53c7b1b7e4f15279335d2 --- /dev/null +++ b/src/main/java/com/snhanyue/course/service/api/impl/CourseServiceImpl.java @@ -0,0 +1,73 @@ +package com.snhanyue.course.service.api.impl; + +import com.snhanyue.course.dto.CourseDTO; +import com.snhanyue.course.mapper.database.CourseMapper; +import com.snhanyue.course.service.api.ICourseService; +import com.snhanyue.course.vo.CourseVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class CourseServiceImpl implements ICourseService { + + @Autowired + CourseMapper courseMapper; + + /** + * 插入课程信息 + * @param courseDTO + * @return + */ + @Override + public int AddCourse(CourseDTO courseDTO) { + int i = courseMapper.AddCourse(courseDTO); + CourseVO courseVO = new CourseVO(); + String course = courseMapper.SelectCourseByCourse(courseVO.getCourse()); + String courseD = courseDTO.getCourse(); + if (i != 0||course.equals(courseD)) { + return 1; + } + return 0; + } + + /** + * 删除id对应课程信息 + * @param id + * @return + */ + @Override + public int DeleteCourse(Integer id) { + int i = courseMapper.DeleteCourse(id); + if (i!=0){ + return 1; + } + return 0; + + } + + /** + * 更新id对应课程信息 + * @param id + * @param course + * @return + */ + @Override + public int UpdateCourse(Integer id, String course) { + int i = courseMapper.UpdateCourse(id,course); + if (i!=0){ + return 1; + } + return 0; + + } + + /** + * 根据id查询课程信息 + * @param id + * @return + */ + @Override + public CourseVO SelectCourse(Integer id) { + return courseMapper.SelectCourse(id); + } +} 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 index 3fb5e454516d7f529334f646a4f05aab7b59b17a..58c0b680c3e01b2f6924bf01021fe3b29584570e 100644 --- a/src/main/java/com/snhanyue/course/service/api/impl/NewsServiceImpl.java +++ b/src/main/java/com/snhanyue/course/service/api/impl/NewsServiceImpl.java @@ -12,44 +12,56 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service - public class NewsServiceImpl implements INewsService { @Autowired NewsMapper newsMapper; - //取出文章作者名 + /** + * 给DTO添加信息 + */ @Override - public void addNews(NewsDTO newsDTO) { - //获取用户输入的名字 - String author = newsDTO.getAuthor(); - //从数据库进行比对 - NewsVO newsVO = newsMapper.selectNewsAuthor(author); + public int addNews(NewsDTO newsDTO) { + String title = newsDTO.getTitle(); + NewsVO newsVO = newsMapper.getBayNewsTitle(title); 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,"插入数据错误请稍后重试"); } + return 1; } + /** + * 删除id对应信息 + */ @Override - public int deleteNews(int id) { - return newsMapper.deleteByTitleInt(id); + public void deleteNewsById(int id) { + if (id!=0||id==newsMapper.getNewsById(id).getId()) { + newsMapper.deleteByNewsId(id); + } } + /** + * 更新id对应标题 + */ @Override - public int updateNews(int id, String title) { - return newsMapper.updateNews(id, title); + public void updateNewsById(int id, String title) { + if (id!=0||!title.isEmpty()){ + newsMapper.updateNewsTitle(id, title); + } } + /** + * 获取标题对应信息 + */ @Override - public NewsVO selectNews(String title) { - return newsMapper.selectNewsAuthor(title); + public NewsVO getNewsByTitle(String title) { + return newsMapper.getBayNewsTitle(title); } } diff --git a/src/main/java/com/snhanyue/course/vo/CourseVO.java b/src/main/java/com/snhanyue/course/vo/CourseVO.java new file mode 100644 index 0000000000000000000000000000000000000000..b1824405068e36c3e10022d52884a4746c30adc5 --- /dev/null +++ b/src/main/java/com/snhanyue/course/vo/CourseVO.java @@ -0,0 +1,17 @@ +package com.snhanyue.course.vo; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class CourseVO implements Serializable { + private int id; + private int isEnable; + private String course; + private String startTime; + private String updateTime; + private String endTime; + private int studentId; + private int teacherId; +} diff --git a/src/main/resources/mybatis/mapper/CourseMapper.xml b/src/main/resources/mybatis/mapper/CourseMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..f110468319efe11fd12ac87aa90b5d15fc4dbcaa --- /dev/null +++ b/src/main/resources/mybatis/mapper/CourseMapper.xml @@ -0,0 +1,64 @@ + + + + + + + insert into course(id, + is_enable, + course, + start_time, + update_time, + end_time, + student_id, + teacher_id) + values (#{id}, + #{isEnable}, + #{course}, + #{startTime}, + #{updateTime}, + #{endTime}, + #{studentId}, + #{teacherId}) + + + update course + set course=#{course} + where id = #{id} + + + delete + from course + where id = #{id} + + + + + + id, + is_enable, + course, + start_time, + update_time, + end_time, + student_id, + teacher_id + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/mybatis/mapper/NewsMapper.xml b/src/main/resources/mybatis/mapper/NewsMapper.xml index 838dca4338f921f047367d7d758189c46d03c9b5..d67027e6c5b7f5fd2c1100350e2b4d9876b5ac5a 100644 --- a/src/main/resources/mybatis/mapper/NewsMapper.xml +++ b/src/main/resources/mybatis/mapper/NewsMapper.xml @@ -31,19 +31,52 @@ #{releaseTime}, #{forwardingNumber}) - - update news set title = #{title} where id = #{id} - - - delete from news where id=#{id} + + + delete + from news + where id = #{id} - + select + + from news + where title = #{title} + + - + + + id, + title, + create_time, + alter_time, + is_enable, + click_times, + update_status, + admin_id, + summary_article, + author, + article_body, + release_time, + forwarding_number + + + +