11package ai .elimu .web .content .storybook .chapter ;
22
3- import ai .elimu .dao .ImageContributionEventDao ;
4- import ai .elimu .dao .ImageDao ;
53import ai .elimu .dao .StoryBookChapterDao ;
64import ai .elimu .dao .StoryBookContributionEventDao ;
75import ai .elimu .dao .StoryBookDao ;
86import ai .elimu .dao .StoryBookParagraphDao ;
97import ai .elimu .entity .content .StoryBook ;
108import ai .elimu .entity .content .StoryBookChapter ;
119import ai .elimu .entity .content .StoryBookParagraph ;
12- import ai .elimu .entity .content .multimedia .Image ;
1310import ai .elimu .entity .contributor .Contributor ;
14- import ai .elimu .entity .contributor .ImageContributionEvent ;
1511import ai .elimu .entity .contributor .StoryBookContributionEvent ;
1612import ai .elimu .entity .enums .PeerReviewStatus ;
1713import ai .elimu .entity .enums .Role ;
@@ -40,9 +36,6 @@ public class StoryBookChapterDeleteController {
4036 private final StoryBookParagraphDao storyBookParagraphDao ;
4137 private final StoryBookContributionEventDao storyBookContributionEventDao ;
4238
43- private final ImageDao imageDao ;
44- private final ImageContributionEventDao imageContributionEventDao ;
45-
4639 private final StoryBooksJsonService storyBooksJsonService ;
4740
4841 @ GetMapping
@@ -72,41 +65,35 @@ public String handleRequest(HttpSession session, @PathVariable Long storyBookId,
7265 log .info ("Deleting StoryBookChapter with ID " + storyBookChapterToBeDeleted .getId ());
7366 storyBookChapterDao .delete (storyBookChapterToBeDeleted );
7467
75- // Delete the chapter's image (if any)
76- Image chapterImage = storyBookChapterToBeDeleted .getImage ();
77- log .info ("chapterImage: " + chapterImage );
78- if (chapterImage != null ) {
79- // Remove content labels
80- chapterImage .setLiteracySkills (null );
81- chapterImage .setNumeracySkills (null );
82- chapterImage .setLetters (null );
83- chapterImage .setNumbers (null );
84- chapterImage .setWords (null );
85- imageDao .update (chapterImage );
86-
87- // Remove contribution events
88- for (ImageContributionEvent imageContributionEvent : imageContributionEventDao .readAll (chapterImage )) {
89- log .warn ("Deleting ImageContributionEvent from the database" );
90- imageContributionEventDao .delete (imageContributionEvent );
91- }
92-
93- log .warn ("Deleting the chapter image from the database" );
94- imageDao .delete (chapterImage );
95- }
96-
9768 // Update the StoryBook's metadata
9869 StoryBook storyBook = storyBookChapterToBeDeleted .getStoryBook ();
9970 storyBook .setRevisionNumber (storyBook .getRevisionNumber () + 1 );
10071 storyBook .setPeerReviewStatus (PeerReviewStatus .PENDING );
10172 storyBookDao .update (storyBook );
10273
74+ // Update the sorting order of the remaining chapters
75+ List <StoryBookChapter > storyBookChapters = storyBookChapterDao .readAll (storyBook );
76+ log .info ("storyBookChapters.size(): " + storyBookChapters .size ());
77+ for (StoryBookChapter storyBookChapter : storyBookChapters ) {
78+ log .info ("storyBookChapter.getId(): " + storyBookChapter .getId () + ", storyBookChapter.getSortOrder(): " + storyBookChapter .getSortOrder ());
79+ if (storyBookChapter .getSortOrder () > storyBookChapterToBeDeleted .getSortOrder ()) {
80+ // Reduce sort order by 1
81+ storyBookChapter .setSortOrder (storyBookChapter .getSortOrder () - 1 );
82+ storyBookChapterDao .update (storyBookChapter );
83+ log .info ("storyBookChapter.getSortOrder() (after update): " + storyBookChapter .getSortOrder ());
84+ }
85+ }
86+
87+ // Refresh the REST API cache
88+ storyBooksJsonService .refreshStoryBooksJSONArray ();
89+
10390 // Store contribution event
10491 StoryBookContributionEvent storyBookContributionEvent = new StoryBookContributionEvent ();
10592 storyBookContributionEvent .setContributor (contributor );
10693 storyBookContributionEvent .setTimestamp (Calendar .getInstance ());
10794 storyBookContributionEvent .setStoryBook (storyBook );
10895 storyBookContributionEvent .setRevisionNumber (storyBook .getRevisionNumber ());
109- storyBookContributionEvent .setComment ("Deleted storybook chapter " + (storyBookChapterToBeDeleted .getSortOrder () + 1 ) + " (🤖 auto-generated comment)" );
96+ storyBookContributionEvent .setComment ("Deleted storybook chapter " + (storyBookChapterToBeDeleted .getSortOrder () + 1 ) + "/" + storyBookChapters . size () + " (🤖 auto-generated comment)" );
11097 storyBookContributionEventDao .create (storyBookContributionEvent );
11198
11299 String contentUrl = DomainHelper .getBaseUrl () + "/content/storybook/edit/" + storyBook .getId ();
@@ -123,22 +110,6 @@ public String handleRequest(HttpSession session, @PathVariable Long storyBookId,
123110 embedThumbnailUrl
124111 );
125112
126- // Update the sorting order of the remaining chapters
127- List <StoryBookChapter > storyBookChapters = storyBookChapterDao .readAll (storyBook );
128- log .info ("storyBookChapters.size(): " + storyBookChapters .size ());
129- for (StoryBookChapter storyBookChapter : storyBookChapters ) {
130- log .info ("storyBookChapter.getId(): " + storyBookChapter .getId () + ", storyBookChapter.getSortOrder(): " + storyBookChapter .getSortOrder ());
131- if (storyBookChapter .getSortOrder () > storyBookChapterToBeDeleted .getSortOrder ()) {
132- // Reduce sort order by 1
133- storyBookChapter .setSortOrder (storyBookChapter .getSortOrder () - 1 );
134- storyBookChapterDao .update (storyBookChapter );
135- log .info ("storyBookChapter.getSortOrder() (after update): " + storyBookChapter .getSortOrder ());
136- }
137- }
138-
139- // Refresh the REST API cache
140- storyBooksJsonService .refreshStoryBooksJSONArray ();
141-
142113 return "redirect:/content/storybook/edit/" + storyBookId ;
143114 }
144115}
0 commit comments