Browse Source

feat: 通知功能.

eric 2 years ago
parent
commit
2a7fd714b6

+ 52 - 0
src/main/java/com/finikes/oc/base/controller/NoticeController.java

@@ -0,0 +1,52 @@
+package com.finikes.oc.base.controller;
+
+import com.finikes.oc.base.dto.*;
+import com.finikes.oc.base.service.NoticeService;
+import com.finikes.oc.common.ApiResponse;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+
+/**
+ * 通知控制器
+ */
+@RestController
+public class NoticeController {
+
+    private final NoticeService noticeService;
+
+    public NoticeController(NoticeService noticeService) {
+        this.noticeService = noticeService;
+    }
+
+    @PutMapping("notice")
+    public ApiResponse<Integer> createNotice(@RequestBody @Valid NoticeCreateDto dto) {
+        int noticeId = noticeService.createNotice(dto);
+        return ApiResponse.successful(noticeId);
+    }
+
+    @PostMapping("notice")
+    public ApiResponse<Integer> updateNotice(@RequestBody @Valid NoticeUpdateDto dto) {
+        noticeService.updateNotice(dto);
+        return ApiResponse.successful();
+    }
+
+    @GetMapping("notice")
+    public ApiResponse<NoticeViewDto> peekNotice(@RequestParam(name = "noticeId") Integer noticeId) {
+        NoticeViewDto dto = noticeService.peekNotice(noticeId);
+        return ApiResponse.successful(dto);
+    }
+
+    @GetMapping("notices")
+    public ApiResponse<PageResponse<NoticeViewDto>> searchNotice(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
+                                                                 @RequestParam(value = "pageCapacity", defaultValue = "10") int pageCapacity,
+                                                                 @RequestParam(value = "type", required = false) Integer type) {
+        NoticeSearchDto searchDto = new NoticeSearchDto();
+        searchDto.setPage(pageNum);
+        searchDto.setPageCapacity(pageCapacity);
+        searchDto.setType(type);
+        PageResponse<NoticeViewDto> pageResponse = noticeService.searchNotice(searchDto);
+        return ApiResponse.successful(pageResponse);
+    }
+
+}

+ 17 - 0
src/main/java/com/finikes/oc/base/dao/NoticeDao.java

@@ -0,0 +1,17 @@
+package com.finikes.oc.base.dao;
+
+import com.finikes.oc.base.entity.Notice;
+import com.finikes.oc.common.BaseDao;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+@Mapper
+public interface NoticeDao extends BaseDao<Notice, Integer> {
+
+
+    int updateSelective(Notice entity);
+
+    List<Notice> selectByType(@Param("type") Integer type);
+}

+ 64 - 0
src/main/java/com/finikes/oc/base/dto/NoticeCreateDto.java

