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 #33 from FinFellows/develop
Browse files Browse the repository at this point in the history
[FEAT]: User, Bookmark 기능 API 배포
  • Loading branch information
sejineer authored Jan 2, 2024
2 parents dcc63d0 + 3ad8529 commit 02bcd4d
Show file tree
Hide file tree
Showing 18 changed files with 689 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.finfellows.domain.bookmark.application;

import com.finfellows.global.config.security.token.UserPrincipal;
import com.finfellows.global.payload.Message;
import org.springframework.http.ResponseEntity;

public interface BookmarkService {
Message insert(UserPrincipal userPrincipal, Long id);

Message delete(UserPrincipal userPrincipal, Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.finfellows.domain.bookmark.application;

import com.finfellows.domain.bookmark.domain.EduContentBookmark;
import com.finfellows.domain.bookmark.domain.repository.EduContentBookmarkRepository;
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.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.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class EduContentBookmarkServiceImpl implements BookmarkService{
private final EduContentBookmarkRepository eduContentBookmarkRepository;
private final UserRepository userRepository;
private final EduContentRepository eduContentRepository;
@Transactional
@Override
public Message insert(UserPrincipal userPrincipal, Long id) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<EduContent> optionalEduContent = eduContentRepository.findById(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)
.eduContent(eduContent)
.build();

eduContentBookmarkRepository.save(eduContentBookmark);

return Message.builder()
.message("즐겨찾기 추가에 성공했습니다.")
.build();

}

@Transactional
@Override
public Message delete(UserPrincipal userPrincipal, Long id) {

Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<EduContent> optionalEduContent = eduContentRepository.findById(id);

User user = optionalUser.get();
EduContent eduContent = optionalEduContent.get();

EduContentBookmark eduContentBookmark = eduContentBookmarkRepository.findByUserAndEduContent(user, eduContent).get();

eduContentBookmarkRepository.delete(eduContentBookmark);


return Message.builder()
.message("즐겨찾기 삭제에 성공했습니다.")
.build();
}

public ResponseCustom<List<EduContentBookmarkRes>> findBookmarks(UserPrincipal userPrincipal) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());

User user = optionalUser.get();

List<EduContentBookmark> bookmarks = eduContentBookmarkRepository.findAllByUser(user);


List<EduContentBookmarkRes> eduContentBookmarkResList = EduContentBookmarkRes.toDto(bookmarks);


return ResponseCustom.OK(eduContentBookmarkResList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.finfellows.domain.bookmark.application;

import com.finfellows.domain.bookmark.domain.EduContentBookmark;
import com.finfellows.domain.bookmark.domain.FinancialProductBookmark;
import com.finfellows.domain.bookmark.domain.repository.EduContentBookmarkRepository;
import com.finfellows.domain.bookmark.domain.repository.FinancialProductBookmarkRepository;
import com.finfellows.domain.bookmark.dto.EduContentBookmarkRes;
import com.finfellows.domain.product.domain.FinancialProduct;
import com.finfellows.domain.product.domain.repository.FinancialProductRepository;
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.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class FinancialProductBookmarkServiceImpl implements BookmarkService{
private final FinancialProductBookmarkRepository financialProductBookmarkRepository;
private final UserRepository userRepository;
private final FinancialProductRepository financialProductRepository;
private final EduContentBookmarkRepository eduContentBookmarkRepository;

@Transactional
@Override
public Message insert(UserPrincipal userPrincipal, Long id) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<FinancialProduct> optionalFinancialProduct = financialProductRepository.findById(id);

User user = optionalUser.get();
FinancialProduct financialProduct = optionalFinancialProduct.get();


FinancialProductBookmark financialProductBookmark = FinancialProductBookmark.builder()
.user(user)
.financialProduct(financialProduct)
.build();

financialProductBookmarkRepository.save(financialProductBookmark);

return Message.builder()
.message("즐겨찾기 추가에 성공했습니다.")
.build();
}

@Transactional
@Override
public Message delete(UserPrincipal userPrincipal, Long id) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<FinancialProduct> optionalFinancialProduct = financialProductRepository.findById(id);

User user = optionalUser.get();
FinancialProduct financialProduct = optionalFinancialProduct.get();

FinancialProductBookmark financialProductBookmark = financialProductBookmarkRepository.findByUserAndFinancialProduct(user, financialProduct).get();

financialProductBookmarkRepository.delete(financialProductBookmark);


return Message.builder()
.message("즐겨찾기 삭제에 성공했습니다.")
.build();
}

public ResponseCustom<List<EduContentBookmarkRes>> findBookmarks(UserPrincipal userPrincipal) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());

