Deleting initial post removes entire thread

This commit is contained in:
Andreas Svanberg 2025-03-17 10:50:39 +01:00
parent 075c691ba3
commit e4711971cc
3 changed files with 27 additions and 2 deletions
core/src
main/java/se/su/dsv/scipro/forum
test/java/se/su/dsv/scipro/forum

@ -22,6 +22,8 @@ public interface BasicForumService extends Serializable {
ForumThread createThread(String subject);
ForumThread findThreadById(Long id);
long countUnreadThreads(List<ForumThread> forumThreadList, User user);
ForumPost getLastPost(ForumThread forumThread);

@ -91,6 +91,11 @@ public class BasicForumServiceImpl implements BasicForumService {
return threadRepository.save(forumThread);
}
@Override
public ForumThread findThreadById(Long id) {
return threadRepository.findOne(id);
}
@Override
public long countUnreadThreads(List<ForumThread> forumThreadList, User user) {
return postRepository.countUnreadThreads(forumThreadList, user);
@ -184,8 +189,12 @@ public class BasicForumServiceImpl implements BasicForumService {
if (!canDelete(post)) {
throw new PostCantBeDeletedException();
}
post.setDeleted(true);
postRepository.save(post);
if (isInitialPost(post)) {
threadRepository.delete(post.getForumThread());
} else {
post.setDeleted(true);
postRepository.save(post);
}
}
private static final class PostCantBeDeletedException extends IllegalArgumentException {

@ -6,6 +6,7 @@ import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -195,4 +196,17 @@ public class BasicForumServiceIntegrationTest extends IntegrationTest {
assertThat(getPublishedEvents(), not(hasItem(new ForumPostReadEvent(post, commenter))));
}
@Test
public void deleting_initial_post_removes_entire_thread() {
ForumThread thread = basicForumService.createThread("Test thread");
ForumPost post = basicForumService.createReply(thread, op, "Test post 1", Set.of());
setLoggedInAs(op);
basicForumService.deletePost(post);
ForumThread threadById = basicForumService.findThreadById(thread.getId());
assertNull(threadById, "Thread was not deleted");
}
}