Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #66 from FinFellows/develop
Browse files Browse the repository at this point in the history
[FEAT]: 금융, 뭐하지? 북마크 기능 수정, post, eduContent, newsContent 양방향 연관관계 설정…
  • Loading branch information
sejineer authored Jan 8, 2024
2 parents 2f6220e + d79f230 commit 99a11d8
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class AuthController {
private final KakaoService kakaoService;


// @Operation(summary = "카카오 code 발급", description = "카카오 API 서버에 접근 권한을 인가하는 code를 발급받습니다.")
// @ApiResponses(value = {
// @ApiResponse(responseCode = "200", description = "code 발급 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = AuthRes.class))}),
// @ApiResponse(responseCode = "400", description = "code 발급 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}),
// })
// @GetMapping(value = "/login")
// public void socialLoginRedirect() throws IOException {
// kakaoService.accessRequest();
// }
@Operation(summary = "카카오 code 발급", description = "카카오 API 서버에 접근 권한을 인가하는 code를 발급받습니다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "code 발급 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = AuthRes.class))}),
@ApiResponse(responseCode = "400", description = "code 발급 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}),
})
@GetMapping(value = "/login")
public void socialLoginRedirect() throws IOException {
kakaoService.accessRequest();
}

@Operation(summary = "유저 정보 확인", description = "현재 접속 중인 유저의 정보를 확인합니다.")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.finfellows.domain.bookmark.dto.EduContentBookmarkRes;
import com.finfellows.domain.educontent.domain.EduContent;
import com.finfellows.domain.educontent.domain.repository.EduContentRepository;
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;
Expand All @@ -24,6 +26,8 @@ public class EduContentBookmarkServiceImpl implements BookmarkService{
private final EduContentBookmarkRepository eduContentBookmarkRepository;
private final UserRepository userRepository;
private final EduContentRepository eduContentRepository;
private final PostRepository postRepository;

@Transactional
@Override
public Message insert(UserPrincipal userPrincipal, Long id) {
Expand All @@ -33,11 +37,7 @@ public Message insert(UserPrincipal userPrincipal, Long id) {
User user = optionalUser.get();
EduContent eduContent = optionalEduContent.get();

// if (eduContentBookmarkRepository.findByUserAndEduContent(user, eduContent).isPresent()) {
// return Message.builder()
// .message("이미 즐겨찾기 목록에 존재합니다.")
// .build();
// }


EduContentBookmark eduContentBookmark = EduContentBookmark.builder()
.user(user)
Expand Down
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);
}
}
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;
}
}
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);

}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.finfellows.domain.bookmark.application.EduContentBookmarkServiceImpl;
import com.finfellows.domain.bookmark.application.FinancialProductBookmarkServiceImpl;
import com.finfellows.domain.bookmark.application.PolicyInfoBookmarkServiceImpl;
import com.finfellows.domain.bookmark.application.PostBookmarkServiceImpl;
import com.finfellows.domain.post.domain.ContentType;
import com.finfellows.global.config.security.token.CurrentUser;
import com.finfellows.global.config.security.token.UserPrincipal;
import com.finfellows.global.payload.ErrorResponse;
Expand All @@ -29,6 +31,7 @@ public class BookmarkController {
private final EduContentBookmarkServiceImpl eduContentBookmarkService;
private final FinancialProductBookmarkServiceImpl financialProductBookmarkService;
private final PolicyInfoBookmarkServiceImpl policyInfoBookmarkService;
private final PostBookmarkServiceImpl postBookmarkService;


@Operation(summary = "금융, 고마워 북마크", description = "금융, 고마워(정책)를 북마크한다.")
Expand Down Expand Up @@ -57,38 +60,39 @@ public ResponseCustom<?> deleteBookmarkPolicyInfo(
return ResponseCustom.OK(policyInfoBookmarkService.delete(userPrincipal, policy_info_id));
}

@Operation(summary = "금융, 배우자 북마크", description = "금융, 배우자(교육)를 북마크한다.")
@Operation(summary = "금융, 배우자 북마크", description = "금융, 배우자(교육, 뉴스)를 북마크한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "금융, 배우자.(교육) 즐겨찾기 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 배우자.(교육) 즐겨찾기 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
@ApiResponse(responseCode = "200", description = "금융, 배우자.(교육, 뉴스) 즐겨찾기 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 배우자.(교육, 뉴스) 즐겨찾기 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
})
@PostMapping("/edu-content/{edu_content_id}")
public ResponseCustom<?> bookmarkEduContent(
@PostMapping("/posts/{post_id}")
public ResponseCustom<?> bookmarkPost(
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal,
@Parameter(description = "금융, 배우자(교육) id를 입력해주세요.", required = true) @Valid @PathVariable("edu_content_id") Long edu_content_id
@Parameter(description = "금융, 배우자(교육, 뉴스) id를 입력해주세요.", required = true) @Valid @PathVariable("post_id") Long post_id,
@Parameter(description = "ContentType을 Params로 입력해주세요. 예시) contentType=EDU_CONTENT", required = true) @RequestParam("contentType") ContentType contentType
) {
return ResponseCustom.OK(eduContentBookmarkService.insert(userPrincipal, edu_content_id));
return ResponseCustom.OK(postBookmarkService.insert(userPrincipal, post_id, contentType));
}

@Operation(summary = "금융, 배우자 북마크 삭제", description = "금융, 배우자(교육) 북마크를 삭제한다.")
@Operation(summary = "금융, 배우자 북마크 삭제", description = "금융, 배우자(교육, 뉴스) 북마크를 삭제한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "금융, 배우자(교육). 즐겨찾기 삭제 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 배우자(교육). 즐겨찾기 삭제 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
@ApiResponse(responseCode = "200", description = "금융, 배우자(교육, 뉴스). 즐겨찾기 삭제 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 배우자(교육, 뉴스). 즐겨찾기 삭제 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
})
@DeleteMapping("/edu-content/{edu_content_id}")
public ResponseCustom<?> deleteBookmarkEduContent(
@DeleteMapping("/posts/{post_id}")
public ResponseCustom<?> deleteBookmarkPost(
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal,
@Parameter(description = "금융, 배우자(교육) id를 입력해주세요.", required = true) @Valid @PathVariable("edu_content_id") Long edu_content_id
@Parameter(description = "금융, 배우자(교육, 뉴스) id를 입력해주세요.", required = true) @Valid @PathVariable("post_id") Long post_id
) {
return ResponseCustom.OK(eduContentBookmarkService.delete(userPrincipal, edu_content_id));
return ResponseCustom.OK(postBookmarkService.delete(userPrincipal, post_id));
}

@Operation(summary = "금융, 뭐하지 북마크", description = "금융, 뭐하지(금융 상품)를 북마크한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "금융, 뭐하지(금융 상품) 즐겨찾기 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 뭐하지(금융 상품) 즐겨찾기 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
})
@PostMapping("/financial-product/{financial_product_id}")
@PostMapping("/financial-products/{financial_product_id}")
public ResponseCustom<?> bookmarkFinancialProduct(
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal,
@Parameter(description = "금융, 뭐하지(금융 상품) id를 입력해주세요.", required = true) @Valid @PathVariable("financial_product_id") Long financial_product_id
Expand All @@ -101,7 +105,7 @@ public ResponseCustom<?> bookmarkFinancialProduct(
@ApiResponse(responseCode = "200", description = "금융, 뭐하지(금융 상품). 즐겨찾기 삭제 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ),
@ApiResponse(responseCode = "400", description = "금융, 뭐하지(금융 상품). 즐겨찾기 삭제 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ),
})
@DeleteMapping("/financial-product/{financial_product_id}")
@DeleteMapping("/financial-products/{financial_product_id}")
public ResponseCustom<?> deleteBookmarkFinancialProduct(
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal,
@Parameter(description = "금융, 뭐하지(금융 상품) id를 입력해주세요.", required = true) @Valid @PathVariable("financial_product_id") Long financial_product_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public void updateContent(String title, String content) {
this.content = content;
}



}
13 changes: 13 additions & 0 deletions src/main/java/com/finfellows/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.finfellows.domain.post.domain;

import com.finfellows.domain.common.BaseEntity;
import com.finfellows.domain.educontent.domain.EduContent;
import com.finfellows.domain.newscontent.domain.NewsContent;
import com.finfellows.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -9,6 +11,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Where;

import java.util.List;

@Entity
@Table(name="Post")
@NoArgsConstructor(access = AccessLevel.PUBLIC)
Expand All @@ -30,10 +34,19 @@ public class Post extends BaseEntity {
@Column(name="contentType")
private ContentType contentType;

@OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
private List<EduContent> eduContent;

@OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
private List<NewsContent> newsContent;


@Builder
public Post(User writer, ContentType contentType){
this.writer=writer;
this.contentType=contentType;
}



}
Loading

0 comments on commit 99a11d8

Please sign in to comment.