1+ package com .haruhan .content .service ;
2+
3+ import com .haruhan .common .error .CustomException ;
4+ import com .haruhan .common .error .StatusCode ;
5+ import com .haruhan .common .error .entity .Content ;
6+ import com .haruhan .common .error .repository .ContentRepository ;
7+ import com .haruhan .content .dto .ContentResDto ;
8+ import com .haruhan .user .entity .User ;
9+ import com .haruhan .user .repository .UserRepository ;
10+ import jakarta .transaction .Transactional ;
11+ import lombok .RequiredArgsConstructor ;
12+ import org .springframework .stereotype .Service ;
13+
14+ import java .util .Arrays ;
15+ import java .util .List ;
16+ import java .util .stream .Collectors ;
17+
18+ @ Service
19+ @ Transactional
20+ @ RequiredArgsConstructor
21+ public class ContentServiceImpl implements ContentService {
22+
23+ private final UserRepository userRepository ;
24+ private final ContentRepository contentRepository ;
25+
26+ @ Override
27+ public List <ContentResDto > getUserReceivedContent (String email ) {
28+ User user = userRepository .findByEmail (email )
29+ .orElseThrow (() -> new CustomException (StatusCode .NOT_FOUND_USER ));
30+
31+ Long lastContentId = user .getLastReceivedContentId ();
32+
33+ if (lastContentId == null || lastContentId < 1 ) {
34+ throw new CustomException (StatusCode .NOT_FOUND );
35+ }
36+
37+ List <Content > contentList = contentRepository .findUpToLastContent (lastContentId );
38+
39+ return contentList .stream ()
40+ .map (this ::convertToDto )
41+ .collect (Collectors .toList ());
42+ }
43+
44+
45+ @ Override
46+ public List <ContentResDto > getTop5BookmarkedContent () {
47+ List <Content > top5Content = contentRepository .findTop5ByBookmarkCount ();
48+
49+ return top5Content .stream ()
50+ .map (this ::convertToDto )
51+ .collect (Collectors .toList ());
52+ }
53+
54+
55+ //문자열을 줄바꿈 기준으로 분리하여 리스트로 변환
56+ private List <String > splitByNewLine (String text ) {
57+ return Arrays .stream (text .split ("\n " ))
58+ .map (String ::trim )
59+ .filter (s -> !s .isEmpty ())
60+ .collect (Collectors .toList ());
61+ }
62+
63+
64+ //Content -> ContentResDto 변환
65+ private ContentResDto convertToDto (Content content ) {
66+ return new ContentResDto (
67+ content .getContent_id (),
68+ content .getTitle (),
69+ content .getSummary (),
70+ splitByNewLine (content .getBackground ()),
71+ splitByNewLine (content .getImportance ()),
72+ splitByNewLine (content .getTip ()),
73+ splitByNewLine (content .getAdditional_resources ())
74+ );
75+ }
76+ }
0 commit comments