Skip to content

Commit

Permalink
Merge pull request #38 from 2E1I/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
oU-Ua authored May 16, 2024
2 parents 617f999 + c013614 commit 8f0a4b0
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ public enum ErrorCode{


//좋아요 관련 에러 코드
LIKE_NOT_FOUND(94040,HttpStatus.NOT_FOUND,"사용자가 모음집을 좋아요한 기록이 없습니다.")
LIKE_NOT_FOUND(94040,HttpStatus.NOT_FOUND,"사용자가 모음집을 좋아요한 기록이 없습니다."),

//친구 관련 에러 코드
FRINEDS_NOT_FOUND(104040, HttpStatus.NOT_FOUND, "해당 사용자와 친구가 아닙니다.");

;
private final int errorCode;
private final HttpStatus httpStatusCode;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public interface CollaboratorsRepository extends JpaRepository<CollaboratorsEnti
Optional<List<UsersEntity>> findCollaboratorsByCollection(@Param("collection") CollectionsEntity collection);

Optional<List<CollaboratorsEntity>> findByCollection(CollectionsEntity collection);

@Query("select count(*) from CollaboratorsEntity where user=:user")
long countCollectionByUser(@Param("user") UsersEntity user);

long countCollaboratorsEntitiesByUser(UsersEntity user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class CollaboratorsService {
private final CollaboratorsRepository collaboratorsRepository;
Expand All @@ -32,4 +34,8 @@ public void insert(CollaboratorsEntity collaborator) {
public List<CollaboratorsEntity> findByCollection(CollectionsEntity collection) {
return collaboratorsRepository.findByCollection(collection).orElseThrow(()->new ApiException(ErrorCode.COLLABORATOR_NOT_FOUND));
}

public long countCollection(UsersEntity user) {
return collaboratorsRepository.countCollaboratorsEntitiesByUser(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public CollectionUserResDTO getCollection(Long id, UsersEntity user) {
List<TagsEntity> tagList = tagsService.findByCollection(collection);
List<String> tagDTOList = tagList.stream().map(TagsEntity::getTagName)
.collect(Collectors.toList());
CollectionUserResDTO result = collectionsConverter.toCollectionUserResDTO(collection,
return collectionsConverter.toCollectionUserResDTO(collection,
linkDTOList, tagDTOList,isLike);
return result;
}
public List<CollectionResDTO> getUserCollection(UsersEntity user){
List<CollectionsEntity> collectionList = collaboratorsService.findCollectionByUser(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
public class CollectionsConverter {

public CollectionUserResDTO toCollectionUserResDTO(CollectionsEntity collection, List<CollectionLinkDTO> linkList, List<String> tagsList, boolean isLike){
CollectionUserResDTO userResDTO = CollectionUserResDTO.builder()
return CollectionUserResDTO.builder()
.title(collection.getTitle())
.imgUrl(collection.getImgURL())
.description(collection.getDescription())
.linkList(linkList)
.tagList(tagsList)
.isLike(isLike)
.build();
return userResDTO;
}

public CollectionTitleResDTO toCollectionTitleResDTO(CollectionsEntity collection){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
package com.e2i1.linkeepserver.domain.friends.business;

import com.e2i1.linkeepserver.common.annotation.Business;
import com.e2i1.linkeepserver.domain.collaborators.service.CollaboratorsService;
import com.e2i1.linkeepserver.domain.friends.converter.FriendsConverter;
import com.e2i1.linkeepserver.domain.friends.dto.FriendStatusResDTO;
import com.e2i1.linkeepserver.domain.friends.dto.FriendsResDTO;
import com.e2i1.linkeepserver.domain.friends.entity.FriendsEntity;
import com.e2i1.linkeepserver.domain.friends.service.FriendsService;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import com.e2i1.linkeepserver.domain.users.service.UsersService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import io.jsonwebtoken.lang.Collections;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@Business
@RequiredArgsConstructor
public class FriendsBusiness {
private final FriendsService friendsService;
private final UsersService usersService;
private final CollaboratorsService collaboratorsService;
private final FriendsConverter friendsConverter;


public List<FriendsResDTO> getFollowers(UsersEntity user) {
List<FriendsEntity> FriendsList = friendsService.getFollowers(user);
return FriendsList.stream().map(friend -> {
long cnt = collaboratorsService.countCollection(friend.getFollowingUser());
return friendsConverter.toFriendsResDTO(friend.getFollowingUser(), cnt);
}).toList();

}

public List<FriendsResDTO> getFollowings(UsersEntity user) {
List<FriendsEntity> FriendsList = friendsService.getFollowings(user);
for(FriendsEntity f : FriendsList)
System.out.println(f.getId());
return FriendsList.stream().map(friend -> {
long cnt = collaboratorsService.countCollection(friend.getFollowedUser());
System.out.println(friend.getFollowedUser()+" : "+cnt);

return friendsConverter.toFriendsResDTO(friend.getFollowedUser(), cnt);
}).toList();

}

public void insertFollow(String nickName, UsersEntity followingUser) {
UsersEntity followedUser = usersService.findByNickName(nickName);
FriendsEntity friend = friendsConverter.toFriendsEntity(followedUser,followingUser);
friendsService.insertFriend(friend);
}

@Transactional
public FriendStatusResDTO changeStatus(long userId, UsersEntity follower ) {
UsersEntity followee = usersService.findById(userId);
FriendsEntity friend = friendsService.findByFollowedUserAndFollowingUser(followee,follower);
friend.updateStatus();
return friendsConverter.toFriendStatusResDTO(friend.getIsFollowing());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.e2i1.linkeepserver.domain.friends.controller;

import com.e2i1.linkeepserver.common.annotation.UserSession;
import com.e2i1.linkeepserver.domain.friends.business.FriendsBusiness;
import com.e2i1.linkeepserver.domain.friends.dto.FriendReqDTO;
import com.e2i1.linkeepserver.domain.friends.dto.FriendStatusReqDTO;
import com.e2i1.linkeepserver.domain.friends.dto.FriendStatusResDTO;
import com.e2i1.linkeepserver.domain.friends.dto.FriendsResDTO;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -14,21 +20,26 @@
public class FriendsController {
private final FriendsBusiness friendsBusiness;

@GetMapping()
public ResponseEntity<FriendsResDTO> getFriends(){
return ResponseEntity.ok(null);
@GetMapping("/follower")
public ResponseEntity<List<FriendsResDTO>> getFollowers(@UserSession UsersEntity user){
return ResponseEntity.ok(friendsBusiness.getFollowers(user));
}

@GetMapping("/following")
public ResponseEntity<List<FriendsResDTO>> getFollowings(@UserSession UsersEntity user){
return ResponseEntity.ok(friendsBusiness.getFollowings(user));
}

@PostMapping()
public ResponseEntity<String> insertFriend(@RequestBody String nickName){
public ResponseEntity<String> insertFriend(@RequestBody FriendReqDTO friendReqDTO, @UserSession UsersEntity user){
friendsBusiness.insertFollow(friendReqDTO.getNickname(), user);
return ResponseEntity.ok("success");
}

@PostMapping("/follow")
public ResponseEntity<HashMap<String,String>> isFollow(@RequestBody Long userId){
HashMap<String,String> result = new HashMap<>();
result.put("isFollowing","Y");
return ResponseEntity.ok(result);
public ResponseEntity<FriendStatusResDTO> isFollow(@RequestBody FriendStatusReqDTO reqDTO, @UserSession UsersEntity user){

return ResponseEntity.ok(friendsBusiness.changeStatus(reqDTO.getUserId(),user));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
package com.e2i1.linkeepserver.domain.friends.converter;

import com.e2i1.linkeepserver.common.annotation.Converter;
import com.e2i1.linkeepserver.domain.friends.dto.FriendStatusResDTO;
import com.e2i1.linkeepserver.domain.friends.dto.FriendsResDTO;
import com.e2i1.linkeepserver.domain.friends.entity.FriendsEntity;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import lombok.RequiredArgsConstructor;

@Converter
@RequiredArgsConstructor
public class FriendsConverter {

public FriendsEntity toFriendsEntity(UsersEntity followedUser, UsersEntity followingUser) {
return FriendsEntity.builder()
.followedUser(followedUser)
.followingUser(followingUser)
.isFollowing(true).build();
}

public FriendsResDTO toFriendsResDTO(UsersEntity user, long cnt) {
return FriendsResDTO.builder().nickName(user.getNickname()).userId(user.getId()).numOfCollections(cnt).build();
}
public FriendStatusResDTO toFriendStatusResDTO(boolean followStatus){
return FriendStatusResDTO.builder().followStatus(followStatus).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.e2i1.linkeepserver.domain.friends.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FriendReqDTO {
private String nickname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.e2i1.linkeepserver.domain.friends.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FriendStatusReqDTO {
private long userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.e2i1.linkeepserver.domain.friends.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FriendStatusResDTO {
private boolean followStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@AllArgsConstructor
public class FriendsResDTO {
private String nickName;
private Long userId;
private Long numOfCollections;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ public void setFollowingUser(UsersEntity usersEntity) {
public void setFollowedUser(UsersEntity usersEntity) {
followedUser = usersEntity;
}
public void updateStatus(){
this.isFollowing = !this.isFollowing;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.e2i1.linkeepserver.domain.friends.repository;

import com.e2i1.linkeepserver.domain.friends.entity.FriendsEntity;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.Repository;

public interface FriendsRepository extends Repository<FriendsEntity,Long> {
public interface FriendsRepository extends JpaRepository<FriendsEntity,Long> {
public List<FriendsEntity> findByFollowedUserAndIsFollowing(UsersEntity user,boolean isFollow);
public List<FriendsEntity> findByFollowingUserAndIsFollowing(UsersEntity user,boolean isFollow);
Optional <FriendsEntity> findByFollowedUserAndFollowingUser(UsersEntity followee, UsersEntity follower);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package com.e2i1.linkeepserver.domain.friends.service;

import com.e2i1.linkeepserver.common.error.ErrorCode;
import com.e2i1.linkeepserver.common.exception.ApiException;
import com.e2i1.linkeepserver.domain.friends.entity.FriendsEntity;
import com.e2i1.linkeepserver.domain.friends.repository.FriendsRepository;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class FriendsService {
private FriendsRepository friendsRepository;
private final FriendsRepository friendsRepository;

public List<FriendsEntity> getFollowers(UsersEntity user) {
return friendsRepository.findByFollowedUserAndIsFollowing(user,true);
}

public List<FriendsEntity> getFollowings(UsersEntity user) {
return friendsRepository.findByFollowingUserAndIsFollowing(user,true);
}
public FriendsEntity insertFriend(FriendsEntity friend) {
return friendsRepository.save(friend);
}

public FriendsEntity findByFollowedUserAndFollowingUser(UsersEntity followee, UsersEntity follower) {
return friendsRepository.findByFollowedUserAndFollowingUser(followee,follower).orElseThrow(() -> new ApiException(ErrorCode.FRINEDS_NOT_FOUND));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public class UsersEntity extends DateEntity {
private List<LikeOthersEntity> likeOthersList;

@OneToMany(mappedBy = "followingUser", cascade = ALL)
private Set<FriendsEntity> followingList; // 내가 팔로우한 유저 리스트
private List<FriendsEntity> followingList; // 내가 팔로우한 유저 리스트

@OneToMany(mappedBy = "followedUser", cascade = ALL)
private Set<FriendsEntity> followerList; // 나를 팔로우한 유저 리스트
private List<FriendsEntity> followerList; // 나를 팔로우한 유저 리스트

@OneToMany(mappedBy = "user", cascade = ALL)
private List<LinksEntity> linkList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface UsersRepository extends JpaRepository<UsersEntity, Long> {
Optional<UsersEntity> findByNicknameAndStatus(String nickname, UserStatus status);

List<UsersEntity> findByNicknameContainingAndStatus(String search, UserStatus status);

UsersEntity findByNickname(String nickName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ public void editProfile(String imgUrl, EditProfileReqDTO editProfile, UsersEntit
// user는 UserSessionResolver에서 getUserWithThrow로 가져온 user 객체임 -> 즉, 영속성 컨텍스트 안에 있음 -> dirty checking 가능
}

public UsersEntity findByNickName(String nickName) {
return usersRepository.findByNickname(nickName);
}
}
16 changes: 14 additions & 2 deletions src/test/java/com/e2i1/linkeepserver/CollectionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.e2i1.linkeepserver.domain.collections.business.CollectionsBusiness;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionResDTO;
import com.e2i1.linkeepserver.domain.collections.dto.CollectionUserResDTO;
import com.e2i1.linkeepserver.domain.friends.entity.FriendsEntity;
import com.e2i1.linkeepserver.domain.users.business.UsersBusiness;
import com.e2i1.linkeepserver.domain.users.entity.UsersEntity;
import com.e2i1.linkeepserver.domain.users.service.UsersService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,9 +19,17 @@
public class CollectionTests {

@Autowired
private CollectionsBusiness collectionsBusiness;

private UsersBusiness usersBusiness;

@Autowired
private UsersService usersService;

@Test
public void 팔로잉_목록(){
UsersEntity user= usersService.findById(2L);
for(FriendsEntity f : user.getFollowerList())
System.out.println("id : "+f.getId());
}



Expand Down

0 comments on commit 8f0a4b0

Please sign in to comment.