Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Friend 일부 리팩터링- #67 #93

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions favor/src/main/java/com/favor/favor/friend/Friend.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.favor.favor.friend;


import com.favor.favor.common.enums.Favor;
import lombok.*;

import com.favor.favor.common.TimeStamped;
import com.favor.favor.reminder.Reminder;
import com.favor.favor.user.User;

import javax.persistence.*;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class Friend extends TimeStamped {

@Id
Expand All @@ -27,33 +23,39 @@ public class Friend extends TimeStamped {
private String friendName;

private String friendMemo;
public void setFriendMemo(String friendMemo) {
this.friendMemo = friendMemo;
}


@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinColumn(name = "user_user_no")
private User user;


@Builder.Default
@OneToMany(mappedBy = "friend", orphanRemoval = true)
private List<Reminder> reminderList = new ArrayList<>();

private Long friendUserNo;

@Builder.Default
@ElementCollection
private List<Long> giftNoList = new ArrayList<>();
public void setGiftNoList(List<Long> giftNoList){
this.giftNoList = giftNoList;
}

@Builder.Default
@ElementCollection
private List<Long> anniversaryNoList = new ArrayList<>();
public void setAnniversaryNoList(List<Long> anniversaryNoList){

public void updateFriendMemo(String friendMemo) {
this.friendMemo = friendMemo;
}

public void updateGiftNoList(List<Long> giftNoList){
this.giftNoList = giftNoList;
}

@Builder
public Friend(Long friendNo, String friendName, String friendMemo, User user, List<Reminder> reminderList, Long friendUserNo, List<Long> giftNoList, List<Long> anniversaryNoList) {
this.friendNo = friendNo;
this.friendName = friendName;
this.friendMemo = friendMemo;
this.user = user;
this.reminderList = reminderList;
this.friendUserNo = friendUserNo;
this.giftNoList = giftNoList;
this.anniversaryNoList = anniversaryNoList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ public ResponseEntity<DefaultResponseDto<Object>> addFriend(

Long userNo = loginUser.getUserNo();

friendService.isExistingUserNo(userNo);
friendService.isExistingFriendUserNo(friendRequestDto.getFriendUserNo());

Friend friend = friendService.addFriend(friendRequestDto, userNo);
FriendResponseDto dto = friendService.returnDto(friend);
FriendResponseDto friendResponseDto = friendService.returnDto(friend);

return ResponseEntity.status(201)
.body(DefaultResponseDto.from("FRIEND_ADDED", "친구 추가 완료", dto));
.body(DefaultResponseDto.from("FRIEND_ADDED", "친구 추가 완료", friendResponseDto));
}

@ApiOperation("친구 조회")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class FriendRequestDto {
@ApiModelProperty(position = 1, required = true, dataType = "Long", value = "회원친구번호", example = "1")
private Long friendUserNo;
@Transactional

public Friend toEntity(User user, User userFriend){
return Friend.builder()
.friendUserNo(friendUserNo)
Expand All @@ -23,4 +22,4 @@ public Friend toEntity(User user, User userFriend){
.user(user)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;

@Getter
@AllArgsConstructor
public class FriendResponseDto {

private Long friendNo;
Expand Down
84 changes: 51 additions & 33 deletions favor/src/main/java/com/favor/favor/friend/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import com.favor.favor.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static com.favor.favor.exception.ExceptionCode.*;

Expand All @@ -33,13 +35,20 @@ public class FriendService {
@Transactional
public Friend addFriend(FriendRequestDto dto, Long userNo){
User user = findUserByUserNo(userNo);
isExistingUserNo(userNo);

Long friendUserNo = dto.getFriendUserNo();
isExistingFriendUserNo(friendUserNo);

User friendUser = findUserByUserNo(friendUserNo);

if(isDuplicateFriendUser(user, friendUser)) {
throw new CustomException(null, DUPLICATE_FRIEND);
}
return save(dto.toEntity(user, friendUser));
Friend friend = dto.toEntity(user, friendUser);
save(friend);

return friend;
}

public Boolean isDuplicateFriendUser(User user, User friendUser){
Expand All @@ -56,7 +65,7 @@ public Boolean isDuplicateFriendUser(User user, User friendUser){

@Transactional
public void updateMemo(Friend friend, MemoUpdateRequestDto memoUpdateRequestDto){
friend.setFriendMemo(memoUpdateRequestDto.getMemo());
friend.updateFriendMemo(memoUpdateRequestDto.getMemo());
friendRepository.save(friend);

}
Expand All @@ -78,10 +87,14 @@ public void deleteFriend(Long friendNo){
}

public List<FriendResponseDto> readAll(){
List<FriendResponseDto> f_List = new ArrayList<>();
List<Friend> friendList = friendRepository.findAll();
for(Friend f : friendList) f_List.add(returnDto(f));
return f_List;
// List<FriendResponseDto> f_List = new ArrayList<>();
// List<Friend> friendList = friendRepository.findAll();
// for(Friend f : friendList) f_List.add(returnDto(f));
// return f_List;

return friendRepository.findAll().stream()
.map(friend -> returnDto(friend))
.collect(Collectors.toList());
}


Expand Down Expand Up @@ -132,35 +145,40 @@ public Friend findFriendByFriendNo(Long friendNo){
return friend;
}
public List<GiftSimpleDto> findGiftListByFriendNo(Long friendNo){
List<Gift> giftList = giftRepository.findGiftsByFriendNoListContains(friendNo);
List<GiftSimpleDto> giftResponseDtoList = new ArrayList<>();
for(Gift gift : giftList){
GiftSimpleDto dto = GiftSimpleDto.from(gift);
giftResponseDtoList.add(dto);
}
return giftResponseDtoList;
return giftRepository.findGiftsByFriendNoListContains(friendNo).stream()
.map(GiftSimpleDto::from)
.collect(Collectors.toList());
}
public List<GiftSimpleDto> findGivenGiftList(Long friendNo){
List<Gift> giftList = giftRepository.findGiftsByFriendNoListContains(friendNo);
List<GiftSimpleDto> giftResponseDtoList = new ArrayList<>();
for(Gift gift : giftList){
if(gift.getIsGiven()){
GiftSimpleDto dto = GiftSimpleDto.from(gift);
giftResponseDtoList.add(dto);
}
}
return giftResponseDtoList;
// List<Gift> giftList = giftRepository.findGiftsByFriendNoListContains(friendNo);
// List<GiftSimpleDto> giftResponseDtoList = new ArrayList<>();
// for(Gift gift : giftList){
// if(gift.getIsGiven()){
// GiftSimpleDto dto = GiftSimpleDto.from(gift);
// giftResponseDtoList.add(dto);
// }
// }
// return giftResponseDtoList;
return giftRepository.findGiftsByFriendNoListContains(friendNo).stream()
.filter(gift -> gift.getIsGiven())
.map(GiftSimpleDto::from)
.collect(Collectors.toList());
}
public List<GiftSimpleDto> findReceivedGiftList(Long friendNo){
List<Gift> giftList = giftRepository.findGiftsByFriendNoListContains(friendNo);
List<GiftSimpleDto> giftResponseDtoList = new ArrayList<>();
for(Gift gift : giftList){
if(!gift.getIsGiven()){
GiftSimpleDto dto = GiftSimpleDto.from(gift);
giftResponseDtoList.add(dto);
}
}
return giftResponseDtoList;
// List<Gift> giftList = giftRepository.findGiftsByFriendNoListContains(friendNo);
// List<GiftSimpleDto> giftResponseDtoList = new ArrayList<>();
// for(Gift gift : giftList){
// if(!gift.getIsGiven()){
// GiftSimpleDto dto = GiftSimpleDto.from(gift);
// giftResponseDtoList.add(dto);
// }
// }
// return giftResponseDtoList;

return giftRepository.findGiftsByFriendNoListContains(friendNo).stream()
.filter(gift -> !gift.getIsGiven())
.map(GiftSimpleDto::from)
.collect(Collectors.toList());
}


Expand Down Expand Up @@ -215,7 +233,7 @@ public HashMap<String, Integer> returnGiftInfo(Long friendNo) {

//IS_EXISTING
public void isExistingUserNo (Long userNo){
Boolean isExistingNo = null;
Boolean isExistingNo;
try{
isExistingNo = userRepository.existsByUserNo(userNo);
} catch(RuntimeException e){
Expand All @@ -227,7 +245,7 @@ public void isExistingUserNo (Long userNo){
}

public void isExistingFriendUserNo (Long userNo){
Boolean isExistingNo = null;
Boolean isExistingNo;
try{
isExistingNo = userRepository.existsByUserNo(userNo);
} catch(RuntimeException e){
Expand Down
2 changes: 1 addition & 1 deletion favor/src/main/java/com/favor/favor/gift/GiftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void addGiftNo(Long giftNo, List<Long> friendNoList){

if(!giftNoList.contains(giftNo)) giftNoList.add(giftNo);

friend.setGiftNoList(giftNoList);
friend.updateGiftNoList(giftNoList);
}
}

Expand Down