Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
haiphucnguyen committed Nov 22, 2024
1 parent ece1c3d commit 4c7cfd0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import io.flexwork.modules.collab.domain.Notification;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {

List<Notification> findByUserIdAndIsReadFalse(Long userId);

@Modifying
@Query("UPDATE Notification n SET n.isRead = true WHERE n.id IN :ids")
void markAsRead(@Param("ids") List<Long> ids);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.flexwork.modules.collab.service;

import io.flexwork.modules.collab.domain.Notification;
import io.flexwork.modules.collab.repository.NotificationRepository;
import io.flexwork.modules.collab.service.dto.NotificationDTO;
import io.flexwork.modules.collab.service.mapper.NotificationMapper;
Expand Down Expand Up @@ -28,12 +27,8 @@ public List<NotificationDTO> getUnreadNotificationsForUser(Long userId) {
.toList();
}

public void markNotificationAsRead(Long notificationId) {
Notification notification =
notificationRepository
.findById(notificationId)
.orElseThrow(() -> new IllegalArgumentException("Notification not found"));
notification.setRead(true);
notificationRepository.save(notification);
@Transactional
public void markNotificationsAsRead(List<Long> notificationIds) {
notificationRepository.markAsRead(notificationIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,44 @@
import io.flexwork.modules.collab.service.NotificationService;
import io.flexwork.modules.collab.service.dto.NotificationDTO;
import java.util.List;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/notifications")
@RequestMapping("/api/notifications")
public class NotificationController {
private final NotificationService notificationService;

public NotificationController(NotificationService notificationService) {
this.notificationService = notificationService;
}

@PostMapping("/{id}/read")
public ResponseEntity<Void> markAsRead(@PathVariable Long id) {
notificationService.markNotificationAsRead(id);
@PostMapping("/mark-read")
public ResponseEntity<Void> markNotificationsAsRead(@RequestBody MarkReadRequest request) {
if (request.getNotificationIds() == null || request.getNotificationIds().isEmpty()) {
return ResponseEntity.badRequest().build();
}

notificationService.markNotificationsAsRead(request.getNotificationIds());
return ResponseEntity.noContent().build();
}

@GetMapping("/unread")
public ResponseEntity<List<NotificationDTO>> getUnreadNotifications(@RequestParam Long userId) {
public ResponseEntity<List<NotificationDTO>> getUnreadNotifications(
@RequestParam("userId") Long userId) {
List<NotificationDTO> notifications =
notificationService.getUnreadNotificationsForUser(userId);
return ResponseEntity.ok(notifications);
}

@Data
public static class MarkReadRequest {
private List<Long> notificationIds;
}
}

0 comments on commit 4c7cfd0

Please sign in to comment.