xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 5 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 6 | import com.g9.g9backend.pojo.Notification; |
| 7 | import com.g9.g9backend.service.NotificationService; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 10 | import org.springframework.http.HttpStatus; |
| 11 | import org.springframework.http.ResponseEntity; |
| 12 | import org.springframework.web.bind.annotation.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * NotificationController 通知控制器类,处理与通知相关的请求 |
| 16 | * |
| 17 | * @author Seamher |
| 18 | */ |
| 19 | @RestController |
| 20 | @RequestMapping("/notification") |
| 21 | public class NotificationController { |
| 22 | |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 23 | private final NotificationService notificationService; |
| 24 | |
| 25 | public NotificationController(NotificationService notificationService) { |
| 26 | this.notificationService = notificationService; |
| 27 | } |
| 28 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 29 | private final Logger logger = LoggerFactory.getLogger(NotificationController.class); |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 30 | |
| 31 | @PostMapping(value = "/read") |
| 32 | public ResponseEntity<String> getNotificationRead(@RequestBody Notification notification) { |
| 33 | Integer notificationId = notification.getNotificationId(); |
| 34 | System.out.println(notificationId); |
| 35 | Notification notificationUpdate = notificationService.getById(notificationId); |
| 36 | notificationUpdate.setRead(true); |
| 37 | notificationService.updateById(notificationUpdate); |
| 38 | |
| 39 | return ResponseEntity.ok(""); |
| 40 | } |
| 41 | |
| 42 | @DeleteMapping |
| 43 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 44 | public ResponseEntity<String> deleteNotification(@RequestParam Integer notificationId) { |
| 45 | notificationService.removeById(notificationId); |
| 46 | |
| 47 | return ResponseEntity.noContent().build(); |
| 48 | } |
| 49 | |
| 50 | @GetMapping |
| 51 | public ResponseEntity<IPage<Notification>> getNotification(@RequestParam Integer userId, |
| 52 | @RequestParam Integer pageNumber, |
| 53 | @RequestParam Integer rows) { |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame] | 54 | Page<Notification> notificationPage = new Page<>(pageNumber, rows); |
| 55 | LambdaQueryWrapper<Notification> notificationQuery = new LambdaQueryWrapper<Notification>() |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 56 | .eq(Notification::getUserId, userId) |
| 57 | .orderByDesc(Notification::getCreateAt); |
| 58 | |
Seamher | 97b1156 | 2025-06-07 13:34:31 +0800 | [diff] [blame] | 59 | IPage<Notification> result = notificationService.page(notificationPage, notificationQuery); |
Seamher | 176d331 | 2025-06-06 16:57:34 +0800 | [diff] [blame] | 60 | logger.info("通知已返回"); |
| 61 | |
| 62 | return ResponseEntity.ok(result); |
| 63 | } |
| 64 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 65 | } |