This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #66 from FinFellows/develop
[FEAT]: 금융, 뭐하지? 북마크 기능 수정, post, eduContent, newsContent 양방향 연관관계 설정…
- Loading branch information
Showing
10 changed files
with
254 additions
and
36 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
82 changes: 82 additions & 0 deletions
82
src/main/java/com/finfellows/domain/bookmark/application/PostBookmarkServiceImpl.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,82 @@ | ||
package com.finfellows.domain.bookmark.application; | ||
|
||
import com.finfellows.domain.bookmark.domain.PostBookmark; | ||
import com.finfellows.domain.bookmark.domain.repository.PostBookmarkRepository; | ||
import com.finfellows.domain.bookmark.dto.PostBookmarkRes; | ||
import com.finfellows.domain.post.domain.ContentType; | ||
import com.finfellows.domain.post.domain.Post; | ||
import com.finfellows.domain.post.domain.repository.PostRepository; | ||
import com.finfellows.domain.user.domain.User; | ||
import com.finfellows.domain.user.domain.repository.UserRepository; | ||
import com.finfellows.global.config.security.token.UserPrincipal; | ||
import com.finfellows.global.payload.Message; | ||
import com.finfellows.global.payload.ResponseCustom; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostBookmarkServiceImpl { | ||
private final UserRepository userRepository; | ||
private final PostRepository postRepository; | ||
private final PostBookmarkRepository postBookmarkRepository; | ||
|
||
|
||
@Transactional | ||
public Message insert(UserPrincipal userPrincipal, Long postId, ContentType contentType) { | ||
Optional<User> optionalUser = userRepository.findById(userPrincipal.getId()); | ||
Optional<Post> optionalPost = postRepository.findById(postId); | ||
|
||
User user = optionalUser.get(); | ||
Post post = optionalPost.get(); | ||
|
||
|
||
PostBookmark postBookmark = PostBookmark.builder() | ||
.user(user) | ||
.post(post) | ||
.contentType(contentType) | ||
.build(); | ||
|
||
postBookmarkRepository.save(postBookmark); | ||
|
||
return Message.builder() | ||
.message("즐겨찾기 추가에 성공했습니다.") | ||
.build(); | ||
} | ||
|
||
@Transactional | ||
public Message delete(UserPrincipal userPrincipal, Long postId) { | ||
Optional<User> optionalUser = userRepository.findById(userPrincipal.getId()); | ||
Optional<Post> optionalPost = postRepository.findById(postId); | ||
|
||
User user = optionalUser.get(); | ||
Post post = optionalPost.get(); | ||
|
||
PostBookmark postBookmark = postBookmarkRepository.findByUserAndPost(user, post).get(); | ||
|
||
postBookmarkRepository.delete(postBookmark); | ||
|
||
|
||
return Message.builder() | ||
.message("즐겨찾기 삭제에 성공했습니다.") | ||
.build(); | ||
} | ||
|
||
@Transactional | ||
public ResponseCustom<?> findBookmarks(UserPrincipal userPrincipal) { | ||
Optional<User> optionalUser = userRepository.findById(userPrincipal.getId()); | ||
|
||
User user = optionalUser.get(); | ||
|
||
List<PostBookmark> bookmarks = postBookmarkRepository.findAllByUser(user); | ||
|
||
|
||
List<PostBookmarkRes> postBookmarkResList = PostBookmarkRes.toDto(bookmarks); | ||
|
||
return ResponseCustom.OK(postBookmarkResList); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/finfellows/domain/bookmark/domain/PostBookmark.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,42 @@ | ||
package com.finfellows.domain.bookmark.domain; | ||
|
||
import com.finfellows.domain.common.BaseEntity; | ||
import com.finfellows.domain.post.domain.ContentType; | ||
import com.finfellows.domain.post.domain.Post; | ||
import com.finfellows.domain.user.domain.User; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.Where; | ||
|
||
@Entity | ||
@Table(name = "PostBookmark") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@Where(clause = "status = 'ACTIVE'") | ||
public class PostBookmark extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", updatable = false) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private ContentType contentType; | ||
|
||
@Builder | ||
public PostBookmark(User user, Post post, ContentType contentType) { | ||
this.user = user; | ||
this.post = post; | ||
this.contentType = contentType; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/finfellows/domain/bookmark/domain/repository/PostBookmarkRepository.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,17 @@ | ||
package com.finfellows.domain.bookmark.domain.repository; | ||
|
||
import com.finfellows.domain.bookmark.domain.PostBookmark; | ||
import com.finfellows.domain.post.domain.ContentType; | ||
import com.finfellows.domain.post.domain.Post; | ||
import com.finfellows.domain.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface PostBookmarkRepository extends JpaRepository<PostBookmark, Long> { | ||
Optional<PostBookmark> findByUserAndPost(User user, Post post); | ||
|
||
List<PostBookmark> findAllByUser(User user); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/finfellows/domain/bookmark/dto/PostBookmarkRes.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,56 @@ | ||
package com.finfellows.domain.bookmark.dto; | ||
|
||
import com.finfellows.domain.bookmark.domain.PostBookmark; | ||
import com.finfellows.domain.educontent.domain.EduContent; | ||
import com.finfellows.domain.newscontent.domain.NewsContent; | ||
import com.finfellows.domain.post.domain.ContentType; | ||
import com.finfellows.domain.post.domain.Post; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Data | ||
public class PostBookmarkRes { | ||
private String title; | ||
private String content; | ||
private ContentType contentType; | ||
|
||
|
||
@Builder | ||
public PostBookmarkRes(String title, String content, ContentType contentType) { | ||
this.title = title; | ||
this.content = content; | ||
this.contentType = contentType; | ||
} | ||
|
||
|
||
public static List<PostBookmarkRes> toDto(List<PostBookmark> bookmarks) { | ||
List<PostBookmarkRes> results = new ArrayList<>(); | ||
|
||
for (PostBookmark bookmark : bookmarks) { | ||
Post post = bookmark.getPost(); | ||
ContentType contentType = post.getContentType(); | ||
|
||
|
||
if (contentType == ContentType.EDU_CONTENT) { | ||
for (EduContent eduContent : post.getEduContent()) { | ||
String title = eduContent.getTitle(); | ||
String content = eduContent.getContent(); | ||
results.add(new PostBookmarkRes(title, content, contentType)); | ||
} | ||
} | ||
if (contentType == ContentType.NEWS_CONTENT) { | ||
for (NewsContent newsContent : post.getNewsContent()) { | ||
String title = newsContent.getTitle(); | ||
String content = newsContent.getContent(); | ||
results.add(new PostBookmarkRes(title, content, contentType)); | ||
} | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,6 @@ public void updateContent(String title, String content) { | |
this.content = content; | ||
} | ||
|
||
|
||
|
||
} |
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
Oops, something went wrong.