-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from NewMillenniumWorkout/dev/statistics
[DEV] 감정 도감 API 구현
- Loading branch information
Showing
10 changed files
with
217 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/example/aneukbeserver/domain/collection/Collection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/aneukbeserver/domain/collection/CollectionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
5 changes: 0 additions & 5 deletions
5
src/main/java/com/example/aneukbeserver/domain/emotion/CategoryEmotionCount.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/aneukbeserver/domain/emotion/SaveEmotionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/example/aneukbeserver/service/CollectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters