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

커플 연결 관련 수정 #127

Merged
merged 3 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -57,4 +59,11 @@ public CoupleConnectionResponseDto checkConnection(
) {
return coupleService.checkConnection(userId);
}

@DeleteMapping("")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void disconnectCouple(@AuthenticationPrincipal Long userId) {
coupleService.disconnectCouple(userId);
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/universe/uni/exception/dto/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public enum ErrorType {
*/
USER_ALREADY_EXISTS_EXCEPTION(HttpStatus.CONFLICT, "UE10001",
"이미 존재하는 유저에 대한 생성에 대한 경우입니다."),
COUPLE_ALREADY_CONNECTED(HttpStatus.CONFLICT, "UE10002",
"이미 커플이 연결된 초대코드입니다."),

/**
* 415 Unsupported Media Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
User findByCoupleIdAndIdNot(Long coupleId, Long userId);

User findByCoupleId(Long coupleId);

int countByCoupleId(Long coupleId);
}
20 changes: 20 additions & 0 deletions src/main/java/com/universe/uni/service/CoupleService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.universe.uni.service;

import static com.universe.uni.exception.dto.ErrorType.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
Expand All @@ -15,11 +17,13 @@
import com.universe.uni.dto.response.CoupleConnectionResponseDto;
import com.universe.uni.dto.response.CoupleDto;
import com.universe.uni.exception.BadRequestException;
import com.universe.uni.exception.ConflictException;
import com.universe.uni.exception.dto.ErrorType;
import com.universe.uni.mapper.CoupleMapper;
import com.universe.uni.repository.CoupleRepository;
import com.universe.uni.repository.UserRepository;

import feign.FeignException;
import lombok.RequiredArgsConstructor;

@Service
Expand Down Expand Up @@ -61,12 +65,19 @@ public CoupleDto findCouple(Long coupleId) {
public void joinCouple(Long userId, String inviteCode) {
final Couple couple = coupleRepository.findByInviteCode(inviteCode)
.orElseThrow(() -> new BadRequestException(ErrorType.INVALID_INVITE_CODE));
validateIfCoupleConnected(couple);
final User user = userRepository.findById(userId)
.orElseThrow(() -> new BadRequestException(ErrorType.USER_NOT_EXISTENT));
user.connectCouple(couple);

}

private void validateIfCoupleConnected(Couple couple) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

커플연결검증 정도로 validateCoupleConnexted정도로 해도 좋을 것 같아요~

if(userRepository.countByCoupleId(couple.getId()) >= 2) {
throw new ConflictException(COUPLE_ALREADY_CONNECTED);
}
}

@Override
@Transactional
public CoupleDto updateCoupleStartDate(Long coupleId, String startDate) {
Expand Down Expand Up @@ -95,4 +106,13 @@ public CoupleConnectionResponseDto checkConnection(Long userId) {
public void deleteCouple(Long coupleId) {
coupleRepository.deleteById(coupleId);
}

@Override
@Transactional
public void disconnectCouple(Long userId) {
final User user = userRepository.findById(userId)
.orElseThrow(() -> new BadRequestException(ErrorType.USER_NOT_EXISTENT));
final Couple couple = user.getCouple();
deleteCouple(couple.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ CoupleDto createCoupleByStartDate(

@Transactional
void deleteCouple(Long coupleId);

@Transactional
void disconnectCouple(Long userId);
}
Loading