User user = optionalUser.get();

List<EduContentBookmark> bookmarks = eduContentBookmarkRepository.findAllByUser(user);


List<EduContentBookmarkRes> eduContentBookmarkResList = EduContentBookmarkRes.toDto(bookmarks);


return ResponseCustom.OK(eduContentBookmarkResList);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.finfellows.domain.bookmark.application;

import com.finfellows.domain.bookmark.domain.PolicyInfoBookmark;
import com.finfellows.domain.bookmark.domain.repository.PolicyInfoBookmarkRepository;
import com.finfellows.domain.bookmark.dto.PolicyInfoBookmarkRes;
import com.finfellows.domain.policyinfo.domain.PolicyInfo;
import com.finfellows.domain.policyinfo.domain.repository.PolicyInfoRepository;
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.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class PolicyInfoBookmarkServiceImpl implements BookmarkService {
private final PolicyInfoBookmarkRepository policyInfoBookmarkRepository;
private final UserRepository userRepository;
private final PolicyInfoRepository policyInfoRepository;

@Transactional
@Override
public Message insert(UserPrincipal userPrincipal, Long id) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<PolicyInfo> optionalPolicyInfo = policyInfoRepository.findById(id);

User user = optionalUser.get();
PolicyInfo policyInfo = optionalPolicyInfo.get();

PolicyInfoBookmark policyInfoBookmark = PolicyInfoBookmark.builder()
.user(user)
.policyInfo(policyInfo)
.build();

policyInfoBookmarkRepository.save(policyInfoBookmark);


return Message.builder()
.message("즐겨찾기 추가에 성공했습니다.")
.build();
}

@Transactional
@Override
public Message delete(UserPrincipal userPrincipal, Long id) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());
Optional<PolicyInfo> optionalPolicyInfo = policyInfoRepository.findById(id);

User user = optionalUser.get();
PolicyInfo policyInfo = optionalPolicyInfo.get();

PolicyInfoBookmark policyInfoBookmark = policyInfoBookmarkRepository.findByUserAndPolicyInfo(user, policyInfo).get();

policyInfoBookmarkRepository.delete(policyInfoBookmark);


return Message.builder()
.message("즐겨찾기 삭제에 성공했습니다.")
.build();
}

public ResponseCustom<?> findBookmarks(UserPrincipal userPrincipal) {
Optional<User> optionalUser = userRepository.findByEmail(userPrincipal.getEmail());

User user = optionalUser.get();

List<PolicyInfoBookmark> bookmarks = policyInfoBookmarkRepository.findAllByUser(user);

List<PolicyInfoBookmarkRes> policyInfoBookmarkResList = PolicyInfoBookmarkRes.toDto(bookmarks);

return ResponseCustom.OK(policyInfoBookmarkResList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.finfellows.domain.bookmark.domain;

import com.finfellows.domain.common.BaseEntity;
import com.finfellows.domain.educontent.domain.EduContent;
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 = "EduContentBookmark")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Where(clause = "status = 'ACTIVE'")
public class EduContentBookmark 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;

// 금융 배우자 id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "edu_content_id")
private EduContent eduContent;

@Builder
public EduContentBookmark(User user, EduContent eduContent) {
this.user = user;
this.eduContent = eduContent;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.finfellows.domain.bookmark.domain;

import com.finfellows.domain.common.BaseEntity;
import com.finfellows.domain.educontent.domain.EduContent;
import com.finfellows.domain.policyinfo.domain.PolicyInfo;
import com.finfellows.domain.product.domain.FinancialProduct;
import com.finfellows.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -12,36 +11,28 @@
import org.hibernate.annotations.Where;

@Entity
@Table(name = "Bookmark")
@Table(name = "FinancialProductBookmark")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Where(clause = "status = 'ACTIVE'")
public class Bookmark extends BaseEntity{

public class FinancialProductBookmark 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;

// 금융 뭐하지 id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "financial_product_id")
private FinancialProduct financialProduct;

// 금융 배우자 id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "edu_content_id")
private EduContent eduContent;

// 금융 고마워 id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "policy_info_id")
private PolicyInfo policyInfo;

@Builder
public Bookmark(FinancialProduct financialProduct, EduContent eduContent, PolicyInfo policyInfo) {
public FinancialProductBookmark(User user, FinancialProduct financialProduct) {
this.user = user;
this.financialProduct = financialProduct;
this.eduContent = eduContent;
this.policyInfo = policyInfo;
}
}
Loading

0 comments on commit 02bcd4d

Please sign in to comment.