@@ -0,0 +1,64 @@
+package com.finikes.oc.base.dto;
+
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+public class NoticeCreateDto {
+    @Size(min = 1, max = 2)
+    @NotNull
+    private Integer type;
+    @NotBlank
+    @Max(256)
+    private String title;
+    @NotBlank
+    @Max(256)
+    private String secondTitle;
+    @NotBlank
+    @Max(256)
+    private String content;
+
+    @Override
+    public String toString() {
+        return "NoticeCreateDto{" +
+                "type=" + type +
+                ", title='" + title + '\'' +
+                ", secondTitle='" + secondTitle + '\'' +
+                ", content='" + content + '\'' +
+                '}';
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getSecondTitle() {
+        return secondTitle;
+    }
+
+    public void setSecondTitle(String secondTitle) {
+        this.secondTitle = secondTitle;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+}

+ 41 - 0
src/main/java/com/finikes/oc/base/dto/NoticeSearchDto.java

@@ -0,0 +1,41 @@
+package com.finikes.oc.base.dto;
+
+public class NoticeSearchDto {
+    private Integer type;
+
+    private Integer page;
+    private Integer pageCapacity;
+
+    @Override
+    public String toString() {
+        return "NoticeSearchDto{" +
+                "type=" + type +
+                ", page=" + page +
+                ", pageCapacity=" + pageCapacity +
+                '}';
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public Integer getPage() {
+        return page;
+    }
+
+    public void setPage(Integer page) {
+        this.page = page;
+    }
+
+    public Integer getPageCapacity() {
+        return pageCapacity;
+    }
+
+    public void setPageCapacity(Integer pageCapacity) {
+        this.pageCapacity = pageCapacity;
+    }
+}

+ 75 - 0
src/main/java/com/finikes/oc/base/dto/NoticeUpdateDto.java

@@ -0,0 +1,75 @@
+package com.finikes.oc.base.dto;
+
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+public class NoticeUpdateDto {
+    @NotNull
+    private Integer id;
+    @Size(min = 1, max = 2)
+    @NotNull
+    private Integer type;
+    @NotBlank
+    @Max(256)
+    private String title;
+    @NotBlank
+    @Max(256)
+    private String secondTitle;
+    @NotBlank
+    @Max(256)
+    private String content;
+
+    @Override
+    public String toString() {
+        return "NoticeUpdateDto{" +
+                "id=" + id +
+                ", type=" + type +
+                ", title='" + title + '\'' +
+                ", secondTitle='" + secondTitle + '\'' +
+                ", content='" + content + '\'' +
+                '}';
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getSecondTitle() {
+        return secondTitle;
+    }
+
+    public void setSecondTitle(String secondTitle) {
+        this.secondTitle = secondTitle;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+}

+ 60 - 0
src/main/java/com/finikes/oc/base/dto/NoticeViewDto.java

@@ -0,0 +1,60 @@
+package com.finikes.oc.base.dto;
+
+public class NoticeViewDto {
+    private Integer id;
+    private Integer type;
+    private String title;
+    private String secondTitle;
+    private String content;
+
+    @Override
+    public String toString() {
+        return "NoticeViewDto{" +
+                "id=" + id +
+                ", type=" + type +
+                ", title='" + title + '\'' +
+                ", secondTitle='" + secondTitle + '\'' +
+                ", content='" + content + '\'' +
+                '}';
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public String getSecondTitle() {
+        return secondTitle;
+    }
+
+    public void setSecondTitle(String secondTitle) {
+        this.secondTitle = secondTitle;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+}

+ 32 - 0
src/main/java/com/finikes/oc/base/dto/PageResponse.java

@@ -0,0 +1,32 @@
+package com.finikes.oc.base.dto;
+
+import java.util.List;
+
+public class PageResponse<T> {
+    private Integer pageQuantity;
+    private List<T> list;
+
+    @Override
+    public String toString() {
+        return "PageResponse{" +
+                "pageQuantity=" + pageQuantity +
+                ", list=" + list +
+                '}';
+    }
+
+    public Integer getPageQuantity() {
+        return pageQuantity;
+    }
+
+    public void setPageQuantity(Integer pageQuantity) {
+        this.pageQuantity = pageQuantity;
+    }
+
+    public List<T> getList() {
+        return list;
+    }
+
+    public void setList(List<T> list) {
+        this.list = list;
+    }
+}

+ 67 - 0
src/main/java/com/finikes/oc/base/entity/Notice.java

@@ -11,6 +11,33 @@ public class Notice {
     private String content; // 内容
     private String content; // 内容
     private long publishTime; // 发布时间
     private long publishTime; // 发布时间
 
 
+    private Long createTime;
+
+    private Long updateTime;
+
+    private Integer cid; // 创建人 ID
+
+    private Integer uid; //修改人 ID
+
+    private Integer state;
+
+    @Override
+    public String toString() {
+        return "Notice{" +
+                "id=" + id +
+                ", type=" + type +
+                ", title='" + title + '\'' +
+                ", secondTitle='" + secondTitle + '\'' +
+                ", content='" + content + '\'' +
+                ", publishTime=" + publishTime +
+                ", createTime=" + createTime +
+                ", updateTime=" + updateTime +
+                ", cid=" + cid +
+                ", uid=" + uid +
+                ", state=" + state +
+                '}';
+    }
+
     public int getType() {
     public int getType() {
         return type;
         return type;
     }
     }
@@ -58,4 +85,44 @@ public class Notice {
     public void setId(int id) {
     public void setId(int id) {
         this.id = id;
         this.id = id;
     }
     }
+
+    public Long getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Long createTime) {
+        this.createTime = createTime;
+    }
+
+    public Long getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Long updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    public Integer getCid() {
+        return cid;
+    }
+
+    public void setCid(Integer cid) {
+        this.cid = cid;
+    }
+
+    public Integer getUid() {
+        return uid;
+    }
+
+    public void setUid(Integer uid) {
+        this.uid = uid;
+    }
+
+    public Integer getState() {
+        return state;
+    }
+
+    public void setState(Integer state) {
+        this.state = state;
+    }
 }
 }

+ 21 - 0
src/main/java/com/finikes/oc/base/mapper/NoticeMapper.java

@@ -0,0 +1,21 @@
+package com.finikes.oc.base.mapper;
+
+import com.finikes.oc.base.dto.NoticeCreateDto;
+import com.finikes.oc.base.dto.NoticeUpdateDto;
+import com.finikes.oc.base.dto.NoticeViewDto;
+import com.finikes.oc.base.entity.Notice;
+import org.mapstruct.Mapper;
+import org.mapstruct.factory.Mappers;
+
+@Mapper
+public interface NoticeMapper {
+
+    NoticeMapper INSTANCE = Mappers.getMapper(NoticeMapper.class);
+
+    Notice noticeCreateDtoToNotice(NoticeCreateDto dto);
+
+    Notice noticeUpdateDtoToNotice(NoticeUpdateDto dto);
+
+    NoticeViewDto noticeToNoticeViewDto(Notice entity);
+
+}

+ 43 - 0
src/main/java/com/finikes/oc/base/service/NoticeService.java

@@ -0,0 +1,43 @@
+package com.finikes.oc.base.service;
+
+import com.finikes.oc.base.dto.*;
+
+import java.util.List;
+
+/**
+ * 通知服务
+ */
+public interface NoticeService {
+
+    /**
+     * 创建通知
+     *
+     * @param dto 通知创建数据传输对象
+     * @return 主键
+     */
+    int createNotice(NoticeCreateDto dto);
+
+    /**
+     * 更新通知
+     *
+     * @param dto 通知更新数据传输对象
+     */
+    void updateNotice(NoticeUpdateDto dto);
+
+    /**
+     * 查看通知
+     *
+     * @param noticeId 通知 ID
+     * @return 通知视图数据传输对象
+     */
+    NoticeViewDto peekNotice(Integer noticeId);
+
+    /**
+     * 检索通知
+     *
+     * @param dto 通知检索数据传输对象
+     * @return 通知视图数据传输对象列表
+     */
+    PageResponse<NoticeViewDto> searchNotice(NoticeSearchDto dto);
+
+}

+ 87 - 0
src/main/java/com/finikes/oc/base/service/NoticeServiceImpl.java

@@ -0,0 +1,87 @@
+package com.finikes.oc.base.service;
+
+import com.finikes.oc.base.dao.NoticeDao;
+import com.finikes.oc.base.dto.*;
+import com.finikes.oc.base.entity.Notice;
+import com.finikes.oc.base.entity.Passport;
+import com.finikes.oc.base.mapper.NoticeMapper;
+import com.finikes.oc.common.PassportHelper;
+import com.finikes.oc.common.exception.BusinessException;
+import com.github.pagehelper.Page;
+import com.github.pagehelper.PageHelper;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Service
+public class NoticeServiceImpl implements NoticeService {
+
+    private final NoticeDao noticeDao;
+
+    public NoticeServiceImpl(NoticeDao noticeDao) {
+        this.noticeDao = noticeDao;
+    }
+
+    @Override
+    public int createNotice(NoticeCreateDto dto) {
+        Passport passport = PassportHelper.currentPassport();
+        if (passport == null) {
+            throw new BusinessException("获取登录状态异常");
+        }
+
+        long now = System.currentTimeMillis();
+
+        Notice notice = NoticeMapper.INSTANCE.noticeCreateDtoToNotice(dto);
+        notice.setPublishTime(now);
+        notice.setCreateTime(now);
+        notice.setUpdateTime(now);
+        notice.setCid(passport.getId());
+        notice.setUid(passport.getId());
+
+        noticeDao.insert(notice);
+        return notice.getId();
+    }
+
+    @Override
+    public void updateNotice(NoticeUpdateDto dto) {
+        Passport passport = PassportHelper.currentPassport();
+        if (passport == null) {
+            throw new BusinessException("获取登录状态异常");
+        }
+
+        Notice notice = NoticeMapper.INSTANCE.noticeUpdateDtoToNotice(dto);
+        notice.setUid(passport.getId());
+        notice.setUpdateTime(System.currentTimeMillis());
+        int effectRow = noticeDao.updateSelective(notice);
+        if (effectRow != 1) {
+            throw new BusinessException("无此通知");
+        }
+    }
+
+    @Override
+    public NoticeViewDto peekNotice(Integer noticeId) {
+        Notice notice = noticeDao.selectByPrimaryKey(noticeId);
+        if (notice == null) {
+            throw new BusinessException("无此通知");
+        }
+        return NoticeMapper.INSTANCE.noticeToNoticeViewDto(notice);
+    }
+
+    @Override
+    public PageResponse<NoticeViewDto> searchNotice(NoticeSearchDto dto) {
+        try (Page<Notice> page = PageHelper.startPage(dto.getPage(), dto.getPageCapacity())){
+            List<Notice> notices = noticeDao.selectByType(dto.getType());
+
+            List<NoticeViewDto> list = notices.stream()
+                    .map(NoticeMapper.INSTANCE::noticeToNoticeViewDto)
+                    .collect(Collectors.toList());
+
+            PageResponse<NoticeViewDto> response = new PageResponse<>();
+            response.setList(list);
+            response.setPageQuantity(page.getPageNum());
+
+            return response;
+        }
+    }
+}

+ 1 - 1
src/main/java/com/finikes/oc/vote/exception/BusinessException.java → src/main/java/com/finikes/oc/common/exception/BusinessException.java

@@ -1,4 +1,4 @@
-package com.finikes.oc.vote.exception;
+package com.finikes.oc.common.exception;
 
 
 /**
 /**
  * 业务异常
  * 业务异常

+ 1 - 1
src/main/java/com/finikes/oc/vote/service/VoteServiceImpl.java

@@ -14,7 +14,7 @@ import com.finikes.oc.vote.entity.Choice;
 import com.finikes.oc.vote.entity.Option;
 import com.finikes.oc.vote.entity.Option;
 import com.finikes.oc.vote.entity.Vote;
 import com.finikes.oc.vote.entity.Vote;
 import com.finikes.oc.vote.entity.VoteActivity;
 import com.finikes.oc.vote.entity.VoteActivity;
-import com.finikes.oc.vote.exception.BusinessException;
+import com.finikes.oc.common.exception.BusinessException;
 import com.finikes.oc.vote.mapper.VoteActivityMapper;
 import com.finikes.oc.vote.mapper.VoteActivityMapper;
 import com.finikes.oc.vote.mapper.VoteMapper;
 import com.finikes.oc.vote.mapper.VoteMapper;
 import com.github.pagehelper.Page;
 import com.github.pagehelper.Page;

+ 2 - 0
src/main/resources/application.yml

@@ -18,5 +18,7 @@ logging:
     com:
     com:
       finikes:
       finikes:
         oc:
         oc:
+          base:
+            dao: debug
           vote:
           vote:
             dao: debug
             dao: debug

+ 76 - 0
src/main/resources/mapper/NoticeMapper.xml

@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.finikes.oc.base.dao.NoticeDao">
+
+    <insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id"
+            parameterType="com.finikes.oc.base.entity.Notice">
+        INSERT INTO t_notice (type, title ,
+        second_title, content,
+        publish_time, create_time, update_time, cid, uid)
+        VALUES (#{type}, #{title} ,
+        #{secondTitle}, #{content},
+        #{publishTime}, #{createTime}, #{updateTime}, #{cid}, #{uid})
+    </insert>
+
+    <update id="update" parameterType="com.finikes.oc.base.entity.Notice">
+        UPDATE t_notice SET type = #{type}, title = #{title},
+        second_title = #{secondTitle}, content = #{content}, publish_time = #{publishTime},
+        create_time = #{createTime}, update_time = #{updateTime}, cid = #{cid}, uid = #{uid}
+        WHERE id = #{id}
+    </update>
+    <update id="updateSelective">
+        UPDATE t_notice
+        <set>
+            <if test="type != null">
+                type = #{type},
+            </if>
+            <if test="title != null">
+                title = #{title},
+            </if>
+            <if test="secondTitle != null">
+                second_title = #{secondTitle},
+            </if>
+            <if test="content != null">
+                content = #{content},
+            </if>
+            <if test="publishTime != null">
+                publish_time = #{publishTime},
+            </if>
+            <if test="createTime != null">
+                create_time = #{createTime},
+            </if>
+            <if test="updateTime != null">
+                update_time = #{updateTime},
+            </if>
+            <if test="cid">
+                cid = #{cid},
+            </if>
+            <if test="uid">
+                uid = #{uid}
+            </if>
+        </set>
+        WHERE id = #{id}
+    </update>
+
+    <select id="selectByPrimaryKey" parameterType="integer" resultType="com.finikes.oc.base.entity.Notice">
+        SELECT id, type, title , second_title, content, publish_time, create_time, update_time, cid, uid
+        FROM t_notice
+        WHERE id = #{primaryKey}
+    </select>
+    <select id="selectByType" resultType="com.finikes.oc.base.entity.Notice">
+        SELECT id, type, title , second_title, content, publish_time, create_time, update_time, cid, uid
+        FROM t_notice
+        <where>
+            <if test="type != null">
+                type = #{type}
+            </if>
+        </where>
+    </select>
+
+    <delete id="deleteByPrimaryKey" parameterType="integer">
+        DELETE
+        FROM t_notice
+        WHERE id = #{primaryKey}
+    </delete>
+
+</mapper>

+ 86 - 0
src/test/java/com/finikes/oc/base/dao/NoticeDaoTest.java

@@ -0,0 +1,86 @@
+package com.finikes.oc.base.dao;
+
+import com.finikes.oc.App;
+import com.finikes.oc.base.entity.Notice;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+import java.util.UUID;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = App.class)
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class NoticeDaoTest {
+
+    @Autowired
+    private NoticeDao noticeDao;
+
+    private static Integer noticeId = 0;
+
+    @Order(1)
+    @Test
+    void insert() {
+        Notice notice = new Notice();
+        notice.setTitle(UUID.randomUUID().toString());
+        notice.setSecondTitle(UUID.randomUUID().toString());
+        notice.setContent(UUID.randomUUID().toString());
+        noticeDao.insert(notice);
+        noticeId = notice.getId();
+    }
+
+    @Order(2)
+    @Test
+    void selectByPrimaryKey() {
+        Notice notice = noticeDao.selectByPrimaryKey(noticeId);
+        assertNotNull(notice);
+    }
+
+    @Order(3)
+    @Test
+    public void update() {
+        Notice notice = noticeDao.selectByPrimaryKey(noticeId);
+        notice.setTitle(UUID.randomUUID().toString());
+        notice.setSecondTitle(UUID.randomUUID().toString());
+        notice.setContent(UUID.randomUUID().toString());
+        notice.setUid(null);
+        notice.setCid(null);
+        int i = noticeDao.update(notice);
+        assertEquals(i, 1);
+    }
+
+    @Order(4)
+    @Test
+    void updateSelective() {
+        Notice notice = noticeDao.selectByPrimaryKey(noticeId);
+        notice.setTitle(UUID.randomUUID().toString());
+        notice.setSecondTitle(UUID.randomUUID().toString());
+        notice.setContent(UUID.randomUUID().toString());
+        int i = noticeDao.updateSelective(notice);
+        assertEquals(i, 1);
+    }
+
+    @Order(5)
+    @Test
+    void selectByType() {
+        List<Notice> list = noticeDao.selectByType(null);
+        assertNotEquals(list.size(), 0);
+        noticeDao.selectByType(1);
+    }
+
+    @Order(6)
+    @Test
+    void deleteByPrimaryKey(){
+        int i = noticeDao.deleteByPrimaryKey(noticeId);
+        assertEquals(i, 1);
+    }
+
+}