CoolFace
Apppublic

sakthivikram/geo-location

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
NotificationController.java84 linesDownload Raw Back to controller
1package com.georeport.controller;2 3import com.georeport.dto.ApiResponse;4import com.georeport.dto.NotificationResponse;5import com.georeport.entity.User;6import com.georeport.service.AuthService;7import com.georeport.service.NotificationService;8import org.springframework.beans.factory.annotation.Autowired;9import org.springframework.http.ResponseEntity;10import org.springframework.web.bind.annotation.*;11 12import java.util.List;13import java.util.Map;14 15/**16 * REST Controller for notification operations.17 * Handles user notification retrieval and management.18 */19@RestController20@RequestMapping("/api/notifications")21@CrossOrigin(origins = "*")22public class NotificationController {23 24    @Autowired25    private NotificationService notificationService;26 27    @Autowired28    private AuthService authService;29 30    /**31     * Get all notifications for current user32     * GET /api/notifications33     */34    @GetMapping35    public ResponseEntity<ApiResponse<List<NotificationResponse>>> getNotifications() {36        User user = authService.getCurrentUser();37        List<NotificationResponse> notifications = notificationService.getUserNotifications(user.getId());38        return ResponseEntity.ok(ApiResponse.success(notifications));39    }40 41    /**42     * Get unread notifications43     * GET /api/notifications/unread44     */45    @GetMapping("/unread")46    public ResponseEntity<ApiResponse<List<NotificationResponse>>> getUnreadNotifications() {47        User user = authService.getCurrentUser();48        List<NotificationResponse> notifications = notificationService.getUnreadNotifications(user.getId());49        return ResponseEntity.ok(ApiResponse.success(notifications));50    }51 52    /**53     * Get unread notification count54     * GET /api/notifications/count55     */56    @GetMapping("/count")57    public ResponseEntity<ApiResponse<Map<String, Long>>> getUnreadCount() {58        User user = authService.getCurrentUser();59        long count = notificationService.getUnreadCount(user.getId());60        return ResponseEntity.ok(ApiResponse.success(Map.of("unreadCount", count)));61    }62 63    /**64     * Mark notification as read65     * PUT /api/notifications/{id}/read66     */67    @PutMapping("/{id}/read")68    public ResponseEntity<ApiResponse<String>> markAsRead(@PathVariable Long id) {69        notificationService.markAsRead(id);70        return ResponseEntity.ok(ApiResponse.success("Notification marked as read"));71    }72 73    /**74     * Mark all notifications as read75     * PUT /api/notifications/read-all76     */77    @PutMapping("/read-all")78    public ResponseEntity<ApiResponse<String>> markAllAsRead() {79        User user = authService.getCurrentUser();80        notificationService.markAllAsRead(user.getId());81        return ResponseEntity.ok(ApiResponse.success("All notifications marked as read"));82    }83}84