Skip to content

Commit

Permalink
Merge pull request #65 from Team-Shaka/feat/64
Browse files Browse the repository at this point in the history
✨ Feat : 초대하기 API 구현
  • Loading branch information
CYY1007 authored Jun 21, 2024
2 parents 9dffb63 + f60fd72 commit 1342337
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;


Expand All @@ -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 에게 온 초대장인지 검증하는 로직 추가
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,4 +23,8 @@ public class InvitationCommandAdapter {
private final InvitationRepository invitationRepository;


public Invitation saveInvitation(Invitation invitation){
return invitationRepository.save(invitation);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ public CommonResponse<InvitationResponseDTO.invitationAccept> acceptInvitation(
return CommonResponse.onSuccess(invitationService.decisionInvitation(user, request));
}

@PostMapping("/invitation")
@Operation(summary = "초대하기 API \uD83D\uDD11✅ 🔑", description = "초대하기 API 입니다. 초대 후 문자나 카카오톡 보내는 것과 별개로 API 호출 부탁드립니닷")
public CommonResponse<InvitationResponseDTO.createInvitation> createInvitation(
@AuthMember @Parameter(hidden = true)User user, @RequestBody InvitationRequestDTO.createInvitation request
){
return CommonResponse.onSuccess(invitationService.createInvitation(user,request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.*;


@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class InvitationRequestDTO {

@Getter
Expand All @@ -13,5 +13,13 @@ public static class invitationAcceptDecision{

}

@Getter
@NoArgsConstructor
public static class createInvitation{
private Long senderId;
private String phoneNumber;
private Long treehouseId;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public static class invitationAccept {
private Long treehouseId;

}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class createInvitation{
private Long invitationId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public interface MemberRepository extends JpaRepository<Member, Long> {

public Optional<Member> findByUser(User user);
Optional<Member> findByUser(User user);

Optional<Member> findByUserAndTreeHouse(User user, TreeHouse treehouse);
}

0 comments on commit 1342337

Please sign in to comment.