Skip to content
Open
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 @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/message")
Expand All @@ -29,4 +30,17 @@ public ResponseEntity getConversations(@RequestParam Long projectId , @RequestPa
List<Message> message = messageService.getConversationById(projectId , userId);
return ResponseEntity.ok(message);
}
/**
* Deletes all messages before the specified timestamp.
*
* @param dateISO cutoff date in ISO-8601 format (e.g., '2023-01-01T00:00:00')
* @return number of messages deleted
*/
@DeleteMapping("/delete")
public ResponseEntity<?> deleteMessagesBefore(@RequestParam("date") String dateISO) {
LocalDateTime threshold = LocalDateTime.parse(dateISO);
long deletedCount = messageService.deleteMessagesBefore(threshold);
return ResponseEntity.ok(deletedCount);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@


import java.util.List;
import java.time.LocalDateTime;

public interface MessageRepo extends MongoRepository<Message, Long> {
List<Message> findBySenderIdAndReceiverId(Long senderId, Long receiverId);
List<Message> findByReceiverIdAndSenderId(Long receiverId, Long senderId);
List<Message> findByConversationIdOrderByTimestampAsc(String id);
}
/**
* Deletes all messages with timestamp before the given threshold.
*
* @param timestamp cutoff timestamp; messages before this will be deleted
* @return the number of messages deleted
*/
long deleteByTimestampBefore(LocalDateTime timestamp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,14 @@ public List<Message> getConversationById(Long projectId, Long userId) {

return messageRepo.findByConversationIdOrderByTimestampAsc(conversation.get().getId());
}
/**
* Deletes all messages before the given timestamp.
*
* @param timestamp messages older than this will be deleted
* @return count of deleted messages
*/
public long deleteMessagesBefore(LocalDateTime timestamp) {
return messageRepo.deleteByTimestampBefore(timestamp);
}

}