Skip to content

Commit cfe22a3

Browse files
committed
Added deleting related records when deleting a comic book [#2222]
1 parent 9e41d3f commit cfe22a3

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

comixed-model/src/main/java/org/comixedproject/model/comicbooks/ComicDetail.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public class ComicDetail {
243243
mappedBy = "comicDetail",
244244
cascade = CascadeType.ALL,
245245
orphanRemoval = true,
246-
fetch = FetchType.LAZY)
246+
fetch = FetchType.EAGER)
247247
@JsonProperty("tags")
248248
@JsonView({View.ComicListView.class, View.ReadingListDetail.class})
249249
@Getter
@@ -302,6 +302,13 @@ public class ComicDetail {
302302
@Setter
303303
private Date addedDate = new Date();
304304

305+
@ElementCollection
306+
@CollectionTable(name = "read_comic_books", joinColumns = @JoinColumn(name = "comic_detail_id"))
307+
@Column(name = "comixed_user_id")
308+
@JsonView(View.UserList.class)
309+
@Getter
310+
private Set<Long> readByUserIds = new HashSet<>();
311+
305312
/**
306313
* Returns the id for the parent comic book.
307314
*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* ComiXed - A digital comic book library management application.
3+
* Copyright (C) 2025, The ComiXed Project.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses>
17+
*/
18+
19+
package org.comixedproject.repositories.comicbooks;
20+
21+
import org.comixedproject.model.comicbooks.ComicDetail;
22+
import org.comixedproject.model.comicbooks.ComicTag;
23+
import org.springframework.data.jpa.repository.JpaRepository;
24+
import org.springframework.stereotype.Repository;
25+
26+
@Repository
27+
public interface ComicTagRepository extends JpaRepository<ComicTag, Long> {
28+
/**
29+
* Deletes all tags for the given comic detail.
30+
*
31+
* @param comicDetail the comic detail
32+
*/
33+
void deleteAllByComicDetail(ComicDetail comicDetail);
34+
}

comixed-services/src/main/java/org/comixedproject/service/comicbooks/ComicBookService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.comixedproject.model.net.library.RemoteLibrarySegmentState;
4545
import org.comixedproject.repositories.comicbooks.ComicBookRepository;
4646
import org.comixedproject.repositories.comicbooks.ComicDetailRepository;
47+
import org.comixedproject.repositories.comicbooks.ComicTagRepository;
4748
import org.comixedproject.state.comicbooks.ComicEvent;
4849
import org.comixedproject.state.comicbooks.ComicStateHandler;
4950
import org.springframework.beans.factory.annotation.Autowired;
@@ -79,6 +80,7 @@ public class ComicBookService {
7980
@Autowired private ImprintService imprintService;
8081
@Autowired private FileTypeAdaptor fileTypeAdaptor;
8182
@Autowired private ApplicationEventPublisher applicationEventPublisher;
83+
@Autowired private ComicTagRepository comicTagRepository;
8284

8385
/**
8486
* Retrieves a single comic by id. It is expected that this comic exists.
@@ -240,6 +242,9 @@ public ComicBook undeleteComicBook(final long id) throws ComicBookException {
240242
*/
241243
@Transactional
242244
public void deleteComicBook(final ComicBook comicBook) {
245+
log.trace("Removing read references");
246+
comicBook.getComicDetail().getReadByUserIds().clear();
247+
this.comicTagRepository.deleteAllByComicDetail(comicBook.getComicDetail());
243248
log.debug("Deleting comicBook: id={}", comicBook.getId());
244249
this.comicBookRepository.delete(comicBook);
245250
}

comixed-services/src/test/java/org/comixedproject/service/comicbooks/ComicBookServiceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.comixedproject.model.net.library.PublisherAndYearSegment;
4343
import org.comixedproject.model.net.library.RemoteLibrarySegmentState;
4444
import org.comixedproject.repositories.comicbooks.ComicBookRepository;
45+
import org.comixedproject.repositories.comicbooks.ComicTagRepository;
4546
import org.comixedproject.state.comicbooks.ComicEvent;
4647
import org.comixedproject.state.comicbooks.ComicStateHandler;
4748
import org.junit.Before;
@@ -109,6 +110,7 @@ public class ComicBookServiceTest {
109110
@InjectMocks private ComicBookService service;
110111
@Mock private ComicStateHandler comicStateHandler;
111112
@Mock private ComicBookRepository comicBookRepository;
113+
@Mock private ComicTagRepository comicTagRepository;
112114
@Mock private ComicBookMetadataAdaptor comicBookMetadataAdaptor;
113115
@Mock private ComicFileAdaptor comicFileAdaptor;
114116
@Mock private ApplicationEventPublisher applicationEventPublisher;
@@ -137,6 +139,7 @@ public class ComicBookServiceTest {
137139

138140
@Before
139141
public void setUp() {
142+
Mockito.when(comicBook.getComicDetail()).thenReturn(comicDetail);
140143
Mockito.when(comicBook.getId()).thenReturn(TEST_COMIC_BOOK_ID);
141144
Mockito.when(comicBook.getComicDetail()).thenReturn(comicDetail);
142145
Mockito.when(incomingComicBook.getComicDetail()).thenReturn(incomingComicDetail);
@@ -397,10 +400,14 @@ public void testSave() {
397400

398401
@Test
399402
public void testDelete() {
403+
Mockito.doNothing()
404+
.when(comicTagRepository)
405+
.deleteAllByComicDetail(Mockito.any(ComicDetail.class));
400406
Mockito.doNothing().when(comicBookRepository).delete(Mockito.any(ComicBook.class));
401407

402408
service.deleteComicBook(comicBook);
403409

410+
Mockito.verify(comicTagRepository, Mockito.times(1)).deleteAllByComicDetail(comicDetail);
404411
Mockito.verify(comicBookRepository, Mockito.times(1)).delete(comicBook);
405412
}
406413

0 commit comments

Comments
 (0)