Skip to content

Commit

Permalink
Merge pull request #67 from Team-Shaka/feature/64
Browse files Browse the repository at this point in the history
Feature/64-트리하우스 목록 조회 및 변경 API 조회
  • Loading branch information
koojun99 authored Mar 7, 2024
2 parents 9544c15 + 5c91e05 commit 32033f4
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public Profile toProfile (Tree tree, Member member, String memberName, String bi
.memberName(memberName)
.bio(bio)
.profileImageUrl(profileImageUrl)
.isActive(true)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ public class Profile extends BaseDateTimeEntity {

private String profileImageUrl; //프로필 이미지(트리 별로 상이)

private boolean isActive; //현재 선택한 트리의 프로필인지 여부

@JoinColumn(name = "memberId")
@ManyToOne(fetch = FetchType.LAZY)
private Member member;
@JoinColumn(name = "treeId")
@ManyToOne(fetch = FetchType.LAZY)
private Tree tree;

public void actvate() {
this.isActive = true;
}
public void inactivate() {
this.isActive = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.tree.entity.Tree;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -17,4 +19,10 @@ public interface ProfileRepository extends JpaRepository<Profile, Long> {
List<Profile> findAllByMember_Id(String memberId);

List<Profile> findAllByTree(Tree tree);

@Query("SELECT p FROM Profile p WHERE p.member = :member AND p.isActive = TRUE")
Optional<Profile> findCurrentProfile(@Param("member") Member member);

@Query("SELECT COUNT(p) = 0 FROM Profile p WHERE p.member = :member AND p.isActive = TRUE")
boolean existsActiveProfileByMember(@Param("member") Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public class ProfileCommandService {
public void createProfile(Profile profile) {
profileRepository.save(profile);
}

public void updateProfile(Profile profile) {
profileRepository.save(profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public List<Long> findJoinedTree(Profile profile) {
public List<Profile> findTreeMembers(Tree tree) {
return profileRepository.findAllByTree(tree);
}

public Profile getCurrentProfile(Member member) {
return profileRepository.findCurrentProfile(member)
.orElseThrow(() -> new GeneralException(GlobalErrorCode.AVAILABLE_PROFILE_NOT_FOUND));
}

public boolean isNewUser(Member member) {
return profileRepository.existsActiveProfileByMember(member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.example.tree.domain.invitation.service.InvitationQueryService;
import org.example.tree.domain.member.entity.Member;
import org.example.tree.domain.member.service.MemberQueryService;
import org.example.tree.domain.post.service.PostService;
import org.example.tree.domain.profile.converter.ProfileConverter;
import org.example.tree.domain.profile.dto.ProfileRequestDTO;
import org.example.tree.domain.profile.dto.ProfileResponseDTO;
Expand Down Expand Up @@ -38,6 +37,12 @@ public class ProfileService {
public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createProfile request, MultipartFile profileImage) throws Exception {
Tree tree = treeQueryService.findById(request.getTreeId());
Member member = memberQueryService.findById(request.getUserId());
boolean isNewUser = profileQueryService.isNewUser(member);
if (!isNewUser) {
Profile currentProfile = profileQueryService.getCurrentProfile(member);
currentProfile.inactivate();
profileCommandService.updateProfile(currentProfile);
}
String profileImageUrl = !profileImage.isEmpty() ? s3UploadService.uploadImage(profileImage) : DEFAULT_PROFILE_IMAGE;
Profile newProfile = profileConverter.toProfile(tree, member, request.getMemberName(), request.getBio(), profileImageUrl);
profileCommandService.createProfile(newProfile);
Expand All @@ -52,6 +57,12 @@ public ProfileResponseDTO.createProfile createProfile(ProfileRequestDTO.createPr
public ProfileResponseDTO.createProfile ownerProfile(ProfileRequestDTO.ownerProfile request, MultipartFile profileImage) throws Exception {
Tree tree = treeQueryService.findById(request.getTreeId());
Member member = memberQueryService.findById(request.getUserId());
boolean isNewUser = profileQueryService.isNewUser(member);
if (!isNewUser) {
Profile currentProfile = profileQueryService.getCurrentProfile(member);
currentProfile.inactivate();
profileCommandService.updateProfile(currentProfile);
}
String profileImageUrl = !profileImage.isEmpty() ? s3UploadService.uploadImage(profileImage) : DEFAULT_PROFILE_IMAGE;
Profile newProfile = profileConverter.toProfile(tree, member, request.getMemberName(), request.getBio(), profileImageUrl);
profileCommandService.createProfile(newProfile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.example.tree.domain.tree.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.example.tree.domain.tree.dto.TreeRequestDTO;
import org.example.tree.domain.tree.dto.TreeResponseDTO;
import org.example.tree.domain.tree.service.TreeService;
import org.example.tree.global.common.ApiResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/trees")
public class TreeController {
private final TreeService treeService;

@Operation(summary = "트리하우스 등록")
@PostMapping("/register")
public ApiResponse createTree(
@RequestBody TreeRequestDTO.createTree request
Expand All @@ -24,5 +25,24 @@ public ApiResponse createTree(
return ApiResponse.onSuccess("");
}

@Operation(summary = "트리하우스 조회")
@GetMapping
public ApiResponse<List<TreeResponseDTO.getTree>> getTrees(
@RequestHeader("Authorization") final String header
)
{
String token = header.replace("Bearer ", "");
return ApiResponse.onSuccess(treeService.getTrees(token));
}

@Operation(summary = "트리하우스 위치 변경")
@PostMapping("/{treeId}")
public ApiResponse<TreeResponseDTO.shiftTree> shiftTree(
@RequestHeader("Authorization") final String header,
@PathVariable final Long treeId
) {
String token = header.replace("Bearer ", "");
return ApiResponse.onSuccess(treeService.shiftTree(treeId, token));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package org.example.tree.domain.tree.converter;

import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.tree.dto.TreeResponseDTO;
import org.example.tree.domain.tree.entity.Tree;
import org.springframework.stereotype.Component;

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

@Component
public class TreeConverter {
public Tree toTree (String name) {
return Tree.builder()
.name(name)
.build();
}

public TreeResponseDTO.getTree toGetTree(Tree tree, List<String> treeMemberProfileImages, Profile currentProfile) {
Boolean isSelected = currentProfile.getTree().getId().equals(tree.getId());
return TreeResponseDTO.getTree.builder()
.treeName(tree.getName())
.treeSize(tree.getTreeSize())
.treeMemberProfileImages(treeMemberProfileImages)
.isSelected(isSelected)
.build();
}

public TreeResponseDTO.shiftTree toShiftTree(Tree tree) {
return TreeResponseDTO.shiftTree.builder()
.treeId(tree.getId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package org.example.tree.domain.tree.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TreeResponseDTO {
// @Getter
// public static class registerTree {
// private String treeName;
// }
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class getTree {
private String treeName;
private Integer treeSize;
private List<String> treeMemberProfileImages;
private Boolean isSelected;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class shiftTree {
private Long treeId;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.example.tree.domain.tree.service;

import lombok.RequiredArgsConstructor;
import org.example.tree.domain.member.entity.Member;
import org.example.tree.domain.tree.entity.Tree;
import org.example.tree.domain.tree.repository.TreeRepository;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TreeQueryService {
Expand All @@ -16,4 +19,5 @@ public Tree findById(Long id) {
return treeRepository.findById(id)
.orElseThrow(()->new GeneralException(GlobalErrorCode.TREE_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,62 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.example.tree.domain.member.entity.Member;
import org.example.tree.domain.member.service.MemberQueryService;
import org.example.tree.domain.profile.entity.Profile;
import org.example.tree.domain.profile.service.ProfileQueryService;
import org.example.tree.domain.tree.converter.TreeConverter;
import org.example.tree.domain.tree.dto.TreeRequestDTO;
import org.example.tree.domain.tree.dto.TreeResponseDTO;
import org.example.tree.domain.tree.entity.Tree;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class TreeService {
private final TreeCommandService treeCommandService;
private final TreeQueryService treeQueryService;
private final TreeConverter treeConverter;
private final MemberQueryService memberQueryService;
private final ProfileQueryService profileQueryService;

@Transactional
public void createTree(TreeRequestDTO.createTree request) {
Tree tree = treeConverter.toTree(request.getTreeName());
treeCommandService.createTree(tree);
}

@Transactional
public List<TreeResponseDTO.getTree> getTrees(String token) {
Member member = memberQueryService.findByToken(token);
Profile currentProfile = profileQueryService.getCurrentProfile(member);
List<Long> treeIds = profileQueryService.findJoinedTree(currentProfile);
List<Tree> trees = treeIds.stream()
.map(treeQueryService::findById)
.collect(Collectors.toList());
return trees.stream()
.map(tree -> {
List<Profile> treeMembers = profileQueryService.findTreeMembers(tree); // Tree의 모든 멤버를 조회합니다.
List<String> randomProfileImages = treeMembers.stream()
.map(Profile::getProfileImageUrl)
.limit(3) // 최대 3명
.collect(Collectors.toList());
return treeConverter.toGetTree(tree, randomProfileImages, currentProfile);
})
.collect(Collectors.toList());
}

@Transactional
public TreeResponseDTO.shiftTree shiftTree(Long treeId, String token) {
Member member = memberQueryService.findByToken(token);
Tree tree = treeQueryService.findById(treeId);
Profile currentProfile = profileQueryService.getCurrentProfile(member);
currentProfile.inactivate();
Profile shiftedProfile = profileQueryService.getTreeProfile(member, tree);
shiftedProfile.actvate();
return treeConverter.toShiftTree(tree);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum GlobalErrorCode {
//Profile
//404 Not Found - 찾을 수 없음
PROFILE_NOT_FOUND(NOT_FOUND, "존재하지 않는 프로필입니다."),
AVAILABLE_PROFILE_NOT_FOUND(NOT_FOUND, "현재 선택된 프로필이 없습니다."),

//Tree
//404 Not Found - 찾을 수 없음
Expand Down Expand Up @@ -54,7 +55,8 @@ public enum GlobalErrorCode {

//Notification
//404 Not Found - 찾을 수 없음
NOTIFICATION_NOT_FOUND(NOT_FOUND, "알림이 없습니다.");
NOTIFICATION_NOT_FOUND(NOT_FOUND, "알림이 없습니다."),
;



Expand Down

0 comments on commit 32033f4

Please sign in to comment.