diff --git a/src/main/java/treehouse/server/api/invitation/business/InvitationMapper.java b/src/main/java/treehouse/server/api/invitation/business/InvitationMapper.java index f48d16f..28b7382 100644 --- a/src/main/java/treehouse/server/api/invitation/business/InvitationMapper.java +++ b/src/main/java/treehouse/server/api/invitation/business/InvitationMapper.java @@ -5,10 +5,15 @@ import org.springframework.stereotype.Component; import treehouse.server.api.invitation.presentation.dto.InvitationResponseDTO; import treehouse.server.global.entity.Invitation.Invitation; +import treehouse.server.global.entity.Invitation.InvitationStatus; import treehouse.server.global.entity.User.User; import treehouse.server.global.entity.User.UserRole; import treehouse.server.global.entity.User.UserStatus; +import treehouse.server.global.entity.member.Member; +import treehouse.server.global.entity.treeHouse.TreeHouse; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.List; @Component @@ -43,5 +48,28 @@ public InvitationResponseDTO.invitationAccept toInvitationResult(Long treeHouseI .treehouseId(treeHouseId) .build(); } + + public Invitation toInvitation(String phoneNumber, Member sender, User receiver, TreeHouse treeHouse){ + + + LocalDateTime now = LocalDateTime.now(); + + // 7일 뒤 시간 계산하기 + LocalDateTime sevenDaysLater = now.plus(7, ChronoUnit.DAYS); + + return Invitation.builder() + .sender(sender) + .receiver(receiver) + .phone(phoneNumber) + .expiredAt(sevenDaysLater) + .status(InvitationStatus.PENDING) + .build(); + } + + public InvitationResponseDTO.createInvitation toCreateInvitationDTO (Invitation invitation){ + return InvitationResponseDTO.createInvitation.builder() + .invitationId(invitation.getId()) + .build(); + } } diff --git a/src/main/java/treehouse/server/api/invitation/business/InvitationService.java b/src/main/java/treehouse/server/api/invitation/business/InvitationService.java index 29b85ec..2ba17e3 100644 --- a/src/main/java/treehouse/server/api/invitation/business/InvitationService.java +++ b/src/main/java/treehouse/server/api/invitation/business/InvitationService.java @@ -8,10 +8,14 @@ import treehouse.server.api.invitation.implement.InvitationQueryAdapter; import treehouse.server.api.invitation.presentation.dto.InvitationRequestDTO; import treehouse.server.api.invitation.presentation.dto.InvitationResponseDTO; +import treehouse.server.api.member.implementation.MemberQueryAdapter; +import treehouse.server.api.treehouse.implementation.TreehouseQueryAdapter; +import treehouse.server.api.user.implement.UserQueryAdapter; import treehouse.server.global.entity.Invitation.Invitation; import treehouse.server.global.entity.User.User; import treehouse.server.global.entity.member.Member; import treehouse.server.global.entity.treeHouse.TreeHouse; +import treehouse.server.global.exception.ThrowClass.UserException; import java.util.List; import java.util.stream.Collectors; @@ -26,6 +30,13 @@ public class InvitationService { private final InvitationCommandAdapter invitationCommandAdapter; private final InvitationMapper invitationMapper; + + private final TreehouseQueryAdapter treehouseQueryAdapter; + + private final MemberQueryAdapter memberQueryAdapter; + + private final UserQueryAdapter userQueryAdapter; + private static final Integer treeMemberRandomProfileSize = 3; @@ -52,6 +63,32 @@ public InvitationResponseDTO.myInvitationInfo getMyInvitationInfo(User user){ return invitationMapper.toMyInvitationInfo(user); } + @Transactional + public InvitationResponseDTO.createInvitation createInvitation(User user, InvitationRequestDTO.createInvitation request){ + + // 트리 찾기 + TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(request.getTreehouseId()); + // 초대 멤버 찾기 + Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treehouse); + // 초대 대상이 가입된 사람인지 찾기 + + User receiverUser = null; + + try { + receiverUser = userQueryAdapter.findByPhoneNumber(request.getPhoneNumber()); + } + catch (UserException e){ + // 뭐 안함 + } + // 초대장 만들어서 저장하기 + + Invitation invitation = invitationCommandAdapter.saveInvitation(invitationMapper.toInvitation(request.getPhoneNumber(), sender, receiverUser, treehouse)); + + // 리턴하기 + + return invitationMapper.toCreateInvitationDTO(invitation); + } + @Transactional public InvitationResponseDTO.invitationAccept decisionInvitation(User user, InvitationRequestDTO.invitationAcceptDecision request){ // 해당 User 에게 온 초대장인지 검증하는 로직 추가 diff --git a/src/main/java/treehouse/server/api/invitation/implement/InvitationCommandAdapter.java b/src/main/java/treehouse/server/api/invitation/implement/InvitationCommandAdapter.java index 24f3c30..dc1818c 100644 --- a/src/main/java/treehouse/server/api/invitation/implement/InvitationCommandAdapter.java +++ b/src/main/java/treehouse/server/api/invitation/implement/InvitationCommandAdapter.java @@ -5,6 +5,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import treehouse.server.api.invitation.persistence.InvitationRepository; import treehouse.server.global.annotations.Adapter; +import treehouse.server.global.entity.Invitation.Invitation; import treehouse.server.global.entity.User.User; import treehouse.server.global.entity.User.UserRole; import treehouse.server.global.entity.redis.RefreshToken; @@ -22,4 +23,8 @@ public class InvitationCommandAdapter { private final InvitationRepository invitationRepository; + public Invitation saveInvitation(Invitation invitation){ + return invitationRepository.save(invitation); + } + } diff --git a/src/main/java/treehouse/server/api/invitation/presentation/InvitationApi.java b/src/main/java/treehouse/server/api/invitation/presentation/InvitationApi.java index 39b068f..8bb036e 100644 --- a/src/main/java/treehouse/server/api/invitation/presentation/InvitationApi.java +++ b/src/main/java/treehouse/server/api/invitation/presentation/InvitationApi.java @@ -46,4 +46,11 @@ public CommonResponse acceptInvitation( return CommonResponse.onSuccess(invitationService.decisionInvitation(user, request)); } + @PostMapping("/invitation") + @Operation(summary = "초대하기 API \uD83D\uDD11✅ 🔑", description = "초대하기 API 입니다. 초대 후 문자나 카카오톡 보내는 것과 별개로 API 호출 부탁드립니닷") + public CommonResponse createInvitation( + @AuthMember @Parameter(hidden = true)User user, @RequestBody InvitationRequestDTO.createInvitation request + ){ + return CommonResponse.onSuccess(invitationService.createInvitation(user,request)); + } } diff --git a/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationRequestDTO.java b/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationRequestDTO.java index 0e51853..33574ae 100644 --- a/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationRequestDTO.java +++ b/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationRequestDTO.java @@ -2,7 +2,7 @@ import lombok.*; - +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class InvitationRequestDTO { @Getter @@ -13,5 +13,13 @@ public static class invitationAcceptDecision{ } + @Getter + @NoArgsConstructor + public static class createInvitation{ + private Long senderId; + private String phoneNumber; + private Long treehouseId; + } + } diff --git a/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationResponseDTO.java b/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationResponseDTO.java index 717db23..a973a18 100644 --- a/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationResponseDTO.java +++ b/src/main/java/treehouse/server/api/invitation/presentation/dto/InvitationResponseDTO.java @@ -45,4 +45,12 @@ public static class invitationAccept { private Long treehouseId; } + + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class createInvitation{ + private Long invitationId; + } } diff --git a/src/main/java/treehouse/server/api/member/persistence/MemberRepository.java b/src/main/java/treehouse/server/api/member/persistence/MemberRepository.java index 86296fa..6d4d543 100644 --- a/src/main/java/treehouse/server/api/member/persistence/MemberRepository.java +++ b/src/main/java/treehouse/server/api/member/persistence/MemberRepository.java @@ -9,7 +9,7 @@ public interface MemberRepository extends JpaRepository { - public Optional findByUser(User user); + Optional findByUser(User user); Optional findByUserAndTreeHouse(User user, TreeHouse treehouse); }