Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -20,8 +21,6 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/me")
@RequiredArgsConstructor
Expand Down Expand Up @@ -120,7 +119,17 @@ public RsData<NotificationGoResponseDto> goPostLink(
@PathVariable("id") Long notificationId
) {
Long userId = principal.getId();
var body = notificationService.markAsReadAndGetPostLink(userId, notificationId);
NotificationGoResponseDto body = notificationService.markAsReadAndGetPostLink(userId, notificationId);
return RsData.successOf(body);
}

@DeleteMapping("/notifications")
@Operation(summary = "Delete all notifications", description = "Remove every notification belonging to the authenticated user")
public RsData<Void> deleteNotifications(
@AuthenticationPrincipal SecurityUser principal
) {
Long userId = principal.getId();
notificationService.deleteAll(userId);
return RsData.of(200, "cleared");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.back.domain.notification.entity.Notification;
import org.springframework.data.domain.Pageable;
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;
Expand Down Expand Up @@ -37,4 +38,8 @@ List<Notification> findMyNotificationsAfter(@Param("userId") Long userId,
where n.id = :id and n.user.id = :userId
""")
Notification findByIdAndUserId(@Param("id") Long id, @Param("userId") Long userId);

@Modifying(clearAutomatically = true, flushAutomatically = true)
long deleteByUser_Id(Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ public void sendNotification(User user, Post post, NotificationType type, String
}
}
}

@Transactional
public void deleteAll(Long userId) {
notificationRepository.deleteByUser_Id(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down Expand Up @@ -249,4 +251,21 @@ void goPostLink() throws Exception {

verify(notificationService).markAsReadAndGetPostLink(principal.getId(), 999L);
}
@Test
@DisplayName("Delete notifications")
void deleteNotifications() throws Exception {
SecurityUser principal = createPrincipal(17L);

willDoNothing().given(notificationService).deleteAll(principal.getId());

mockMvc.perform(delete("/me/notifications")
.with(withPrincipal(principal))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("cleared"))
.andExpect(jsonPath("$.data").doesNotExist());

verify(notificationService).deleteAll(principal.getId());
}
}