From 7cdbc0c1120628aa96e2c4ccf0938cbe74ad9b6a Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Thu, 29 Jun 2023 18:29:28 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B1=8C=EB=A6=B0=EC=A7=80=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20api=20=EC=A0=84=EC=B2=B4=EC=A0=81=EC=9C=BC=EB=A1=9C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../controller/ChallengeController.java | 7 +++ .../walkie/domain/ChallengeStatus.java | 10 ++-- .../request/ChallengeStatusCreateRequest.java | 24 ++++++++- .../walkie/dto/response/ApiResponse.java | 2 +- .../repository/ChallengeRepository.java | 9 ++++ .../walkie/service/ChallengeService.java | 53 ++++++++++++++----- 7 files changed, 85 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 44939a5..f5b2e38 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'mysql:mysql-connector-java' runtimeOnly 'com.h2database:h2' - compileOnly 'org.projectlombok:lombok' + implementation 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' implementation 'org.springframework.boot:spring-boot-starter' diff --git a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java index d471aea..fb13de6 100644 --- a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java +++ b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java @@ -3,6 +3,7 @@ import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.domain.ChallengeStatus; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; +import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; import com.whyranoid.walkie.service.ChallengeService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; @@ -37,4 +38,10 @@ public ResponseEntity getChallengeDetail(@RequestParam("challengeId") Long chall public ResponseEntity updateChallengeStatus(@RequestBody ChallengeStatusChangeRequest challengeStatusChangeRequest) { return ResponseEntity.ok(challengeService.updateChallengeStatus(challengeStatusChangeRequest)); } + + // 챌린지를 새로 시작하는 것으로 ChallengeStatus를 새로 만들어서 추가해야 함. + @PostMapping("/challenge-detail/start") + public ResponseEntity startChallenge(@RequestBody ChallengeStatusCreateRequest challengeStatusCreateRequest) { + return ResponseEntity.ok(challengeService.createChallengeStatus(challengeStatusCreateRequest)); + } } \ No newline at end of file diff --git a/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java b/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java index f83d220..7b0197d 100644 --- a/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java +++ b/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java @@ -1,9 +1,6 @@ package com.whyranoid.walkie.domain; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import javax.persistence.*; import javax.validation.constraints.NotNull; @@ -20,11 +17,11 @@ public class ChallengeStatus { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long statusId; - @ManyToOne(optional = false, fetch = FetchType.EAGER) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) private Walkie walkie; - @ManyToOne(optional = false, fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "challenge_id", nullable = false) private Challenge challenge; @@ -35,4 +32,5 @@ public class ChallengeStatus { @NotNull @Column(nullable = false) private int progress = 0; + } diff --git a/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java index c6e1685..bc319b6 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java @@ -1,2 +1,24 @@ -package com.whyranoid.walkie.dto.request;public class ChallengeStatusCreateRequest { +package com.whyranoid.walkie.dto.request; + +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotNull; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +public class ChallengeStatusCreateRequest { + @NotNull + private Long userId; + + @NotNull + private Long challengeId; + + @Builder + public ChallengeStatusCreateRequest(Long userId, Long challengeId) { + this.userId = userId; + this.challengeId = challengeId; + } } diff --git a/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java b/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java index 2d1e935..01f9d5b 100644 --- a/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java +++ b/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java @@ -5,7 +5,7 @@ @Getter @Builder -public class ChallengeStatusChangeResponse { +public class ApiResponse { Integer status; String message; diff --git a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java index 6698135..fe9ebad 100644 --- a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java @@ -2,6 +2,7 @@ import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.domain.ChallengeStatus; +import com.whyranoid.walkie.domain.Walkie; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -33,6 +34,14 @@ public List getDetailChallenge(Long challengeId, Long userId) { .getResultList(); } + public Challenge getChallengeById(Long challengeId) { + return em.find(Challenge.class, challengeId); + } + + public Walkie getWalkieById(Long userId) { + return em.find(Walkie.class, userId); + } + public void insertChallengeStatus(ChallengeStatus cs) { em.persist(cs); } diff --git a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java index ac68817..135993d 100644 --- a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java +++ b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java @@ -2,15 +2,19 @@ import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.domain.ChallengeStatus; +import com.whyranoid.walkie.domain.Walkie; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; -import com.whyranoid.walkie.dto.response.ChallengeStatusChangeResponse; +import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; +import com.whyranoid.walkie.dto.response.ApiResponse; import com.whyranoid.walkie.repository.ChallengeRepository; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +@Slf4j @Service @Transactional @RequiredArgsConstructor @@ -26,10 +30,33 @@ public List getChallengeDetail(Long challengeId, Long userId) { return challengeRepository.getDetailChallenge(challengeId, userId); } - public ChallengeStatusChangeResponse updateChallengeStatus(ChallengeStatusChangeRequest challengeStatusChangeRequest) { - Character requestStatus = challengeStatusChangeRequest.getStatus(); - Long requestStatusId = challengeStatusChangeRequest.getStatusId(); - try { + public ApiResponse createChallengeStatus(ChallengeStatusCreateRequest challengeStatusCreateRequest) { + + // walkie와 challenge는 클라이언트에서 보내줌, 근데 나는 userId랑 challengeId만 받고 싶은데..? + Long userId = challengeStatusCreateRequest.getUserId(); + Long challengeId = challengeStatusCreateRequest.getChallengeId(); + + Challenge challenge = challengeRepository.getChallengeById(challengeId); + Walkie walkie = challengeRepository.getWalkieById(userId); + + ChallengeStatus cs = new ChallengeStatus(); + cs.setWalkie(walkie); + cs.setChallenge(challenge); + cs.setStatus('P'); + + challengeRepository.insertChallengeStatus(cs); + + return ApiResponse.builder() + .status(200) + .message("챌린지가 시작되었습니다.") + .build(); + + } + + public ApiResponse updateChallengeStatus(ChallengeStatusChangeRequest challengeStatusChangeRequest) { +// try { + Character requestStatus = challengeStatusChangeRequest.getStatus(); + Long requestStatusId = challengeStatusChangeRequest.getStatusId(); // 챌린지 중도 포기해서 삭제하려는 경우 if(requestStatus == 'N') { challengeRepository.deleteChallengeStatus(requestStatusId); @@ -39,17 +66,17 @@ else if(requestStatus == 'C') { challengeRepository.updateChallengeStatus(requestStatusId, requestStatus); } - challengeRepository.updateChallengeStatus(requestStatusId, requestStatus); - return ChallengeStatusChangeResponse.builder() + return ApiResponse.builder() .status(200) .message("성공") .build(); - } catch(Exception e) { - return ChallengeStatusChangeResponse.builder() - .status(400) - .message("실패") - .build(); - } +// } +// catch(Exception e) { +// return ApiResponse.builder() +// .status(400) +// .message("실패") +// .build(); +// } } }