Skip to content

Commit f87cdb2

Browse files
committed
refactor: move promote point logic from controller to service layer
- Extract note creation and update logic to NoteConstructionService - Add getParentNoteId method to PromotionType enum for cleaner code - Add vitest-test-results to gitignore
1 parent 26b2a3b commit f87cdb2

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ doughnut_mobile/android/app/local.properties
140140
# AI assistants
141141
.claude/
142142
.serena/
143+
frontend/vitest-test-results/

backend/src/main/java/com/odde/doughnut/controllers/AiController.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.odde.doughnut.controllers.dto.IgnorePointsRequestDTO;
77
import com.odde.doughnut.controllers.dto.IgnorePointsResponseDTO;
88
import com.odde.doughnut.entities.Note;
9-
import com.odde.doughnut.entities.User;
109
import com.odde.doughnut.exceptions.UnexpectedNoAccessRightException;
1110
import com.odde.doughnut.factoryServices.EntityPersister;
1211
import com.odde.doughnut.services.AuthorizationService;
@@ -156,36 +155,15 @@ public PromotePointResponseDTO promotePoint(
156155

157156
PromotePointRequestDTO.PromotionType promotionType = request.getPromotionType();
158157

159-
// 1. Call AI to generate result
160-
PointExtractionResult result =
158+
PointExtractionResult aiResult =
161159
notebookAssistantForNoteServiceFactory
162160
.createNoteAutomationService(note)
163161
.promotePoint(request.getPoint(), promotionType);
164162

165-
if (result == null) {
163+
if (aiResult == null) {
166164
throw new RuntimeException("AI failed to generate extraction result");
167165
}
168166

169-
// 2. Determine parent based on promotion type
170-
Integer parentNoteId =
171-
promotionType == PromotePointRequestDTO.PromotionType.SIBLING
172-
? note.getParent().getId()
173-
: note.getId();
174-
175-
// 3. Create new note under the determined parent
176-
User user = authorizationService.getCurrentUser();
177-
Note newNote =
178-
noteConstructionService.createNoteUnderParentId(parentNoteId, result.newNoteTitle);
179-
newNote.setDetails(result.newNoteDetails);
180-
newNote.setUpdatedAt(testabilitySettings.getCurrentUTCTimestamp());
181-
entityPersister.save(newNote);
182-
183-
// 4. Update original note's details
184-
note.setUpdatedAt(testabilitySettings.getCurrentUTCTimestamp());
185-
note.setDetails(result.updatedParentDetails);
186-
entityPersister.save(note);
187-
188-
// 5. Return result
189-
return new PromotePointResponseDTO(newNote.toNoteRealm(user), note.toNoteRealm(user));
167+
return noteConstructionService.createNoteFromPromotedPoint(note, promotionType, aiResult);
190168
}
191169
}

backend/src/main/java/com/odde/doughnut/controllers/dto/PromotePointRequestDTO.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.odde.doughnut.controllers.dto;
22

3+
import com.odde.doughnut.entities.Note;
34
import jakarta.validation.constraints.NotNull;
45
import lombok.Getter;
56
import lombok.Setter;
@@ -9,7 +10,11 @@
910
public class PromotePointRequestDTO {
1011
public enum PromotionType {
1112
CHILD,
12-
SIBLING
13+
SIBLING;
14+
15+
public Integer getParentNoteId(Note note) {
16+
return this == SIBLING ? note.getParent().getId() : note.getId();
17+
}
1318
}
1419

1520
private String point;

backend/src/main/java/com/odde/doughnut/services/NoteConstructionService.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.odde.doughnut.controllers.dto.NoteCreationDTO;
44
import com.odde.doughnut.controllers.dto.NoteCreationResult;
5+
import com.odde.doughnut.controllers.dto.PromotePointRequestDTO;
6+
import com.odde.doughnut.controllers.dto.PromotePointResponseDTO;
7+
import com.odde.doughnut.services.ai.PointExtractionResult;
58
import com.odde.doughnut.entities.Note;
69
import com.odde.doughnut.entities.RelationType;
710
import com.odde.doughnut.entities.User;
@@ -138,4 +141,24 @@ public Note createNoteAfter(
138141
entityPersister.save(note);
139142
return note;
140143
}
144+
145+
public PromotePointResponseDTO createNoteFromPromotedPoint(
146+
Note originalNote,
147+
PromotePointRequestDTO.PromotionType promotionType,
148+
PointExtractionResult aiResult)
149+
throws UnexpectedNoAccessRightException {
150+
User user = authorizationService.getCurrentUser();
151+
Timestamp currentUTCTimestamp = testabilitySettings.getCurrentUTCTimestamp();
152+
153+
Note newNote = createNoteUnderParentId(promotionType.getParentNoteId(originalNote), aiResult.newNoteTitle);
154+
newNote.setDetails(aiResult.newNoteDetails);
155+
newNote.setUpdatedAt(currentUTCTimestamp);
156+
entityPersister.save(newNote);
157+
158+
originalNote.setUpdatedAt(currentUTCTimestamp);
159+
originalNote.setDetails(aiResult.updatedParentDetails);
160+
entityPersister.save(originalNote);
161+
162+
return new PromotePointResponseDTO(newNote.toNoteRealm(user), originalNote.toNoteRealm(user));
163+
}
141164
}

0 commit comments

Comments
 (0)