Skip to content

Commit

Permalink
Merge pull request #136 from UMC-CommonPlant/main
Browse files Browse the repository at this point in the history
[Build] user, plant
  • Loading branch information
sonshn authored Jul 5, 2024
2 parents 903a262 + 2c4d709 commit d9b5a77
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class OAuthController implements OAuthSwagger{

@GetMapping("/login/{provider}")
public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String accessToken, @PathVariable String provider){
log.info("[API] Social Login");
log.info("accessToken : " + accessToken);
String token = oAuthService.oAuthLogin(accessToken, provider);
String email = oAuthService.kakaoLogin(accessToken);
Expand All @@ -34,6 +35,7 @@ public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String ac

@GetMapping("/api/token")
public ResponseEntity<JsonResponse> validToken(){
log.info("[API] Validate Token");
String token = jwtService.getJwt();
boolean isValid = jwtService.validateToken(token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public class PlantController implements PlantSwagger {

/**
* [POST] 식물 추가
* [POST] /plant/add
* @return 추가한 식물의 애칭
*/
@PostMapping(value = "/plant/add", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.createPlantReq createPlantReq,
@RequestPart("image") MultipartFile file) {
log.info("[API] createPlant - 식물 추가");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -48,6 +49,8 @@ public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.c
*/
@GetMapping("/plant/{plantIdx}")
public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {
log.info("[API]: getPlantCard - 식물 조회");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -63,6 +66,8 @@ public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {
*/
@GetMapping("/user/plantList")
public ResponseEntity<JsonResponse> getPlantList() {
log.info("[API]: getPlantList - 같은 사람이 키우는 식물 리스트 조회");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand Down Expand Up @@ -96,6 +101,8 @@ public ResponseEntity<JsonResponse> getPlantList() {
*/
@PutMapping("/plant/update/wateredDate/{plantIdx}")
public ResponseEntity<JsonResponse> updateWateredDate(@PathVariable Long plantIdx){
log.info("[API]: updateWateredDate - 식물의 D-Day 업데이트");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -105,11 +112,13 @@ public ResponseEntity<JsonResponse> updateWateredDate(@PathVariable Long plantId
}

/**
* [GET] /plant/update
* [GET] 식물을 수정할 때 뜨는 조회 화면
* @return 수정하기 전 식물의 정보
*/
@GetMapping("/plant/update/{plantIdx}")
public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx){
log.info("[API]: getUpdatedPlant - 식물을 수정할 때 뜨는 조회 화면");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -119,13 +128,15 @@ public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx)
}

/**
* [PUT] /plant/update
* [PUT] 식물 수정
* @return 수정한 식물의 정보
*/
@PutMapping(value = "/plant/update/{plantIdx}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<JsonResponse> updatePlant(@PathVariable Long plantIdx,
@RequestPart("plant") PlantDto.updatePlantReq updatePlantReq,
@RequestPart("image") MultipartFile file){
log.info("[API] updatePlant - 식물 수정");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -135,12 +146,14 @@ public ResponseEntity<JsonResponse> updatePlant(@PathVariable Long plantIdx,
}

/**
* [DELETE] /plant/delete
* [DELETE] 식물 삭제
* @param plantIdx
* @return 삭제한 식물의 닉네임
*/
@DeleteMapping("/plant/delete/{plantIdx}")
public ResponseEntity<JsonResponse> deletePlant(@PathVariable Long plantIdx){
log.info("[API] deletePlant - 식물 삭제");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ResponseEntity<JsonResponse> updatePlant (
) @PathVariable Long plantIdx,
@Parameter(
name = "nickname",
description = "식물의 새로운 닉네임",
description = "식물의 새로운 닉네임과 물 주기 기간 값",
required = true
) @RequestPart("plant") PlantDto.updatePlantReq updatePlantReq,
@Parameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static class getPlantRes{
private String scientificName;
@Schema(description = "식물의 물주는 주기 (Information 에 저장된 권장 주기)", example = "10")
private Long waterDay;
@Schema(description = "관리 팁", example = "물을 좋아하나 과습에 주의하세요!")
private String tip;
@Schema(description = "빛의 강도", example = "밝은 곳을 좋아해요!")
private String sunlight;
@Schema(description = "최저 온도", example = "16")
Expand Down Expand Up @@ -147,8 +149,12 @@ public getMyGardenPlantListRes(Plant plant, Long remainderDate, String recentMem
@NoArgsConstructor
@Data
public static class updatePlantReq{
@Schema(description = "수정하는 식물의 인덱스 값")
private Long plantIdx;
@Schema(description = "식물의 새로운 닉네임", example = "머니볼")
private String nickname;
@Schema(description = "식물의 새로운 물주기 기간 값. 입력은 문자열로 받음.", example = "15")
private String waterCycle;
}

/**
Expand All @@ -160,6 +166,7 @@ public static class updatePlantReq{
public static class updatePlantRes{
private Long plantIdx;
private String nickname;
private int waterCycle;
private String imgUrl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public Plant(Place place, String plantName, String nickname, int waterCycle, Str
/**
* 식물 수정 API 관련 메소드
*/
public void updatePlant(String imgUrl, String nickname) {
public void updatePlant(String imgUrl, String nickname, int waterCycle) {
this.imgUrl = imgUrl;
this.nickname = nickname;
this.waterCycle = waterCycle;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public String createPlant(User user, PlantDto.createPlantReq req, MultipartFile
waterCycle = req.getWaterCycle();

if(waterCycle.charAt(0) == 0) {
throw new BadRequestException(ErrorResponseStatus.REGEX_VALIDATION_ERROR);
throw new BadRequestException(ErrorResponseStatus.PLANT_WATERED_DATE_REGEX_VALIDATION_ERROR);
} else if(req.getWaterCycle().matches("^[1-9]\\d*$")){
castedWaterCycle = Integer.parseInt(waterCycle);
} else {
throw new BadRequestException(ErrorResponseStatus.REGEX_VALIDATION_ERROR);
throw new BadRequestException(ErrorResponseStatus.PLANT_WATERED_DATE_REGEX_VALIDATION_ERROR);
}
}

Expand Down Expand Up @@ -154,6 +154,12 @@ public PlantDto.getPlantRes getPlant(User user, Long plantIdx) {

// log.info("getMemoList: " + getAllMemoList);

String tip = null;

if(!infoResponse.get(0).getTip().isEmpty()) {
tip = infoResponse.get(0).getTip();
}

PlantDto.getPlantRes getPlantRes = new PlantDto.getPlantRes(
plant.getPlantIdx(),
plant.getPlantName(),
Expand All @@ -165,6 +171,7 @@ public PlantDto.getPlantRes getPlant(User user, Long plantIdx) {
getAllMemoList,
infoResponse.get(0).getScientificName(),
infoResponse.get(0).getWater_day(),
tip,
infoResponse.get(0).getSunlight(),
infoResponse.get(0).getTemp_min(),
infoResponse.get(0).getTemp_max(),
Expand Down Expand Up @@ -361,21 +368,42 @@ public PlantDto.updatePlantRes updatePlant(User user, Long plantIdx, PlantDto.up
throw new BadRequestException(ErrorResponseStatus.LONG_PLANT_NICKNAME);
}

// TODO: 식물의 물 주기 기간
String waterCycle = null;
int castedWaterCycle = 0;

if(String.valueOf(req.getWaterCycle()).isEmpty()){
throw new BadRequestException(ErrorResponseStatus.EMPTY_PLANT_WATERED_DATE);
} else {
waterCycle = req.getWaterCycle();

if(waterCycle.charAt(0) == 0) {
throw new BadRequestException(ErrorResponseStatus.PLANT_WATERED_DATE_REGEX_VALIDATION_ERROR);
} else if(req.getWaterCycle().matches("^[1-9]\\d*$")){
castedWaterCycle = Integer.parseInt(waterCycle);
} else {
throw new BadRequestException(ErrorResponseStatus.PLANT_WATERED_DATE_REGEX_VALIDATION_ERROR);
}
}

// TODO: imgUrl
String imgUrl = null;

if (plantImage.getSize() > 0) {
imgUrl = plant.getImgUrl();
imageService.deleteFileInS3(plant.getImgUrl());

imgUrl = imageService.saveImage(plantImage);
} else {
throw new BadRequestException(ErrorResponseStatus.NO_SELECTED_PLANT_IMAGE);
}

plant.updatePlant(imgUrl, plantNickname);
plant.updatePlant(imgUrl, plantNickname, castedWaterCycle);
plantRepository.save(plant);

return new PlantDto.updatePlantRes(
plant.getPlantIdx(),
plantNickname,
castedWaterCycle,
imgUrl
);
}
Expand All @@ -399,6 +427,7 @@ public PlantDto.updatePlantRes getUpdatedPlant(User user, Long plantIdx) {
return new PlantDto.updatePlantRes(
plant.getPlantIdx(),
plant.getNickname(),
plant.getWaterCycle(),
plant.getImgUrl()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ public class UserController implements UserSwagger{

@PostMapping(value = "/user", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) //join
public ResponseEntity<JsonResponse> join(@RequestPart("user") UserDto.join req, @RequestParam(value ="image", required = false)MultipartFile image){
log.info("join");
log.info("[API] Join User");
String token = userService.joinUser(req, image);

return ResponseEntity.ok(new JsonResponse(true, 200, "join", token));
}
@GetMapping("/user/{name}") // 회원정보 조회
public ResponseEntity<JsonResponse> getUserByName(@PathVariable String name){
log.info("[API] Get User Info");
User user = userService.getUserByName(name);

return ResponseEntity.ok(new JsonResponse(true, 200, "getUser", user));
}
@GetMapping("/user/{name}/exists")
public ResponseEntity<JsonResponse> checkNameDuplicate(@PathVariable String name){
log.info("[API] Check Duplicated Name");
boolean checkName = userService.checkNameDuplication(name);

return ResponseEntity.ok(new JsonResponse(true, 200, "사용가능한 이름입니다.", checkName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public enum ErrorResponseStatus {
NO_PLANT_NICKNAME(false, 4201, "식물의 애칭을 입력해 주세요!"),
LONG_PLANT_NICKNAME(false, 4202, "식물의 애칭은 10자 이하로 설정해주세요!"),
NO_SELECTED_PLANT_IMAGE(false,4203, "식물의 이미지를 선택해주세요!"),
REGEX_VALIDATION_ERROR(false, 4204, "정확한 물 주기 값을 입력해주세요!"),

EMPTY_PLANT_WATERED_DATE(false, 4204, "식물의 새로운 물 주기 기간을 입력해주세요!"),
PLANT_WATERED_DATE_REGEX_VALIDATION_ERROR(false, 4205, "정확한 물 주기 기간 값을 입력해주세요!"),

// 4300 : Memo
PLANT_NOT_FOUND(false, 4300, "등록되지 않은 식물입니다."),
Expand Down

0 comments on commit d9b5a77

Please sign in to comment.