Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -17,7 +17,7 @@ public enum ErrorCode implements BaseCode {
USER_NOT_FOUND_BY_EMAIL(HttpStatus.NOT_FOUND, "USER_4042", "존재하지 않는 회원입니다.-EMAIL"),
USER_NOT_FOUND_BY_USERNAME(HttpStatus.NOT_FOUND, "USER_4043", "존재하지 않는 회원입니다.-USERNAME & KEY"),
USER_NOT_OLDER(HttpStatus.FORBIDDEN, "USER_4031", "해당 유저가 노인이 아니랍니다."),
USER_NOT_PROTECTOR(HttpStatus.FORBIDDEN, "USER_4032", "해당 유저가 보호자가 아닙니다."),
USER_NOT_GUARDIAN(HttpStatus.FORBIDDEN, "USER_4032", "해당 유저가 보호자가 아닙니다."),

// Jwt
WRONG_REFRESH_TOKEN(HttpStatus.NOT_FOUND, "JWT_4041", "일치하는 리프레시 토큰이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public enum SuccessCode implements BaseCode {
USER_NICKNAME_UPDATE_SUCCESS(HttpStatus.OK, "USER_2004", "닉네임 수정이 완료되었습니다."),
USER_ADDRESS_UPDATE_SUCCESS(HttpStatus.OK, "USER_2005", "주소지 수정이 완료되었습니다."),
USER_MYPAGE_VIEW_SUCCESS(HttpStatus.OK, "USER_2006", "마이페이지 정보 조회가 완료되었습니다."),
USER_PROTECTOR_CONNECT_OLDER_SUCCESS(HttpStatus.OK, "USER_2007", "보호자의 노인 연결이 완료되었습니다."),
USER_PROTECTOR_REGISTER_OLDER_SUCCESS(HttpStatus.OK, "USER_2008", "보호자의 노인 등록이 완료되었습니다."),
USER_GUARDIAN_CONNECT_OLDER_SUCCESS(HttpStatus.OK, "USER_2007", "보호자의 노인 연결이 완료되었습니다."),
USER_GUARDIAN_REGISTER_OLDER_SUCCESS(HttpStatus.OK, "USER_2008", "보호자의 노인 등록이 완료되었습니다."),

// Recommend Activity
RECOMMEND_ACTIVITY_VIEW_LIST_SUCCESS(HttpStatus.OK, "ACTIVITY_2001", "추천하는 활동 리스를 반환 완료했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.example.silverbridgeX_user.user.dto.UserRequestDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto.UserAddressReqDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto.UserNicknameReqDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.GuardianMyPageResDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.OlderMyPageResDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.ProtectorMyPageResDto;
import com.example.silverbridgeX_user.user.jwt.CustomUserDetails;
import com.example.silverbridgeX_user.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -112,48 +112,48 @@ public ApiResponse<OlderMyPageResDto> mypageOlder(
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2006", description = "노인 마이페이지 정보 조회가 완료되었습니다.")
})
@GetMapping(value = "/mypage/protector")
public ApiResponse<ProtectorMyPageResDto> mypageProtector(
@GetMapping(value = "/mypage/guardian")
public ApiResponse<GuardianMyPageResDto> mypageGuardian(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
User protector = userService.findByUserName(customUserDetails.getUsername());
userService.validateProtector(protector);
List<User> olders = protector.getOlders();
User guardian = userService.findByUserName(customUserDetails.getUsername());
userService.validateGuardian(guardian);
List<User> olders = guardian.getOlders();

return ApiResponse.onSuccess(SuccessCode.USER_MYPAGE_VIEW_SUCCESS,
UserConverter.protectorMyPageResDto(protector, olders));
UserConverter.guardianMyPageResDto(guardian, olders));
}

@Operation(summary = "보호자의 노인 연결", description = "보호자가 관리할 노인을 연결하는 메서드입니다.")
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2007", description = "보호자의 노인 연결이 완료되었습니다.")
})
@PostMapping(value = "/protectors/older-links")
@PostMapping(value = "/guardians/older-links")
public ApiResponse<Boolean> assignOlder(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestParam("olderKey") String olderKey
) {
User protector = userService.findByUserName(customUserDetails.getUsername());
userService.assignOlder(protector, olderKey);
User guardian = userService.findByUserName(customUserDetails.getUsername());
userService.assignOlder(guardian, olderKey);

return ApiResponse.onSuccess(SuccessCode.USER_PROTECTOR_CONNECT_OLDER_SUCCESS, true);
return ApiResponse.onSuccess(SuccessCode.USER_GUARDIAN_CONNECT_OLDER_SUCCESS, true);
}

