Skip to content

Commit

Permalink
Merge pull request #37 from NewMillenniumWorkout/dev/statistics
Browse files Browse the repository at this point in the history
[DEV] 감정 도감 API 구현
  • Loading branch information
LUCETE012 authored Dec 4, 2024
2 parents ea95994 + 7b65339 commit 9cf01e3
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,6 +73,9 @@ public class DiaryController {
@Autowired
private SelectedEmotionService selectedEmotionService;

@Autowired
private CollectionService collectionService;


@Value("${spring.ai.url}")
private String aiUrl;
Expand Down Expand Up @@ -180,8 +185,39 @@ public ResponseEntity<StatusResponseDto> 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<StatusResponseDto> saveParagraphEmotion(@Parameter(hidden = true) @RequestHeader("Authorization") final String accessToken, @RequestBody SaveEmotionDTO request) {
String userEmail = jwtUtil.getEmail(accessToken.substring(7));
Optional<Member> member = memberService.findByEmail(userEmail);
log.info(String.valueOf(request));
Optional<DiaryParagraph> 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<String> emotion_list = request.getEmotions();

List<Emotion> 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 = "서버 에러, 관리자에게 문의 바랍니다."),
Expand All @@ -203,15 +239,11 @@ public ResponseEntity<StatusResponseDto> remakeParagraph(@Parameter(hidden = tru

List<String> emotion_list = request.getEmotions();

List<Emotion> emotionList = emotionService.getEmotionObjectsByNames(emotion_list);

Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand All @@ -72,4 +62,26 @@ public ResponseEntity<StatusResponseDto> 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<StatusResponseDto> getEmotionCollection(@Parameter(hidden = true) @RequestHeader("Authorization") final String accessToken) {
String userEmail = jwtUtil.getEmail(accessToken.substring(7));
Optional<Member> member = memberService.findByEmail(userEmail);

if (member.isEmpty())
return ResponseEntity.badRequest().body(addStatus(400, "사용자가 존재하지 않습니다."));

Map<String, Object> stats = collectionService.getEmotionCollection(member.get());

return ResponseEntity.ok().body(addStatus(200, stats));

}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Collection, Long> {

List<Collection> findByMember(Member member);

Optional<Collection> findByMemberAndEmotion(Member member, Emotion emotion);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface EmotionRepository extends JpaRepository<Emotion, Long> {
Optional<Emotion> findByTitle(String title);

long countByCategory(String category);
}
Original file line number Diff line number Diff line change
@@ -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<String> emotions;
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> getEmotionCollection(Member member) {
Map<String, Object> statistics = new HashMap<>();

long totalEmotions = emotionRepository.count(); // 혹시 감정이 추가될수도 있으니 ..

// 특정 멤버의 Collection
List<Collection> collections = collectionRepository.findByMember(member);

// 사용한 전체
long usedEmotions = collections.stream()
.map(collection -> collection.getEmotion().getId())
.distinct()
.count();

statistics.put("usedEmotionCount", usedEmotions);
statistics.put("totalEmotionCount", totalEmotions);

// 카테고리별
Map<String, Long> categoryUsageStats = collections.stream()
.collect(Collectors.groupingBy(
collection -> collection.getEmotion().getCategory(),
Collectors.counting()
));

Map<String, Object> categoryStats = new HashMap<>();
for (Map.Entry<String, Long> 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<Emotion> emotionList, Member member) {
for (Emotion emotion : emotionList) {
Optional<Collection> 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);
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String mergeParagraph(List<DiaryParagraph> paragraphs) {
.map(paragraph -> paragraph.getFinalContent() != null
? paragraph.getFinalContent()
: paragraph.getOriginalContent()
).collect(Collectors.joining());
).collect(Collectors.joining(" "));
}

public List<DiaryDTO> getAllDiary(Member member) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,6 +13,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class SelectedEmotionService {
Expand All @@ -25,6 +29,8 @@ public void saveSelectedEmotions(DiaryParagraph diaryParagraph, List<Emotion> em
selectedEmotion.setEmotion(emotion);
selectedEmotion.setDiaryParagraph(diaryParagraph);
selectedEmotionRepository.save(selectedEmotion);


}
}

Expand Down

0 comments on commit 9cf01e3

Please sign in to comment.