diff --git a/src/main/java/com/example/aneukbeserver/controller/DiaryController.java b/src/main/java/com/example/aneukbeserver/controller/DiaryController.java index bbec826..c6361a7 100644 --- a/src/main/java/com/example/aneukbeserver/controller/DiaryController.java +++ b/src/main/java/com/example/aneukbeserver/controller/DiaryController.java @@ -5,12 +5,14 @@ import com.example.aneukbeserver.domain.chat.Chat; import com.example.aneukbeserver.domain.chatMessages.ChatMessageDTO; import com.example.aneukbeserver.domain.chatMessages.ChatMessages; +import com.example.aneukbeserver.domain.collection.Collection; import com.example.aneukbeserver.domain.diary.Diary; import com.example.aneukbeserver.domain.diary.DiaryAiResponseDTO; import com.example.aneukbeserver.domain.diary.DiaryDTO; import com.example.aneukbeserver.domain.diaryParagraph.*; import com.example.aneukbeserver.domain.emotion.Emotion; import com.example.aneukbeserver.domain.emotion.EmotionDTO; +import com.example.aneukbeserver.domain.emotion.SaveEmotionDTO; import com.example.aneukbeserver.domain.member.Member; import com.example.aneukbeserver.domain.selectedEmotion.SelectedEmotion; @@ -71,6 +73,9 @@ public class DiaryController { @Autowired private SelectedEmotionService selectedEmotionService; + @Autowired + private CollectionService collectionService; + @Value("${spring.ai.url}") private String aiUrl; @@ -180,8 +185,39 @@ public ResponseEntity getFinalDiary(@Parameter(hidden = true) return ResponseEntity.ok(addStatus(200, diaryDTO)); } + @Operation(summary = "감정 선택 후 저장", description = "각 문단별로 감정 선택을 후 감정들을 저장합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "성공"), + @ApiResponse(responseCode = "500", description = "서버 에러, 관리자에게 문의 바랍니다."), + @ApiResponse(responseCode = "400", description = "사용자가 존재하지 않습니다."), + @ApiResponse(responseCode = "401", description = "채팅이 존재하지 않습니다.") + + }) + @PostMapping("/emotion/save") + public ResponseEntity saveParagraphEmotion(@Parameter(hidden = true) @RequestHeader("Authorization") final String accessToken, @RequestBody SaveEmotionDTO request) { + String userEmail = jwtUtil.getEmail(accessToken.substring(7)); + Optional member = memberService.findByEmail(userEmail); + log.info(String.valueOf(request)); + Optional diaryParagraph = diaryParagraphService.findByParagraphId(request.getDiary_id(), request.getOrder_index()); + + if (member.isEmpty()) + return ResponseEntity.badRequest().body(addStatus(400, "사용자가 존재하지 않습니다.")); + if (diaryParagraph.isEmpty()) + return ResponseEntity.badRequest().body(addStatus(401, "Paragraph이 존재하지 않습니다.")); + + List emotion_list = request.getEmotions(); + + List emotionList = emotionService.getEmotionObjectsByNames(emotion_list); + + selectedEmotionService.saveSelectedEmotions(diaryParagraph.get(), emotionList); + collectionService.saveEmotionCollection(emotionList, member.get()); + + return ResponseEntity.ok().body(addStatus(200, "성공적으로 저장되었습니다.")); + } + + - @Operation(summary = "감정 선택 후 문장 바뀜", description = "각 문단별로 감정 선택을 통해 문단을 재구성하여 response 합니다.") + @Operation(summary = "감정 선택 후 문장 바뀜", description = "각 문단별로 감정 선택을 통해 문단을 재구성하여 response 합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "성공"), @ApiResponse(responseCode = "500", description = "서버 에러, 관리자에게 문의 바랍니다."), @@ -203,15 +239,11 @@ public ResponseEntity remakeParagraph(@Parameter(hidden = tru List emotion_list = request.getEmotions(); - List emotionList = emotionService.getEmotionObjectsByNames(emotion_list); - Map aiRequest = Map.of( "original_content", request.getOriginal_content(), "emotion_list", emotion_list ); - selectedEmotionService.saveSelectedEmotions(diaryParagraph.get(), emotionList); - try { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); diff --git a/src/main/java/com/example/aneukbeserver/controller/StatisticsController.java b/src/main/java/com/example/aneukbeserver/controller/StatisticsController.java index d95ec5f..7e4b0ba 100644 --- a/src/main/java/com/example/aneukbeserver/controller/StatisticsController.java +++ b/src/main/java/com/example/aneukbeserver/controller/StatisticsController.java @@ -2,9 +2,6 @@ import com.example.aneukbeserver.auth.dto.StatusResponseDto; import com.example.aneukbeserver.auth.jwt.JwtUtil; -import com.example.aneukbeserver.domain.diary.Diary; -import com.example.aneukbeserver.domain.diary.DiaryDTO; -import com.example.aneukbeserver.domain.emotion.CategoryEmotionCount; import com.example.aneukbeserver.domain.member.Member; import com.example.aneukbeserver.service.*; import io.swagger.v3.oas.annotations.Operation; @@ -42,14 +39,7 @@ public class StatisticsController { private EmotionService emotionService; @Autowired - private ChatService chatService; - - @Autowired - private ChatMessagesService chatMessagesService; - - @Autowired - private DiaryService diaryService; - + private CollectionService collectionService; @Operation(summary = "최근 30일 통계", description = "최근 30일의 감정들의 개수를 카테고리마다 보여줍니다") @ApiResponses(value = { @@ -72,4 +62,26 @@ public ResponseEntity get30DaysStatistics(@Parameter(hidden = return ResponseEntity.ok().body(addStatus(200, stats)); } + + @Operation(summary = "감정도감", description = "사용한 감정/전체 감정, 각 카테고리별 사용한 감정/전체 감정을 보여줍니다") + @ApiResponses(value = { + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "성공"), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "서버 에러, 관리자에게 문의 바랍니다."), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "사용자가 존재하지 않습니다."), + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "일기가 존재하지 않습니다.") + + }) + @GetMapping("/collection") + public ResponseEntity getEmotionCollection(@Parameter(hidden = true) @RequestHeader("Authorization") final String accessToken) { + String userEmail = jwtUtil.getEmail(accessToken.substring(7)); + Optional member = memberService.findByEmail(userEmail); + + if (member.isEmpty()) + return ResponseEntity.badRequest().body(addStatus(400, "사용자가 존재하지 않습니다.")); + + Map stats = collectionService.getEmotionCollection(member.get()); + + return ResponseEntity.ok().body(addStatus(200, stats)); + + } } diff --git a/src/main/java/com/example/aneukbeserver/domain/collection/Collection.java b/src/main/java/com/example/aneukbeserver/domain/collection/Collection.java new file mode 100644 index 0000000..c779e9a --- /dev/null +++ b/src/main/java/com/example/aneukbeserver/domain/collection/Collection.java @@ -0,0 +1,27 @@ +package com.example.aneukbeserver.domain.collection; + +import com.example.aneukbeserver.domain.emotion.Emotion; +import com.example.aneukbeserver.domain.member.Member; +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@Entity +public class Collection { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name="member_id", nullable = false) + private Member member; + + @ManyToOne + @JoinColumn(name="emotion_id", nullable = false) + private Emotion emotion; + + @Column + private Long usageCount = 0L; +} diff --git a/src/main/java/com/example/aneukbeserver/domain/collection/CollectionRepository.java b/src/main/java/com/example/aneukbeserver/domain/collection/CollectionRepository.java new file mode 100644 index 0000000..d052771 --- /dev/null +++ b/src/main/java/com/example/aneukbeserver/domain/collection/CollectionRepository.java @@ -0,0 +1,15 @@ +package com.example.aneukbeserver.domain.collection; + +import com.example.aneukbeserver.domain.emotion.Emotion; +import com.example.aneukbeserver.domain.member.Member; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface CollectionRepository extends JpaRepository { + + List findByMember(Member member); + + Optional findByMemberAndEmotion(Member member, Emotion emotion); +} diff --git a/src/main/java/com/example/aneukbeserver/domain/emotion/CategoryEmotionCount.java b/src/main/java/com/example/aneukbeserver/domain/emotion/CategoryEmotionCount.java deleted file mode 100644 index 5fac8eb..0000000 --- a/src/main/java/com/example/aneukbeserver/domain/emotion/CategoryEmotionCount.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.aneukbeserver.domain.emotion; - -public class CategoryEmotionCount { - -} diff --git a/src/main/java/com/example/aneukbeserver/domain/emotion/EmotionRepository.java b/src/main/java/com/example/aneukbeserver/domain/emotion/EmotionRepository.java index 9fe3103..f73e0b7 100644 --- a/src/main/java/com/example/aneukbeserver/domain/emotion/EmotionRepository.java +++ b/src/main/java/com/example/aneukbeserver/domain/emotion/EmotionRepository.java @@ -7,4 +7,6 @@ public interface EmotionRepository extends JpaRepository { Optional findByTitle(String title); + + long countByCategory(String category); } diff --git a/src/main/java/com/example/aneukbeserver/domain/emotion/SaveEmotionDTO.java b/src/main/java/com/example/aneukbeserver/domain/emotion/SaveEmotionDTO.java new file mode 100644 index 0000000..e0b0361 --- /dev/null +++ b/src/main/java/com/example/aneukbeserver/domain/emotion/SaveEmotionDTO.java @@ -0,0 +1,16 @@ +package com.example.aneukbeserver.domain.emotion; + +import lombok.Data; +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@Data +public class SaveEmotionDTO { + private Long diary_id; + private Integer order_index; + private List emotions; +} diff --git a/src/main/java/com/example/aneukbeserver/service/CollectionService.java b/src/main/java/com/example/aneukbeserver/service/CollectionService.java new file mode 100644 index 0000000..d0f78e0 --- /dev/null +++ b/src/main/java/com/example/aneukbeserver/service/CollectionService.java @@ -0,0 +1,90 @@ +package com.example.aneukbeserver.service; + +import com.example.aneukbeserver.domain.collection.Collection; +import com.example.aneukbeserver.domain.collection.CollectionRepository; +import com.example.aneukbeserver.domain.diaryParagraph.DiaryParagraph; +import com.example.aneukbeserver.domain.emotion.Emotion; +import com.example.aneukbeserver.domain.emotion.EmotionRepository; +import com.example.aneukbeserver.domain.member.Member; +import com.example.aneukbeserver.domain.selectedEmotion.SelectedEmotion; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class CollectionService { + + @Autowired + private CollectionRepository collectionRepository; + + @Autowired + private EmotionRepository emotionRepository; + + + public Map getEmotionCollection(Member member) { + Map statistics = new HashMap<>(); + + long totalEmotions = emotionRepository.count(); // 혹시 감정이 추가될수도 있으니 .. + + // 특정 멤버의 Collection + List collections = collectionRepository.findByMember(member); + + // 사용한 전체 + long usedEmotions = collections.stream() + .map(collection -> collection.getEmotion().getId()) + .distinct() + .count(); + + statistics.put("usedEmotionCount", usedEmotions); + statistics.put("totalEmotionCount", totalEmotions); + + // 카테고리별 + Map categoryUsageStats = collections.stream() + .collect(Collectors.groupingBy( + collection -> collection.getEmotion().getCategory(), + Collectors.counting() + )); + + Map categoryStats = new HashMap<>(); + for (Map.Entry entry : categoryUsageStats.entrySet()) { + String category = entry.getKey(); + long count = entry.getValue(); + long totalInCategory = emotionRepository.countByCategory(category); + + categoryStats.put(category, Map.of( + "usedCount", count, + "totalCount", totalInCategory + )); + } + statistics.put("categoryStats", categoryStats); + + return statistics; + } + + public void saveEmotionCollection(List emotionList, Member member) { + for (Emotion emotion : emotionList) { + Optional existingCollection = collectionRepository.findByMemberAndEmotion(member, emotion); + if (existingCollection.isPresent()) { + Collection collection = existingCollection.get(); + collection.setUsageCount(collection.getUsageCount() + 1); + collectionRepository.save(collection); + } + else { + Collection newCollection = new Collection(); + newCollection.setMember(member); + newCollection.setEmotion(emotion); + newCollection.setUsageCount(1L); + collectionRepository.save(newCollection); + } + } + + + } +} diff --git a/src/main/java/com/example/aneukbeserver/service/DiaryService.java b/src/main/java/com/example/aneukbeserver/service/DiaryService.java index ad37213..3b86275 100644 --- a/src/main/java/com/example/aneukbeserver/service/DiaryService.java +++ b/src/main/java/com/example/aneukbeserver/service/DiaryService.java @@ -62,7 +62,7 @@ public String mergeParagraph(List paragraphs) { .map(paragraph -> paragraph.getFinalContent() != null ? paragraph.getFinalContent() : paragraph.getOriginalContent() - ).collect(Collectors.joining()); + ).collect(Collectors.joining(" ")); } public List getAllDiary(Member member) { diff --git a/src/main/java/com/example/aneukbeserver/service/SelectedEmotionService.java b/src/main/java/com/example/aneukbeserver/service/SelectedEmotionService.java index a407536..0eb973b 100644 --- a/src/main/java/com/example/aneukbeserver/service/SelectedEmotionService.java +++ b/src/main/java/com/example/aneukbeserver/service/SelectedEmotionService.java @@ -1,7 +1,10 @@ package com.example.aneukbeserver.service; +import com.example.aneukbeserver.domain.collection.Collection; +import com.example.aneukbeserver.domain.collection.CollectionRepository; import com.example.aneukbeserver.domain.diaryParagraph.DiaryParagraph; import com.example.aneukbeserver.domain.emotion.Emotion; +import com.example.aneukbeserver.domain.member.Member; import com.example.aneukbeserver.domain.selectedEmotion.SelectedEmotion; import com.example.aneukbeserver.domain.selectedEmotion.SelectedEmotionRepository; import lombok.RequiredArgsConstructor; @@ -10,6 +13,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; @Service public class SelectedEmotionService { @@ -25,6 +29,8 @@ public void saveSelectedEmotions(DiaryParagraph diaryParagraph, List em selectedEmotion.setEmotion(emotion); selectedEmotion.setDiaryParagraph(diaryParagraph); selectedEmotionRepository.save(selectedEmotion); + + } }