Skip to content

Commit

Permalink
[feat] #21 - 유저 상세 조회 API 생성 완료
Browse files Browse the repository at this point in the history
- postman 확인 완료
  • Loading branch information
abi-hong committed Sep 12, 2022
1 parent 2db76bf commit 5cc701d
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public enum SuccessCode {
UPDATE_SUNPLANTLOG_SUCCESS(OK, "광합성 로그 등록하기를 성공했습니다."),
UPDATE_REPOTPLANTLOG_SUCCESS(OK, "분갈이 로그 등록하기를 성공했습니다."),
UPDATE_WATERPLANTLOG_SUCCESS(OK, "물주기 로그 등록하기를 성공했습니다."),
UPDATE_LOOKPLANTLOG_SUCCESS(OK, "관찰 로그 등록하기를 성공했습니다.");
UPDATE_LOOKPLANTLOG_SUCCESS(OK, "관찰 로그 등록하기를 성공했습니다."),

USER_INFO_SUCCESS(OK, "유저 프로필 상세 조회를 성공했습니다.");

private final HttpStatus status;
private final String msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public ResponseEntity<MyPlantUpdateResponse> putLook(@PathVariable Long userId,
return MyPlantUpdateResponse.newResponse(UPDATE_LOOKPLANTLOG_SUCCESS);
}

// 오류 해결 - https://velog.io/@youns1121/JPA-java.lang.IllegalStateException-Cannot-call-sendError-after-the-response-has-been-committed
@GetMapping("/plant/{userId}")
public List<MyPlant> check(@PathVariable Long userId) {
usersRepository.findByUserId(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import com.plantity.server.config.BaseResponse2;
import com.plantity.server.domain.users.Users;
import com.plantity.server.domain.users.UsersRequestDto;
import com.plantity.server.domain.users.UsersResponseDto;
import com.plantity.server.dto.res.users.UserResponse;
import com.plantity.server.repository.UsersRepository;
import com.plantity.server.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.plantity.server.constants.SuccessCode.USER_INFO_SUCCESS;

@RestController
@RequiredArgsConstructor
public class UsersController {
Expand All @@ -25,6 +27,17 @@ public List<Users> getUsers(){
return userRepository.findAll();
}

// user 상세 정보 조회
@GetMapping("/users/{userId}")
public ResponseEntity<UserResponse> getUser(@PathVariable Long userId) {

UsersRequestDto usersRequestDto = UsersRequestDto.of(userId);

UsersResponseDto usersResponseDto = userService.userInfo(usersRequestDto);

return UserResponse.newResponse(USER_INFO_SUCCESS, usersResponseDto);
}

// 임시로 User 추가하기
@PostMapping("/save/test/users")
public BaseResponse2<Long> postTestUsers(@RequestBody UsersRequestDto usersRequestDto){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.plantity.server.domain.myPlant;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.plantity.server.domain.BaseTimeEntity;
import com.plantity.server.domain.plantlog.PlantLog;
import com.plantity.server.domain.users.Users;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.plantity.server.domain.users;


import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.plantity.server.domain.BaseTimeEntity;
import com.plantity.server.domain.myPlant.MyPlant;
import com.plantity.server.domain.plantFollowing.PlantFollowing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
@Setter
@RequiredArgsConstructor
public class UsersRequestDto {

private Long userId;
private String nickName;
//private String social;
private String rating;
private int score;
//private String status;

public UsersRequestDto(String nickName, String social, String rating, int score, String status){
this.nickName = nickName;
//this.social = social;
this.rating = rating;
this.score = score;
//this.status = status;
private UsersRequestDto(Long userId){
this.userId = userId;
//this.nickName = nickName;
//this.rating = rating;
//this.score = score;
}

public static UsersRequestDto of(Long userId) {
return new UsersRequestDto(userId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.plantity.server.domain.users;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class UsersResponseDto {

private String nickName;
private String rating;
private int score;

private UsersResponseDto(Users users) {
this.nickName = users.getNickName();
this.rating = users.getRating();
this.score = users.getScore();
}

public static UsersResponseDto from(Users users) {
return new UsersResponseDto(users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.plantity.server.dto.res.users;

import com.plantity.server.constants.SuccessCode;
import com.plantity.server.domain.users.UsersResponseDto;
import com.plantity.server.dto.BaseResponse;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.ResponseEntity;

@Getter
@NoArgsConstructor
public class UserResponse extends BaseResponse {

private UsersResponseDto data;

private UserResponse(Boolean success, String msg, UsersResponseDto data) {
super(success, msg);
this.data = data;
}

public static ResponseEntity<UserResponse> newResponse(SuccessCode code, UsersResponseDto data) {
UserResponse response = new UserResponse(true, code.getMsg(), data);
return new ResponseEntity(response, code.getStatus());
}
}
10 changes: 10 additions & 0 deletions server/src/main/java/com/plantity/server/service/UsersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.plantity.server.domain.users.Users;
import com.plantity.server.domain.users.UsersRequestDto;
import com.plantity.server.domain.users.UsersResponseDto;
import com.plantity.server.repository.UsersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -24,4 +25,13 @@ public Long updateUser(Long id, UsersRequestDto userRequestDto){
}
*/

public UsersResponseDto userInfo(UsersRequestDto requestDto) {
Users users = userRepository.findById(requestDto.getUserId()).orElseThrow(
() -> new IllegalArgumentException("해당 유저가 없습니다.")
);

return UsersResponseDto.from(users);

}
}

0 comments on commit 5cc701d

Please sign in to comment.