@Operation(summary = "보호자의 노인 등록", description = "보호자가 관리할 노인을 등록하는 메서드입니다.")
@ApiResponses(value = {
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2008", description = "보호자의 노인 등록이 완료되었습니다.")
})
@PostMapping(value = "/protectors/olders")
@PostMapping(value = "/guardians/olders")
public ApiResponse<String> registerOlder(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody UserRequestDto.UserReqDto userReqDto
) throws Exception {
User protector = userService.findByUserName(customUserDetails.getUsername());
User guardian = userService.findByUserName(customUserDetails.getUsername());

User older = userService.createUser(userReqDto);
String key = userService.registerOlder(protector, older);
String key = userService.registerOlder(guardian, older);

return ApiResponse.onSuccess(SuccessCode.USER_PROTECTOR_REGISTER_OLDER_SUCCESS, key);
return ApiResponse.onSuccess(SuccessCode.USER_GUARDIAN_REGISTER_OLDER_SUCCESS, key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.example.silverbridgeX_user.user.domain.User;
import com.example.silverbridgeX_user.user.dto.JwtDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.GuardianMyPageResDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.OlderInfoDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.OlderMyPageResDto;
import com.example.silverbridgeX_user.user.dto.UserResponseDto.ProtectorMyPageResDto;
import java.util.List;

public class UserConverter {
Expand Down Expand Up @@ -37,21 +37,21 @@ public static OlderMyPageResDto olderMyPageResDto(User user) {
.build();
}

public static ProtectorMyPageResDto protectorMyPageResDto(User user, List<User> olders) {
public static GuardianMyPageResDto guardianMyPageResDto(User user, List<User> olders) {
List<OlderInfoDto> olderInfoDtos = olders.stream()
.map(older -> OlderInfoDto.builder()
.nickname(older.getNickname())
.key(older.getUsername())
.build())
.toList();

return ProtectorMyPageResDto.builder()
return GuardianMyPageResDto.builder()
.key(user.getUsername())
.nickname(user.getNickname())
.address(user.getStreetAddress())
.email(user.getEmail())
.olderInfoDtos(olderInfoDtos)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

Expand Down Expand Up @@ -53,10 +52,10 @@ public class User extends BaseEntity {
private UserRole role;

@ManyToOne
@JoinColumn(name = "protector_id")
private User protector;
@JoinColumn(name = "guardian_id")
private User guardian;

@OneToMany(mappedBy = "protector", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "guardian", cascade = CascadeType.ALL)
private List<User> olders = new ArrayList<>();

private String sex;
Expand Down Expand Up @@ -115,8 +114,8 @@ public void updateCoordinate(String longitude, String latitude) {
this.latitude = latitude;
}

public void updateProtector(User protector) {
this.protector = protector; // null 가능
public void updateGuardian(User guardian) {
this.guardian = guardian; // null 가능
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum UserRole {
OLDER,
PROTECTOR
GUARDIAN
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class UserRequestDto {
@AllArgsConstructor
@NoArgsConstructor
public static class UserReqDto {
@Schema(description = "역할(OLDER/PROTECTOR)")
@Schema(description = "역할(OLDER/GUARDIAN)")
private UserRole role;

@Schema(description = "이메일")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public static class OlderMyPageResDto {
private String email;
}

@Schema(description = "ProtectorMyPageResDto")
@Schema(description = "GuardianMyPageResDto")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class ProtectorMyPageResDto {
public static class GuardianMyPageResDto {
@Schema(description = "닉네임")
private String nickname;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ public void deleteUser(String username) {
log.info("DB에서 리프레시 토큰 삭제 완료");
}

if (user.getRole().equals(UserRole.PROTECTOR)) {
if (user.getRole().equals(UserRole.GUARDIAN)) {
for (User older : user.getOlders()) {
older.updateProtector(null); // FK 끊기
older.updateGuardian(null); // FK 끊기
}
}

Expand Down Expand Up @@ -259,31 +259,31 @@ public void validateOlder(User user) {
}
}

public void validateProtector(User user) {
if (!user.getRole().equals(UserRole.PROTECTOR)) {
public void validateGuardian(User user) {
if (!user.getRole().equals(UserRole.GUARDIAN)) {
log.info(String.valueOf(user.getRole()));
throw new GeneralException(ErrorCode.USER_NOT_PROTECTOR);
throw new GeneralException(ErrorCode.USER_NOT_GUARDIAN);
}
}

@Transactional
public void assignOlder(User protector, String key) {
public void assignOlder(User guardian, String key) {
User older = userRepository.findByUsername(key)
.orElseThrow(() -> new GeneralException(ErrorCode.USER_NOT_FOUND_BY_USERNAME));

validateProtector(protector);
validateGuardian(guardian);
validateOlder(older);

older.updateProtector(protector);
older.updateGuardian(guardian);
userRepository.save(older);
}

@Transactional
public String registerOlder(User protector, User older) {
validateProtector(protector);
public String registerOlder(User guardian, User older) {
validateGuardian(guardian);
validateOlder(older);

older.updateProtector(protector);
older.updateGuardian(guardian);
userRepository.save(older);
return older.getUsername();
}
Expand Down