Skip to content

Commit

Permalink
fix: 챌린지 관련 api 전체적으로 수정 및 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-j0y committed Jun 29, 2023
1 parent 2ce4eda commit 7cdbc0c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -35,4 +32,5 @@ public class ChallengeStatus {
@NotNull
@Column(nullable = false)
private int progress = 0;

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Getter
@Builder
public class ChallengeStatusChangeResponse {
public class ApiResponse {

Integer status;
String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,14 @@ public List<ChallengeStatus> 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);
}
Expand Down
53 changes: 40 additions & 13 deletions src/main/java/com/whyranoid/walkie/service/ChallengeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,10 +30,33 @@ public List<ChallengeStatus> 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);
Expand All @@ -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();
// }

}
}

0 comments on commit 7cdbc0c

Please sign in to comment.