Pārlūkot izejas kodu

fix: add notification index, thread pool executor, and markRead error handling

wangkangyjy 2 mēneši atpakaļ
vecāks
revīzija
4b46db1259

+ 0 - 2
backend/src/main/java/com/yimeng/okr/OkrApplication.java

@@ -3,13 +3,11 @@ package com.yimeng.okr;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 
 @SpringBootApplication
 @MapperScan("com.yimeng.okr.mapper")
 @EnableScheduling
-@EnableAsync
 public class OkrApplication {
 
     public static void main(String[] args) {

+ 24 - 0
backend/src/main/java/com/yimeng/okr/config/AsyncConfig.java

@@ -0,0 +1,24 @@
+package com.yimeng.okr.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+
+@Configuration
+@EnableAsync
+public class AsyncConfig {
+
+    @Bean
+    public Executor taskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(2);
+        executor.setMaxPoolSize(8);
+        executor.setQueueCapacity(100);
+        executor.setThreadNamePrefix("async-");
+        executor.initialize();
+        return executor;
+    }
+}

+ 10 - 4
backend/src/main/java/com/yimeng/okr/service/NotificationService.java

@@ -10,6 +10,8 @@ import com.yimeng.okr.mapper.NotificationMapper;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
+import com.yimeng.okr.exception.BusinessException;
+
 import java.time.LocalDateTime;
 import java.time.temporal.ChronoUnit;
 
@@ -39,11 +41,15 @@ public class NotificationService {
 
     public void markRead(Long id, Long userId) {
         Notification n = notificationMapper.selectById(id);
-        if (n != null && n.getUserId().equals(userId)) {
-            n.setIsRead(1);
-            n.setReadAt(LocalDateTime.now());
-            notificationMapper.updateById(n);
+        if (n == null) {
+            throw new BusinessException(404, "通知不存在");
+        }
+        if (!n.getUserId().equals(userId)) {
+            throw new BusinessException(403, "无权操作此通知");
         }
+        n.setIsRead(1);
+        n.setReadAt(LocalDateTime.now());
+        notificationMapper.updateById(n);
     }
 
     public void markAllRead(Long userId) {

+ 2 - 0
backend/src/main/resources/schema.sql

@@ -180,3 +180,5 @@ CREATE TABLE IF NOT EXISTS notification (
     read_at TEXT,
     created_at TEXT NOT NULL
 );
+
+CREATE INDEX IF NOT EXISTS idx_notification_user ON notification(user_id, is_read, created_at);