Skip to content

Commit 05109a3

Browse files
committed
[Feat] 사용자 포인트 차감 API 구현
사용자 포인트 차감 API 구현 일단 memberId를 입력받는 방식으로 구현했으나 추후 로그인 정보 조회 방식으로 수정할 예정
1 parent bd8af5b commit 05109a3

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/main/java/ttakkeun/ttakkeun_server/controller/DiagnoseController.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package ttakkeun.ttakkeun_server.controller;
22

3+
import feign.Response;
34
import lombok.RequiredArgsConstructor;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.http.HttpStatus;
67
import org.springframework.http.ResponseEntity;
7-
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.RequestMapping;
9-
import org.springframework.web.bind.annotation.RequestParam;
10-
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.bind.annotation.*;
119
import ttakkeun.ttakkeun_server.apiPayLoad.ApiResponse;
1210
import ttakkeun.ttakkeun_server.apiPayLoad.code.status.ErrorStatus;
1311
import ttakkeun.ttakkeun_server.apiPayLoad.code.status.SuccessStatus;
1412
import ttakkeun.ttakkeun_server.dto.GetMyPointResponseDTO;
13+
import ttakkeun.ttakkeun_server.dto.UpdateMyPointResponseDTO;
1514
import ttakkeun.ttakkeun_server.service.DiagnoseService;
1615

1716
import java.util.NoSuchElementException;
@@ -26,7 +25,7 @@ public class DiagnoseController {
2625
private DiagnoseService diagnoseService;
2726

2827
// memberId 임의로 입력받아서 조회하는 방식으로 구현함
29-
// 추후 액세스 토큰으로 멤버 아이디 받아와서 조회하는 방식으로 수정 예정
28+
// 추후 로그인 정보로 멤버 아이디 받아와서 조회하는 방식으로 수정 예정
3029
@GetMapping("/point")
3130
public ResponseEntity<ApiResponse<GetMyPointResponseDTO>> getPointsByMember(@RequestParam("member-id") Long memberId) {
3231
try {
@@ -35,10 +34,25 @@ public ResponseEntity<ApiResponse<GetMyPointResponseDTO>> getPointsByMember(@Req
3534
return ResponseEntity.ok(response);
3635
} catch (NoSuchElementException e) {
3736
ApiResponse<GetMyPointResponseDTO> response = ApiResponse.ofFailure(ErrorStatus.MEMBER_NOT_FOUND, null);
38-
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
37+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
3938
} catch (Exception e) {
4039
ApiResponse<GetMyPointResponseDTO> response = ApiResponse.ofFailure(ErrorStatus._INTERNAL_SERVER_ERROR, null);
4140
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
4241
}
4342
}
43+
44+
@PatchMapping("/loading")
45+
public ResponseEntity<ApiResponse<UpdateMyPointResponseDTO>> updatePointsByMember(@RequestParam("member-id") Long memberId) {
46+
try {
47+
Integer point = diagnoseService.updatePointsByMember(memberId);
48+
ApiResponse<UpdateMyPointResponseDTO> response = ApiResponse.of(SuccessStatus._OK, new UpdateMyPointResponseDTO(point));
49+
return ResponseEntity.ok(response);
50+
} catch (NoSuchElementException e) {
51+
ApiResponse<UpdateMyPointResponseDTO> response = ApiResponse.ofFailure(ErrorStatus.MEMBER_NOT_FOUND, null);
52+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
53+
} catch (Exception e) {
54+
ApiResponse<UpdateMyPointResponseDTO> response = ApiResponse.ofFailure(ErrorStatus._INTERNAL_SERVER_ERROR, null);
55+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
56+
}
57+
}
4458
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ttakkeun.ttakkeun_server.dto;
2+
3+
public record UpdateMyPointResponseDTO(Integer point) {
4+
}

src/main/java/ttakkeun/ttakkeun_server/service/DiagnoseService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ private boolean isToday(LocalDateTime dateTime) {
5151
return today.equals(date);
5252
// 입력받은 값이 오늘 날짜와 같다면 true 반환, 오늘 날짜와 다르다면 false 반환
5353
}
54+
55+
public Integer updatePointsByMember(Long memberId) throws Exception {
56+
Optional<Point> pointOpt = pointRepository.findByMemberId(memberId);
57+
58+
if (pointOpt.isPresent()) {
59+
Point point = pointOpt.get(); // Optinal 객체에서 point를 가져옴
60+
Integer points = point.getPoints();
61+
62+
// 진단시 포인트가 1점 차감됨
63+
point.setPoints(points-1);
64+
point.setUpdatedAt(LocalDateTime.now());
65+
pointRepository.save(point);
66+
return point.getPoints();
67+
} else {
68+
// Optional 객체에서 값이 비어있는 경우 예외를 던짐
69+
// 0 반환에서 오류 발생하도록 수정함
70+
throw new NoSuchElementException("Member with ID " + memberId + " not found");
71+
}
72+
}
5473
}

src/main/resources/application.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ spring:
22
main:
33
allow-bean-definition-overriding: true
44
datasource:
5-
# url: ${db.url}
6-
# username: ${db.username}
7-
# password: ${db.password}
8-
#
9-
url: jdbc:mysql://localhost:3306/ttakkeun
10-
username: root
11-
password: sallyjane
5+
url: ${db.url}
6+
username: ${db.username}
7+
password: ${db.password}
128
driver-class-name: com.mysql.cj.jdbc.Driver
139
sql:
1410
init:

0 commit comments

Comments
 (0)