Skip to content

Commit

Permalink
Merge pull request #132 from Soongsil-CoffeeChat/ref/#131
Browse files Browse the repository at this point in the history
[Ref] UserController 응답 양식 통일
  • Loading branch information
KimKyoHwee authored Aug 27, 2024
2 parents 46f51f8 + 82f163c commit e694aa8
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 84 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
id 'org.jetbrains.kotlin.jvm'
}

group = 'com.soongsil'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
}

configurations {
Expand Down Expand Up @@ -85,6 +85,7 @@ dependencies {

//Cool SMS
implementation 'net.nurigo:sdk:4.2.7'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

// Querydsl 빌드 옵션 설정
Expand All @@ -109,5 +110,8 @@ clean {
tasks.named('test') {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}


8 changes: 8 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
pluginManagement {
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.9.23'
}
}
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0'
}
rootProject.name = 'CoffeeChat'
110 changes: 64 additions & 46 deletions src/main/java/com/soongsil/CoffeeChat/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import static com.soongsil.CoffeeChat.enums.RequestUri.*;

import java.net.URI;
import java.util.Map;


import com.soongsil.CoffeeChat.dto.*;
import com.soongsil.CoffeeChat.dto.UserController.MenteeInfoDto;
import com.soongsil.CoffeeChat.dto.UserController.MentorInfoDto;
import com.soongsil.CoffeeChat.dto.UserController.UserInfoDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
Expand All @@ -17,11 +23,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.soongsil.CoffeeChat.controller.handler.ApiResponseGenerator;
import com.soongsil.CoffeeChat.dto.MenteeJoinRequestDto;
import com.soongsil.CoffeeChat.dto.MentorJoinRequestDto;
import com.soongsil.CoffeeChat.dto.Oauth.CustomOAuth2User;
import com.soongsil.CoffeeChat.dto.UserGetUpdateDto;
import com.soongsil.CoffeeChat.dto.UserJoinRequestDto;
import com.soongsil.CoffeeChat.entity.Mentee;
import com.soongsil.CoffeeChat.entity.Mentor;
import com.soongsil.CoffeeChat.entity.User;
Expand Down Expand Up @@ -55,47 +57,57 @@ private String getUserNameByAuthentication(Authentication authentication) throws
@PostMapping()
@Operation(summary = "기본정보 기입")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<User> joinWithMentor(Authentication authentication,
@RequestBody UserJoinRequestDto dto) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return ResponseEntity.status(HttpStatus.CREATED).body(
userService.saveUserInformation(getUserNameByAuthentication(authentication), dto)
);
public ResponseEntity<ApiResponseGenerator<UserInfoDto>> joinWithMentor(Authentication authentication,
@RequestBody UserJoinRequestDto dto) throws Exception {
UserInfoDto userInfoDto=userService.saveUserInformation(getUserNameByAuthentication(authentication), dto);
return ResponseEntity.created(URI.create(USER_URI))
.body(
ApiResponseGenerator.onSuccessCREATED(
userInfoDto
)
);
}

@PostMapping("/mentor")
@Operation(summary = "멘토로 가입하기!")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<Mentor> joinWithMentor(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<MentorInfoDto>> joinWithMentor(Authentication authentication,
@RequestBody MentorJoinRequestDto dto) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return ResponseEntity.status(HttpStatus.CREATED).body(
userService.saveMentorInformation(getUserNameByAuthentication(authentication), dto)
);
MentorInfoDto mentorInfoDto=userService.saveMentorInformation(getUserNameByAuthentication(authentication), dto);
return ResponseEntity.created(URI.create(USER_URI+"/"+"mentor"))
.body(
ApiResponseGenerator.onSuccessCREATED(
mentorInfoDto
)
);
}

@PostMapping("/mentee")
@Operation(summary = "멘티로 가입하기!")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<Mentee> joinWithMentee(Authentication authentication,
@RequestBody MenteeJoinRequestDto dto) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return ResponseEntity.status(HttpStatus.CREATED).body(
userService.saveMenteeInformation(getUserNameByAuthentication(authentication), dto)
);
public ResponseEntity<ApiResponseGenerator<MenteeInfoDto>> joinWithMentee(Authentication authentication,
@RequestBody MenteeJoinRequestDto dto) throws Exception {
MenteeInfoDto menteeInfoDto=userService.saveMenteeInformation(getUserNameByAuthentication(authentication), dto);
return ResponseEntity.created(URI.create(USER_URI+"/"+"mentee"))
.body(
ApiResponseGenerator.onSuccessCREATED(
menteeInfoDto
)
);
}

@PutMapping("/picture")
@Operation(summary = "이미지 저장하기")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<User> saveUserPicture(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<UserInfoDto>> saveUserPicture(Authentication authentication,
@RequestBody String picture) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return ResponseEntity.ok(userService.saveUserPicture(getUserNameByAuthentication(authentication), picture));
UserInfoDto userInfoDto=userService.saveUserPicture(getUserNameByAuthentication(authentication), picture);
return ResponseEntity.created(URI.create(USER_URI+"/"+"picture"))
.body(
ApiResponseGenerator.onSuccessCREATED(
userInfoDto
)
);
}

@GetMapping("/sms")
Expand All @@ -106,43 +118,49 @@ public ResponseEntity<User> saveUserPicture(Authentication authentication,
@ApiResponse(responseCode = "400", description = "인증번호 전송 실패",
content = @Content(mediaType = "application/json", schema = @Schema(example = "{\n\"message\": \"Failed to send verification code\"\n}")))
})
public ResponseEntity<Map<String, String>> getSmsCode(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<Map<String, String>>> getSmsCode(Authentication authentication,
@RequestParam("phoneNum") String phoneNum) {
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
// TODO: Map.of() 함수 사용해서 코드 간결화하면 더 좋을 듯 (EmailController 참조)
return userService.getSmsCode(phoneNum);
return ResponseEntity.ok().body(
ApiResponseGenerator.onSuccessOK(
userService.getSmsCode(phoneNum)
)
);
}

@PatchMapping("/phone")
@Operation(summary = "번호 저장하기")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<User> saveUserPhone(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<UserInfoDto>> saveUserPhone(Authentication authentication,
@RequestParam("phoneNum") String phoneNum) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return userService.saveUserPhone(phoneNum, getUserNameByAuthentication(authentication));
return ResponseEntity.created(URI.create(USER_URI+"/phone")).body(
ApiResponseGenerator.onSuccessOK(
userService.saveUserPhone(phoneNum, getUserNameByAuthentication(authentication))
)
);
}

@PatchMapping("/email")
@Operation(summary = "메일 저장하기")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<User> saveUserEmail(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<UserInfoDto>> saveUserEmail(Authentication authentication,
@RequestParam("email") String email) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return new ResponseEntity<>(userService.saveUserEmail(email, getUserNameByAuthentication(authentication)),
HttpStatus.OK);
return ResponseEntity.created(URI.create(USER_URI+"/email")).body(
ApiResponseGenerator.onSuccessOK(
userService.saveUserEmail(email, getUserNameByAuthentication(authentication))
)
);
}

@PatchMapping()
@Operation(summary = "사용자 정보 수정")
@ApiResponse(responseCode = "200", description = "성공!")
public ResponseEntity<User> saveUserEmail(Authentication authentication,
public ResponseEntity<ApiResponseGenerator<UserInfoDto>> saveUserEmail(Authentication authentication,
@RequestBody UserGetUpdateDto dto) throws Exception {
// TODO: 엔티티 반환 X, DTO 만들어서 반환하기
// TODO: ApiResponseGenerator 잘 사용하기 (statusCode 200, 201은 많이 써서 함수로 아예 만들어놨음)
return new ResponseEntity<>(userService.changeUserInfo(dto, getUserNameByAuthentication(authentication)),
HttpStatus.OK);
return ResponseEntity.created(URI.create(USER_URI+"/email")).body(
ApiResponseGenerator.onSuccessOK(
userService.changeUserInfo(dto, getUserNameByAuthentication(authentication))
)
);
}

@GetMapping()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.soongsil.CoffeeChat.controller.exception.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;

@Getter
@RequiredArgsConstructor
public enum UserErrorCode {
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER_404", "해당 USER의 엔티티가 존재하지 않습니다."),
USER_SMS_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "USER_500", "SMS 전송에 실패했습니다.");

private final HttpStatusCode httpStatusCode;
private final String errorCode;
private final String errorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.soongsil.CoffeeChat.dto.UserController;

import com.soongsil.CoffeeChat.entity.Mentee;
import com.soongsil.CoffeeChat.enums.PartEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MenteeInfoDto {
private PartEnum part;

public static MenteeInfoDto toDto(Mentee mentee){
return MenteeInfoDto.builder()
.part(mentee.getPart())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.soongsil.CoffeeChat.dto.UserController;

import com.soongsil.CoffeeChat.entity.Introduction;
import com.soongsil.CoffeeChat.entity.Mentor;
import com.soongsil.CoffeeChat.enums.ClubEnum;
import com.soongsil.CoffeeChat.enums.PartEnum;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MentorInfoDto {
private PartEnum part;
private ClubEnum club;
private Long introductionId;

public static MentorInfoDto toDto(Mentor mentor){
return MentorInfoDto.builder()
.part(mentor.getPart())
.club(mentor.getClub())
.introductionId(mentor.getIntroduction().getId())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.soongsil.CoffeeChat.dto.UserController;


import com.soongsil.CoffeeChat.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;

@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserInfoDto {
private String username;
private String name;
private String email;
private String role;
private String phoneNum; //전화번호
private String picture;

public static UserInfoDto toDto(User user){
return UserInfoDto.builder().
username(user.getUsername()).
name(user.getName()).
email(user.getEmail()).
role(user.getRole()).
phoneNum(user.getPhoneNum()).
picture(user.getPicture())
.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.soongsil.CoffeeChat.entity.Mentor;
import com.soongsil.CoffeeChat.entity.User;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
User findByUsername(String username);
Optional<User> findByUsername(String username);

User findByMentor(Mentor mentor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.soongsil.CoffeeChat.controller.exception.enums.ApplicationErrorCode.*;
import static com.soongsil.CoffeeChat.controller.exception.enums.MentorErrorCode.*;
import static com.soongsil.CoffeeChat.controller.exception.enums.PossibleDateErrorCode.*;
import static com.soongsil.CoffeeChat.controller.exception.enums.UserErrorCode.USER_NOT_FOUND;
import static com.soongsil.CoffeeChat.enums.ApplicationStatus.*;

import java.time.LocalDate;
Expand Down Expand Up @@ -64,6 +65,14 @@ public class ApplicationService {
@Autowired
private RedisTemplate<String, String> redisTemplate;

private User findUserByUsername(String username){
return userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(
USER_NOT_FOUND.getHttpStatusCode(),
USER_NOT_FOUND.getErrorMessage())
);
}

@Transactional
public ApplicationCreateResponseDto createApplication(ApplicationCreateRequestDto request, String userName) throws
Exception {
Expand Down Expand Up @@ -150,7 +159,8 @@ public ApplicationCreateResponseDto createApplication(ApplicationCreateRequestDt
);

// COGO 저장
Mentee findMentee = userRepository.findByUsername(userName).getMentee();
User user = findUserByUsername(userName);
Mentee findMentee = user.getMentee();
Mentor findMentor = mentorRepository.findById(request.getMentorId())
.orElseThrow(() -> new CustomException(
MENTOR_NOT_FOUND.getHttpStatusCode(),
Expand Down Expand Up @@ -240,7 +250,8 @@ public List<ApplicationGetResponseDto> getApplications(String username, String a

//TODO: JOIN문으로 변경
List<ApplicationGetResponseDto> dtos = new ArrayList<>();
Mentor findMentor = userRepository.findByUsername(username).getMentor();
User user = findUserByUsername(username);
Mentor findMentor = user.getMentor();
List<Application> findApplications = applicationRepository.findApplicationByMentor(findMentor);

for (Application app : findApplications) {
Expand Down
Loading

0 comments on commit e694aa8

Please sign in to comment.