correct notification, add community

Change-Id: Ifb87c82f5a074b1e4b653fe109bcea403b9848f8
diff --git a/src/main/java/com/g9/g9backend/controller/CommunityController.java b/src/main/java/com/g9/g9backend/controller/CommunityController.java
index 229fe96..ea79dbb 100644
--- a/src/main/java/com/g9/g9backend/controller/CommunityController.java
+++ b/src/main/java/com/g9/g9backend/controller/CommunityController.java
@@ -1,8 +1,25 @@
 package com.g9.g9backend.controller;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.g9.g9backend.pojo.Community;
+import com.g9.g9backend.pojo.DTO.*;
+import com.g9.g9backend.pojo.Subscription;
+import com.g9.g9backend.pojo.Thread;
+import com.g9.g9backend.pojo.ThreadLike;
+import com.g9.g9backend.service.*;
+import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
 
 /**
  * CommunityController 社区控制器类,处理与社区相关的请求
@@ -12,5 +29,250 @@
 @RestController
 public class CommunityController {
 
+    private final CommunityService communityService;
+
+    private final ThreadService threadService;
+
+    private final SubscriptionService subscriptionService;
+
+    private final ThreadLikeService threadLikeService;
+
     private final Logger logger = LoggerFactory.getLogger(CommunityController.class);
+
+    public CommunityController(CommunityService communityService, ThreadService threadService, SubscriptionService subscriptionService, ThreadLikeService threadLikeService) {
+        this.communityService = communityService;
+        this.threadService = threadService;
+        this.threadLikeService = threadLikeService;
+        this.subscriptionService = subscriptionService;
+    }
+
+    @PostMapping(value = "/thread")
+    public ResponseEntity<String> postThread(@RequestBody Thread thread) {
+        thread.setLikes(0);
+        thread.setCommentNumber(0);
+        threadService.save(thread);
+
+        Community community = communityService.getById(thread.getCommunityId());
+        community.setThreadNumber(community.getThreadNumber() + 1);
+        communityService.updateById(community);
+
+        logger.info("帖子已发布");
+
+        return ResponseEntity.ok("");
+    }
+
+    @PostMapping(value = "/thread/like")
+    public ResponseEntity<String> postThreadLike(@RequestBody Map<String, Integer> request) {
+        Integer userId = request.get("userId");
+        Integer threadId = request.get("threadId");
+
+        ThreadLike threadLike = new ThreadLike(userId, threadId);
+        threadLikeService.save(threadLike);
+
+        Thread thread = threadService.getById(threadId);
+        thread.setLikes(thread.getLikes() + 1);
+        threadService.updateById(thread);
+
+        logger.info("点赞成功");
+
+        return ResponseEntity.ok("");
+    }
+
+    @DeleteMapping(value = "/thread")
+    public ResponseEntity<String> deleteThread(@RequestParam Integer threadId) {
+        Thread thread = threadService.getById(threadId);
+        threadService.removeById(threadId);
+
+        Community community = communityService.getById(thread.getCommunityId());
+        community.setThreadNumber(community.getThreadNumber() - 1);
+        communityService.updateById(community);
+
+        logger.info("帖子已删除");
+
+        return ResponseEntity.noContent().build();
+    }
+
+    @DeleteMapping(value = "/thread/like")
+    public ResponseEntity<String> deleteLike(@RequestParam Integer userId, @RequestParam Integer threadId) {
+        LambdaQueryWrapper<ThreadLike> threadLikeQuery = new LambdaQueryWrapper<ThreadLike>()
+                .eq(ThreadLike::getUserId, userId)
+                .eq(ThreadLike::getThreadId, threadId);
+
+        threadLikeService.remove(threadLikeQuery);
+
+        Thread thread = threadService.getById(threadId);
+        thread.setLikes(thread.getLikes() - 1);
+        threadService.updateById(thread);
+
+        logger.info("取消点赞成功");
+
+        return ResponseEntity.noContent().build();
+    }
+
+    @GetMapping(value = "/community")
+    public ResponseEntity<GetCommunityDTO> getCommunity(@RequestParam String searchValue,
+                                                        @RequestParam String type,
+                                                        @RequestParam Integer pageNumber,
+                                                        @RequestParam Integer rows) {
+        Page<Community> communityPage = new Page<>(pageNumber, rows);
+        LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>()
+                .eq(Community::getType, type)
+                .like(StringUtils.isNotBlank(searchValue), Community::getCommunityName, searchValue)
+                .orderByDesc(Community::getHot);
+
+        Page<Community> result = communityService.page(communityPage, communityQuery);
+
+        GetCommunityDTO getCommunityDTO = wrapCommunityPage(result, item -> {
+            GetCommunityDTO.Community community = new GetCommunityDTO.Community();
+            community.setCommunityId(item.getCommunityId());
+            community.setCommunityName(item.getCommunityName());
+            community.setCommunityPicture(item.getCommunityPicture());
+            community.setDescription(item.getDescription());
+            community.setHot(item.getHot());
+            community.setThreadNumber(item.getThreadNumber());
+            community.setResourceId(item.getResourceId());
+            return community;
+        });
+
+        return ResponseEntity.ok(getCommunityDTO);
+    }
+
+    @NotNull
+    private <T> GetCommunityDTO wrapCommunityPage(Page<T> page, Function<T, GetCommunityDTO.Community> mapper) {
+        List<GetCommunityDTO.Community> records = page.getRecords().stream().map(mapper).toList();
+
+        return new GetCommunityDTO(records, (int) page.getTotal(), (int) page.getPages(), (int) page.getCurrent(), (int) page.getSize());
+    }
+
+    @GetMapping(value = "/community/info")
+    public ResponseEntity<Community> getCommunityInfo(@RequestParam Integer communityId) {
+
+        return ResponseEntity.ok(communityService.getById(communityId));
+    }
+
+    @GetMapping(value = "/thread")
+    public ResponseEntity<ThreadDTO> getThread(@RequestParam Integer threadId, @RequestParam Integer userId) {
+        LambdaQueryWrapper<ThreadLike> threadLikeQuery = new LambdaQueryWrapper<ThreadLike>()
+                .eq(ThreadLike::getUserId, userId)
+                .eq(ThreadLike::getThreadId, threadId);
+        Thread thread = threadService.getById(threadId);
+        ThreadDTO threadDTO = new ThreadDTO(
+                threadId,
+                thread.getUserId(),
+                thread.getThreadPicture(),
+                thread.getTitle(),
+                thread.getContent(),
+                thread.getLikes(),
+                threadLikeService.getOne(threadLikeQuery) != null,
+                thread.getCreateAt(),
+                thread.getCommentNumber(),
+                thread.getCommunityId()
+        );
+
+        return ResponseEntity.ok(threadDTO);
+    }
+
+    @GetMapping(value = "/community/threads")
+    public ResponseEntity<GetCommunityThreadsDTO> getCommunityThreads(@RequestParam Integer communityId,
+                                                                      @RequestParam Integer pageNumber,
+                                                                      @RequestParam Integer rows,
+                                                                      @RequestParam String option,
+                                                                      @RequestParam String searchValue,
+                                                                      @RequestParam Integer userId) {
+        Page<Thread> threadPage = new Page<>(pageNumber, rows);
+        LambdaQueryWrapper<Thread> threadQuery = new LambdaQueryWrapper<Thread>()
+                .eq(Thread::getCommunityId, communityId)
+                .like(StringUtils.isNotBlank(searchValue), Thread::getTitle, searchValue);
+
+        if (Objects.equals(option, "最高热度")) {
+
+            threadQuery.orderByDesc(Thread::getLikes);
+        } else {
+
+            List<Integer> userIds = subscriptionService.list(new LambdaQueryWrapper<Subscription>()
+                            .eq(Subscription::getUserId, userId))
+                            .stream()
+                            .map(Subscription::getFollowerId)
+                            .toList();
+
+            threadQuery.in(!userIds.isEmpty(), Thread::getUserId, userIds)
+                    .orderByDesc(Thread::getLikes);
+        }
+
+        Page<Thread> result = threadService.page(threadPage, threadQuery);
+
+        GetCommunityThreadsDTO getCommunityThreadsDTO = wrapThreadPage(result, item -> {
+            GetCommunityThreadsDTO.Thread thread = new GetCommunityThreadsDTO.Thread();
+            thread.setThreadId(item.getThreadId());
+            thread.setUserId(item.getUserId());
+            thread.setThreadPicture(item.getThreadPicture());
+            thread.setTitle(item.getTitle());
+            thread.setLikes(item.getLikes());
+            thread.setCreateAt(item.getCreateAt());
+            return thread;
+        });
+
+        return ResponseEntity.ok(getCommunityThreadsDTO);
+    }
+
+    @NotNull
+    private <T> GetCommunityThreadsDTO wrapThreadPage(Page<T> page, Function<T, GetCommunityThreadsDTO.Thread> mapper) {
+        List<GetCommunityThreadsDTO.Thread> records = page.getRecords().stream().map(mapper).toList();
+
+        return new GetCommunityThreadsDTO(records, (int) page.getTotal(), (int) page.getPages(), (int) page.getCurrent(), (int) page.getSize());
+    }
+
+    @GetMapping(value = "/community/hot")
+    public ResponseEntity<GetCommunityHotDTO> getCommunityHot() {
+        Page<Community> communityPage = new Page<>(1, 3);
+        LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>()
+                .orderByDesc(Community::getHot);
+
+        Page<Community> result = communityService.page(communityPage, communityQuery);
+
+        List<GetCommunityHotDTO.Community> communityList = getCommunityList(result);
+
+        GetCommunityHotDTO getCommunityHotDTO = new GetCommunityHotDTO(communityList);
+
+        return ResponseEntity.ok(getCommunityHotDTO);
+    }
+
+    @NotNull
+    private static List<GetCommunityHotDTO.Community> getCommunityList(Page<Community> communityPage) {
+        List<Community> communityListT = communityPage.getRecords();
+        List<GetCommunityHotDTO.Community> communityList = new ArrayList<>();
+        int status = 1;
+        for (Community community : communityListT) {
+            GetCommunityHotDTO.Community communityHot = new GetCommunityHotDTO.Community(
+                    community.getCommunityId(),
+                    community.getCommunityName(),
+                    community.getCommunityPicture(),
+                    community.getCommunityPicture(),
+                    community.getHot(),
+                    community.getType(),
+                    community.getThreadNumber(),
+                    community.getResourceId(),
+                    status);
+            communityList.add(communityHot);
+            status++;
+        }
+
+        return communityList;
+    }
+
+    @GetMapping(value = "/community/common")
+    public ResponseEntity<GetCommunityCommonDTO> getCommunityCommon() {
+        Page<Community> communityPage = new Page<>(2, 3);
+        LambdaQueryWrapper<Community> communityQuery = new LambdaQueryWrapper<Community>()
+                .orderByDesc(Community::getHot);
+
+        Page<Community> result = communityService.page(communityPage, communityQuery);
+
+        List<Community> communityList = result.getRecords();
+
+        GetCommunityCommonDTO getCommunityCommonDTO = new GetCommunityCommonDTO(communityList);
+
+        return ResponseEntity.ok(getCommunityCommonDTO);
+    }
+
 }
diff --git a/src/main/java/com/g9/g9backend/controller/NotificationController.java b/src/main/java/com/g9/g9backend/controller/NotificationController.java
index 55a30f3..4f451df 100644
--- a/src/main/java/com/g9/g9backend/controller/NotificationController.java
+++ b/src/main/java/com/g9/g9backend/controller/NotificationController.java
@@ -51,12 +51,12 @@
     public ResponseEntity<IPage<Notification>> getNotification(@RequestParam Integer userId,
                                                                @RequestParam Integer pageNumber,
                                                                @RequestParam Integer rows) {
-        Page<Notification> pageNotification = new Page<>(pageNumber, rows);
-        LambdaQueryWrapper<Notification> query = new LambdaQueryWrapper<Notification>()
+        Page<Notification> notificationPage = new Page<>(pageNumber, rows);
+        LambdaQueryWrapper<Notification> notificationQuery = new LambdaQueryWrapper<Notification>()
                 .eq(Notification::getUserId, userId)
                 .orderByDesc(Notification::getCreateAt);
 
-        IPage<Notification> result = notificationService.page(pageNotification, query);
+        IPage<Notification> result = notificationService.page(notificationPage, notificationQuery);
         logger.info("通知已返回");
 
         return ResponseEntity.ok(result);
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityCommonDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityCommonDTO.java
new file mode 100644
index 0000000..8e24f40
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityCommonDTO.java
@@ -0,0 +1,17 @@
+package com.g9.g9backend.pojo.DTO;
+
+import com.g9.g9backend.pojo.Community;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GetCommunityCommonDTO {
+
+    List<Community> communityList;
+
+}
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityDTO.java
new file mode 100644
index 0000000..791bb10
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityDTO.java
@@ -0,0 +1,45 @@
+package com.g9.g9backend.pojo.DTO;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GetCommunityDTO {
+
+    private List<Community> records;
+
+    private Integer total;
+
+    private Integer pages;
+
+    private Integer current;
+
+    private Integer size;
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class Community {
+
+        private int communityId;
+
+        private String communityName;
+
+        private String communityPicture;
+
+        private String description;
+
+        private float hot;
+
+        private int threadNumber;
+
+        private int resourceId;
+
+    }
+
+}
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityHotDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityHotDTO.java
new file mode 100644
index 0000000..222eb07
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityHotDTO.java
@@ -0,0 +1,42 @@
+package com.g9.g9backend.pojo.DTO;
+
+import com.g9.g9backend.pojo.Community;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GetCommunityHotDTO {
+
+    List<Community> communityList;
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class Community{
+
+        private int communityId;
+
+        private String communityName;
+
+        private String communityPicture;
+
+        private String description;
+
+        private float hot;
+
+        private String type;
+
+        private int threadNumber;
+
+        private int resourceId;
+
+        private int status;
+
+    }
+
+}
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityThreadsDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityThreadsDTO.java
new file mode 100644
index 0000000..d734433
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/GetCommunityThreadsDTO.java
@@ -0,0 +1,44 @@
+package com.g9.g9backend.pojo.DTO;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+import java.util.List;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GetCommunityThreadsDTO {
+
+    private List<Thread> records;
+
+    private Integer total;
+
+    private Integer pages;
+
+    private Integer current;
+
+    private Integer size;
+
+    @Data
+    @AllArgsConstructor
+    @NoArgsConstructor
+    public static class Thread{
+
+        private int threadId;
+
+        private int userId;
+
+        private String threadPicture;
+
+        private String title;
+
+        private int likes;
+
+        private Date createAt;
+
+    }
+
+}
diff --git a/src/main/java/com/g9/g9backend/pojo/DTO/ThreadDTO.java b/src/main/java/com/g9/g9backend/pojo/DTO/ThreadDTO.java
new file mode 100644
index 0000000..5e0aec7
--- /dev/null
+++ b/src/main/java/com/g9/g9backend/pojo/DTO/ThreadDTO.java
@@ -0,0 +1,36 @@
+package com.g9.g9backend.pojo.DTO;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class ThreadDTO {
+
+    private int threadId;
+
+    private int userId;
+
+    private String threadPicture;
+
+    private String title;
+
+    private String content;
+
+    private int likes;
+
+    @JsonProperty("isLike")
+    private boolean isLike;
+
+    private Date createAt;
+
+    private int commentNumber;
+
+    private int communityId;
+
+}