From 81022951ffc239398554edfd306bf710299aba87 Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Sat, 1 Jul 2023 19:04:05 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EB=B3=84=20=EC=B1=8C=EB=A6=B0=EC=A7=80=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=98=A4=EA=B8=B0,=20=EC=83=88=EB=A1=9C=EC=9A=B4=20=EC=B1=8C?= =?UTF-8?q?=EB=A6=B0=EC=A7=80=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20api?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../walkie/controller/ChallengeController.java | 13 +++++++++---- .../java/com/whyranoid/walkie/domain/Challenge.java | 3 +++ .../walkie/repository/ChallengeRepository.java | 10 ++++++++-- .../whyranoid/walkie/service/ChallengeService.java | 3 +++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java index b4a30b5..9414597 100644 --- a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java +++ b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java @@ -21,10 +21,15 @@ public class ChallengeController { private final ChallengeService challengeService; @GetMapping("/challenges/new") - public ResponseEntity getChallengesByCategory(@RequestParam("userId") Long userId) { - // 본인 정보를 가져오는 로직이 필요 - List newChallenges = challengeService.getChallengesByCategory(userId, 'L'); - return new ResponseEntity<>(newChallenges, httpHeaders, HttpStatus.OK); + public ResponseEntity getNewChallenges(@RequestParam("userId") Long userId) { + List challenges = challengeService.getNewChallenges(userId); + return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); + } + + @GetMapping("/challenges/category") + public ResponseEntity getChallengesByCategory(@RequestParam("userId") Long userId, @RequestParam("category") char category) { + List challenges = challengeService.getChallengesByCategory(userId, category); + return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); } @GetMapping("/challenges/progress") diff --git a/src/main/java/com/whyranoid/walkie/domain/Challenge.java b/src/main/java/com/whyranoid/walkie/domain/Challenge.java index b5b0c23..79d0403 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Challenge.java +++ b/src/main/java/com/whyranoid/walkie/domain/Challenge.java @@ -44,4 +44,7 @@ public class Challenge { @Column(name = "calorie") private int calorie; + + @Column(name = "new_flag") + private int newFlag; } \ No newline at end of file diff --git a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java index 0419035..7f8b8da 100644 --- a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java @@ -19,15 +19,21 @@ public class ChallengeRepository { @PersistenceContext private final EntityManager em; + public List getNewChallenges(Long userId) { + return em.createQuery("select c.name, c.challengeId from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.newFlag = 1") + .setParameter("userId", userId) + .getResultList(); + } + public List getChallengesByCategory(Long userId, char category) { - return em.createQuery("select c1 from Challenge c1 where category = :category except select c2 from Challenge c2 left join ChallengeStatus cs on c2.challengeId = cs.challenge.challengeId and cs.walkie.userId = :userId") + return em.createQuery("select c.name, c.challengeId from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.category = :category") .setParameter("userId", userId) .setParameter("category", category) .getResultList(); } public List getProgressChallenges(Long userId) { - return em.createQuery("select c.name, cs.progress from ChallengeStatus cs left join Challenge c on cs.challenge.challengeId = c.challengeId where cs.walkie.userId = :userId") + return em.createQuery("select c.name, cs.progress, c.challengeId from ChallengeStatus cs left join Challenge c on cs.challenge.challengeId = c.challengeId where cs.walkie.userId = :userId") .setParameter("userId", userId) .getResultList(); } diff --git a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java index 16d9d04..388180c 100644 --- a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java +++ b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java @@ -22,6 +22,9 @@ public class ChallengeService{ private final ChallengeRepository challengeRepository; + public List getNewChallenges(Long userId) { + return challengeRepository.getNewChallenges(userId); + } public List getChallengesByCategory(Long userId, char category) { return challengeRepository.getChallengesByCategory(userId, category); } From 78c720fe9baedc9d82255c1d590b45850273591c Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Sat, 1 Jul 2023 22:39:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20=EC=9D=B8=EA=B8=B0=20=EC=B1=8C?= =?UTF-8?q?=EB=A6=B0=EC=A7=80=20=EB=AA=A9=EB=A1=9D=20=EA=B0=80=EC=A0=B8?= =?UTF-8?q?=EC=98=A4=EB=8A=94=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whyranoid/walkie/controller/ChallengeController.java | 6 ++++++ .../whyranoid/walkie/repository/ChallengeRepository.java | 5 +++++ .../java/com/whyranoid/walkie/service/ChallengeService.java | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java index 9414597..0529242 100644 --- a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java +++ b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java @@ -26,6 +26,12 @@ public ResponseEntity getNewChallenges(@RequestParam("userId") Long userId) { return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); } + @GetMapping("/challenges/top-rank") + public ResponseEntity getPopularChallenges() { + List challenges = challengeService.getPopularChallenges(); + return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); + } + @GetMapping("/challenges/category") public ResponseEntity getChallengesByCategory(@RequestParam("userId") Long userId, @RequestParam("category") char category) { List challenges = challengeService.getChallengesByCategory(userId, category); diff --git a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java index 7f8b8da..d9d1f53 100644 --- a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java @@ -25,6 +25,11 @@ public List getNewChallenges(Long userId) { .getResultList(); } + public List getPopularChallenges() { + return em.createQuery("select c.name, c.challengeId, count(*) as duplicated_count from Challenge c left join ChallengeStatus cs on c.challengeId = cs.challenge.challengeId group by c.challengeId, c.name order by duplicated_count desc") + .getResultList(); + } + public List getChallengesByCategory(Long userId, char category) { return em.createQuery("select c.name, c.challengeId from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.category = :category") .setParameter("userId", userId) diff --git a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java index 388180c..eb2dc75 100644 --- a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java +++ b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java @@ -25,6 +25,11 @@ public class ChallengeService{ public List getNewChallenges(Long userId) { return challengeRepository.getNewChallenges(userId); } + + public List getPopularChallenges() { + return challengeRepository.getPopularChallenges(); + } + public List getChallengesByCategory(Long userId, char category) { return challengeRepository.getChallengesByCategory(userId, category); } From 29244ea1de5763e78a8075a6f026d7cacc01cf10 Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Sun, 2 Jul 2023 19:45:05 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=EC=B1=8C=EB=A6=B0=EC=A7=80=20?= =?UTF-8?q?=EC=83=81=EC=84=B8=20=ED=8E=98=EC=9D=B4=EC=A7=80=EC=97=90=20?= =?UTF-8?q?=EC=B1=8C=EB=A6=B0=EC=A7=80=EC=97=90=20=EB=8F=84=EC=A0=84=20?= =?UTF-8?q?=EC=A4=91=EC=9D=B8=20=EB=8B=A4=EB=A5=B8=20=EC=82=AC=EB=9E=8C?= =?UTF-8?q?=EB=8F=84=20=EB=9C=A8=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChallengeController.java | 4 +-- .../walkie/dto/ChallengeDetailDto.java | 25 +++++++++++++++++++ .../repository/ChallengeRepository.java | 11 ++++++-- .../walkie/service/ChallengeService.java | 11 ++++++-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java diff --git a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java index 0529242..775a7ef 100644 --- a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java +++ b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java @@ -1,7 +1,7 @@ package com.whyranoid.walkie.controller; import com.whyranoid.walkie.domain.Challenge; -import com.whyranoid.walkie.domain.ChallengeStatus; +import com.whyranoid.walkie.dto.ChallengeDetailDto; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; import com.whyranoid.walkie.service.ChallengeService; @@ -46,7 +46,7 @@ public ResponseEntity getProgressChallenge(@RequestParam("userId") Long userId) @GetMapping("/challenge-detail") public ResponseEntity getChallengeDetail(@RequestParam("challengeId") Long challengeId, @RequestParam("userId") Long userId) { - List challenge = challengeService.getChallengeDetail(challengeId, userId); + ChallengeDetailDto challenge = challengeService.getChallengeDetail(challengeId, userId); return new ResponseEntity<>(challenge, httpHeaders, HttpStatus.OK); } diff --git a/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java b/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java new file mode 100644 index 0000000..22d3d27 --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java @@ -0,0 +1,25 @@ +package com.whyranoid.walkie.dto; + +import com.whyranoid.walkie.domain.ChallengeStatus; +import com.whyranoid.walkie.domain.Walkie; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +public class ChallengeDetailDto { + List challenges; + + List walkies; + + @Builder + public ChallengeDetailDto(List challenges, List walkies) { + this.challenges = challenges; + this.walkies = walkies; + } +} diff --git a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java index d9d1f53..d400333 100644 --- a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java @@ -43,8 +43,15 @@ public List getProgressChallenges(Long userId) { .getResultList(); } - public List getDetailChallenge(Long challengeId, Long userId) { - return em.createQuery("select c, s.progress, s.status from ChallengeStatus s left join Challenge c on c.challengeId = s.challenge.challengeId where c.challengeId = :challengeId and s.walkie.userId = :userId") + public List getChallengeDetail(Long challengeId, Long userId) { + return em.createQuery("select c, cs.progress, cs.status, cs.walkie from ChallengeStatus cs left join Challenge c on c.challengeId = cs.challenge.challengeId where c.challengeId = :challengeId and cs.walkie.userId = :userId") + .setParameter("challengeId", challengeId) + .setParameter("userId", userId) + .getResultList(); + } + + public List getChallengeMember(Long challengeId, Long userId) { + return em.createQuery("select cs.walkie from ChallengeStatus cs where cs.challenge.challengeId = :challengeId and cs.walkie.userId != :userId") .setParameter("challengeId", challengeId) .setParameter("userId", userId) .getResultList(); diff --git a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java index eb2dc75..fc3d09b 100644 --- a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java +++ b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java @@ -3,6 +3,7 @@ import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.domain.ChallengeStatus; import com.whyranoid.walkie.domain.Walkie; +import com.whyranoid.walkie.dto.ChallengeDetailDto; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; import com.whyranoid.walkie.dto.response.ApiResponse; @@ -38,8 +39,14 @@ public List getProgressChallenges(Long userId) { return challengeRepository.getProgressChallenges(userId); } - public List getChallengeDetail(Long challengeId, Long userId) { - return challengeRepository.getDetailChallenge(challengeId, userId); + public ChallengeDetailDto getChallengeDetail(Long challengeId, Long userId) { + List cs = challengeRepository.getChallengeDetail(challengeId, userId); + List walkies = challengeRepository.getChallengeMember(challengeId, userId); + + return ChallengeDetailDto.builder() + .challenges(cs) + .walkies(walkies) + .build(); } public ApiResponse createChallengeStatus(ChallengeStatusCreateRequest challengeStatusCreateRequest) { From fff29be308c5ee24754e73a8f2eddd1d373dee0a Mon Sep 17 00:00:00 2001 From: Seungmin-develop Date: Mon, 31 Jul 2023 21:59:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=B1=8C=EB=A6=B0=EC=A7=80=20api=20swagger?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 - .../controller/ChallengeController.java | 51 ++++++++++++++++--- .../com/whyranoid/walkie/domain/Badge.java | 3 ++ .../walkie/domain/BadgeCollection.java | 28 ++++++++++ .../walkie/domain/ChallengeStatus.java | 2 +- .../com/whyranoid/walkie/domain/Walkie.java | 5 ++ .../walkie/dto/ChallengeDetailDto.java | 8 +-- .../whyranoid/walkie/dto/ChallengeDto.java | 16 ++++-- .../request/ChallengeStatusChangeRequest.java | 3 ++ .../request/ChallengeStatusCreateRequest.java | 3 ++ .../walkie/dto/response/ApiResponse.java | 3 ++ .../dto/response/ChallengePreviewDto.java | 30 +++++++++++ .../walkie/repository/BadgeRepository.java | 22 ++++++++ .../repository/ChallengeRepository.java | 23 +++++---- .../walkie/service/BadgeService.java | 15 ++++++ .../walkie/service/ChallengeService.java | 33 ++++++------ 16 files changed, 200 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/whyranoid/walkie/domain/BadgeCollection.java create mode 100644 src/main/java/com/whyranoid/walkie/dto/response/ChallengePreviewDto.java create mode 100644 src/main/java/com/whyranoid/walkie/repository/BadgeRepository.java create mode 100644 src/main/java/com/whyranoid/walkie/service/BadgeService.java diff --git a/build.gradle b/build.gradle index f5b2e38..8485c97 100644 --- a/build.gradle +++ b/build.gradle @@ -31,8 +31,6 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'mysql:mysql-connector-java:8.0.32' -// compileOnly "org.springframework.boot:spring-boot-starter-security" -// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'org.springdoc:springdoc-openapi-ui:1.7.0' } diff --git a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java index 775a7ef..0e52626 100644 --- a/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java +++ b/src/main/java/com/whyranoid/walkie/controller/ChallengeController.java @@ -1,10 +1,18 @@ package com.whyranoid.walkie.controller; -import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.dto.ChallengeDetailDto; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; +import com.whyranoid.walkie.dto.response.ChallengePreviewDto; import com.whyranoid.walkie.service.ChallengeService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -13,6 +21,7 @@ import java.util.List; +@Tag(name = "Challenge", description = "챌린지 API") @RestController @RequiredArgsConstructor @RequestMapping("/api/challenge") @@ -20,30 +29,54 @@ public class ChallengeController { HttpHeaders httpHeaders = new HttpHeaders(); private final ChallengeService challengeService; + @Operation(summary = "새로운 챌린지 가져오기", description = "새로운 챌린지들을 가져옵니다.") + @Parameters({ + @Parameter(name = "userId", description = "유저 아이디", example = "123") + }) + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ChallengePreviewDto.class)))) @GetMapping("/challenges/new") public ResponseEntity getNewChallenges(@RequestParam("userId") Long userId) { - List challenges = challengeService.getNewChallenges(userId); + List challenges = challengeService.getNewChallenges(userId); return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); } + @Operation(summary = "인기 챌린지 가져오기", description = "인기가 많은 순서대로 챌린지들을 가져옵니다.") + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ChallengePreviewDto.class)))) @GetMapping("/challenges/top-rank") public ResponseEntity getPopularChallenges() { - List challenges = challengeService.getPopularChallenges(); + List challenges = challengeService.getPopularChallenges(); return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); } + @Operation(summary = "카테고리별 챌린지 가져오기", description = "카테고리를 넣어주면 해당 카테고리의 챌린지들을 반환합니다.") + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ChallengePreviewDto.class)))) + @Parameters({ + @Parameter(name = "userId", description = "유저 아이디", example = "123"), + @Parameter(name = "category", description = "카테고리(L은 라이프, C는 칼로리, D는 거리)", example = "C") + }) @GetMapping("/challenges/category") public ResponseEntity getChallengesByCategory(@RequestParam("userId") Long userId, @RequestParam("category") char category) { - List challenges = challengeService.getChallengesByCategory(userId, category); + List challenges = challengeService.getChallengesByCategory(userId, category); return new ResponseEntity<>(challenges, httpHeaders, HttpStatus.OK); } + @Operation(summary = "진행중인 챌린지 가져오기", description = "현재 유저가 진행 중인 챌린지들을 반환합니다.") + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ChallengePreviewDto.class)))) + @Parameters({ + @Parameter(name = "userId", description = "유저 아이디", example = "123") + }) @GetMapping("/challenges/progress") public ResponseEntity getProgressChallenge(@RequestParam("userId") Long userId) { - List progressChallenges = challengeService.getProgressChallenges(userId); + List progressChallenges = challengeService.getProgressChallenges(userId); return new ResponseEntity<>(progressChallenges, httpHeaders, HttpStatus.OK); } + @Operation(summary = "챌린지 상세 정보 보기", description = "챌린지의 상세한 정보를 반환합니다.") + @Parameters({ + @Parameter(name = "challengeId", description = "챌린지 아이디", example = "2"), + @Parameter(name = "userId", description = "유저 아이디", example = "123") + }) + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(schema = @Schema(implementation = ChallengeDetailDto.class))) @GetMapping("/challenge-detail") public ResponseEntity getChallengeDetail(@RequestParam("challengeId") Long challengeId, @RequestParam("userId") Long userId) { ChallengeDetailDto challenge = challengeService.getChallengeDetail(challengeId, userId); @@ -51,12 +84,18 @@ public ResponseEntity getChallengeDetail(@RequestParam("challengeId") Long chall } // 챌린지를 중도 포기할 때나 챌린지 조건에 도달하여 성공하였을 때 + @Operation(summary = "챌린지의 상태 변경하기", description = "챌린지 중도 포기 및 성공 시에 따라 챌린지의 상태를 변경합니다. (챌린지 시작 전 : ‘N’, 챌린지 진행 중 : ‘P’, 챌린지 완료 ‘C’)") + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class))) @PostMapping("/challenge-detail/update-status") - public ResponseEntity updateChallengeStatus(@RequestBody ChallengeStatusChangeRequest challengeStatusChangeRequest) { + public ResponseEntity updateChallengeStatus( + @RequestBody ChallengeStatusChangeRequest challengeStatusChangeRequest + ) { return ResponseEntity.ok(challengeService.updateChallengeStatus(challengeStatusChangeRequest)); } // 챌린지를 새로 시작하는 것으로 ChallengeStatus를 새로 만들어서 추가해야 함. + @Operation(summary = "챌린지의 시작하기", description = "챌린지를 시작합니다.") + @ApiResponse(responseCode = "200", description = "호출 성공", content = @Content(schema = @Schema(implementation = com.whyranoid.walkie.dto.response.ApiResponse.class))) @PostMapping("/challenge-detail/start") public ResponseEntity startChallenge(@RequestBody ChallengeStatusCreateRequest challengeStatusCreateRequest) { return ResponseEntity.ok(challengeService.createChallengeStatus(challengeStatusCreateRequest)); diff --git a/src/main/java/com/whyranoid/walkie/domain/Badge.java b/src/main/java/com/whyranoid/walkie/domain/Badge.java index afd2f65..0205ca7 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Badge.java +++ b/src/main/java/com/whyranoid/walkie/domain/Badge.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.domain; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -15,10 +16,12 @@ public class Badge { @Id @Column(name = "badge_id", nullable = false) + @Schema(example = "2") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long badgeId; @NotNull + @Schema(example = "badge_image2") @Column(name = "badge_img", nullable = false) private String img; } diff --git a/src/main/java/com/whyranoid/walkie/domain/BadgeCollection.java b/src/main/java/com/whyranoid/walkie/domain/BadgeCollection.java new file mode 100644 index 0000000..024cb35 --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/domain/BadgeCollection.java @@ -0,0 +1,28 @@ +package com.whyranoid.walkie.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class BadgeCollection { + + @Id + @Column(name = "collection_id", nullable = false) + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long collectionId; + + @NotNull + @Column(name = "user_id", nullable = false) + private Long userId; + + @NotNull + @Column(name = "badge_id", nullable = false) + private Long badgeId; +} diff --git a/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java b/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java index 7b0197d..87831e8 100644 --- a/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java +++ b/src/main/java/com/whyranoid/walkie/domain/ChallengeStatus.java @@ -27,7 +27,7 @@ public class ChallengeStatus { @NotNull @Column(nullable = false) - private char status; + private char status = 'N'; @NotNull @Column(nullable = false) diff --git a/src/main/java/com/whyranoid/walkie/domain/Walkie.java b/src/main/java/com/whyranoid/walkie/domain/Walkie.java index 40aeb94..6394121 100644 --- a/src/main/java/com/whyranoid/walkie/domain/Walkie.java +++ b/src/main/java/com/whyranoid/walkie/domain/Walkie.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.domain; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -15,19 +16,23 @@ public class Walkie { @Id // Entity의 primary key임을 명시 @Column(name = "user_id") + @Schema(example = "123") @GeneratedValue(strategy = GenerationType.IDENTITY) // 기본 키 생성을 데이터베이스에 위임, mysql의 경우 auto-increment가 기본 private Long userId; @NotNull @Column(name = "user_name", nullable = false) @Size(min = 1, max = 15) // 임의로 지정해 놓은 것 + @Schema(example = "승민") private String userName; @Column(name = "profile_img") + @Schema(example = "seungmin_profile_img") private String profileImg; @NotNull @Column(name = "status", nullable = false) + @Schema(example = "N") private Character status; @NotNull diff --git a/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java b/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java index 22d3d27..5521225 100644 --- a/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java +++ b/src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java @@ -1,6 +1,5 @@ package com.whyranoid.walkie.dto; -import com.whyranoid.walkie.domain.ChallengeStatus; import com.whyranoid.walkie.domain.Walkie; import lombok.Builder; import lombok.Getter; @@ -13,13 +12,14 @@ @Setter @NoArgsConstructor public class ChallengeDetailDto { - List challenges; + + ChallengeDto challenge; List walkies; @Builder - public ChallengeDetailDto(List challenges, List walkies) { - this.challenges = challenges; + public ChallengeDetailDto(ChallengeDto challenge, List walkies) { + this.challenge = challenge; this.walkies = walkies; } } diff --git a/src/main/java/com/whyranoid/walkie/dto/ChallengeDto.java b/src/main/java/com/whyranoid/walkie/dto/ChallengeDto.java index 81c0e15..4993b0f 100644 --- a/src/main/java/com/whyranoid/walkie/dto/ChallengeDto.java +++ b/src/main/java/com/whyranoid/walkie/dto/ChallengeDto.java @@ -1,6 +1,7 @@ package com.whyranoid.walkie.dto; import com.whyranoid.walkie.domain.Badge; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -11,14 +12,19 @@ @AllArgsConstructor @NoArgsConstructor public class ChallengeDto { + @Schema(example = "2") private Long challengeId; - private char category; + @Schema(example = "L") + private Character category; private Badge badge; + @Schema(example = "햄버거 세트의 평균 칼로리는 1110kcal 입니다. 일주일 동안 걷기로 햄버거 세트 태우기 도전!") private String content; + @Schema(example = "햄버거 세트 불태우기") private String name; + @Schema(example = "challenge_img2") private String img; - - // 여기서부터 challenge_status; - private char status; - private int progress; + @Schema(example = "0") + private Character status; + @Schema(example = "N") + private Integer progress; } diff --git a/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusChangeRequest.java b/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusChangeRequest.java index ba5f99d..1fb8c29 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusChangeRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusChangeRequest.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.dto.request; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -11,8 +12,10 @@ @Getter public class ChallengeStatusChangeRequest { @NotNull + @Schema(example = "3") private Long statusId; @NotNull + @Schema(example = "P") private Character status; @Builder 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 bc319b6..e0e1ded 100644 --- a/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java +++ b/src/main/java/com/whyranoid/walkie/dto/request/ChallengeStatusCreateRequest.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.dto.request; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -11,9 +12,11 @@ @Getter public class ChallengeStatusCreateRequest { @NotNull + @Schema(example = "123") private Long userId; @NotNull + @Schema(example = "4") private Long challengeId; @Builder 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 01f9d5b..ee39fc3 100644 --- a/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java +++ b/src/main/java/com/whyranoid/walkie/dto/response/ApiResponse.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.dto.response; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.Builder; import lombok.Getter; @@ -7,6 +8,8 @@ @Builder public class ApiResponse { + @Schema(example = "200") Integer status; + @Schema(example = "성공!") String message; } diff --git a/src/main/java/com/whyranoid/walkie/dto/response/ChallengePreviewDto.java b/src/main/java/com/whyranoid/walkie/dto/response/ChallengePreviewDto.java new file mode 100644 index 0000000..f05c072 --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/dto/response/ChallengePreviewDto.java @@ -0,0 +1,30 @@ +package com.whyranoid.walkie.dto.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.*; + +@Getter +@Setter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class ChallengePreviewDto { + + @Schema(example = "1") + private Long challengeId; + + @Schema(example = "C") + private Character category; + + @Schema(example = "햄버거 세트 불태우기") + private String name; + + @Schema(example = "N") + private Character status; + + @Schema(example = "0") + private Integer progress; + + @Schema(example = "1") + private Integer newFlag; +} diff --git a/src/main/java/com/whyranoid/walkie/repository/BadgeRepository.java b/src/main/java/com/whyranoid/walkie/repository/BadgeRepository.java new file mode 100644 index 0000000..8db4ae3 --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/repository/BadgeRepository.java @@ -0,0 +1,22 @@ +package com.whyranoid.walkie.repository; + +import com.whyranoid.walkie.domain.BadgeCollection; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +@Repository +@Transactional +@RequiredArgsConstructor +public class BadgeRepository { + @PersistenceContext + private final EntityManager em; + + public void insertBadgeCollection(BadgeCollection badgeCollection) { + em.persist(badgeCollection); + } + +} diff --git a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java index d400333..94dd9bf 100644 --- a/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/ChallengeRepository.java @@ -3,6 +3,7 @@ import com.whyranoid.walkie.domain.Challenge; import com.whyranoid.walkie.domain.ChallengeStatus; import com.whyranoid.walkie.domain.Walkie; +import com.whyranoid.walkie.dto.response.ChallengePreviewDto; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -19,35 +20,35 @@ public class ChallengeRepository { @PersistenceContext private final EntityManager em; - public List getNewChallenges(Long userId) { - return em.createQuery("select c.name, c.challengeId from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.newFlag = 1") + public List getNewChallenges(Long userId) { + return em.createQuery("select new com.whyranoid.walkie.dto.response.ChallengePreviewDto(c.challengeId, c.category, c.name, cs.status, cs.progress, c.newFlag) from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.newFlag = 1") .setParameter("userId", userId) .getResultList(); } - public List getPopularChallenges() { - return em.createQuery("select c.name, c.challengeId, count(*) as duplicated_count from Challenge c left join ChallengeStatus cs on c.challengeId = cs.challenge.challengeId group by c.challengeId, c.name order by duplicated_count desc") + public List getPopularChallenges() { + return em.createQuery("select new com.whyranoid.walkie.dto.response.ChallengePreviewDto(c.challengeId, c.category, c.name, cs.status, cs.progress, c.newFlag) from Challenge c left join ChallengeStatus cs on c.challengeId = cs.challenge.challengeId group by c.challengeId, c.name order by count(*) desc") .getResultList(); } - public List getChallengesByCategory(Long userId, char category) { - return em.createQuery("select c.name, c.challengeId from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.category = :category") + public List getChallengesByCategory(Long userId, char category) { + return em.createQuery("select new com.whyranoid.walkie.dto.response.ChallengePreviewDto(c.challengeId, c.category, c.name, cs.status, cs.progress, c.newFlag) from Challenge c left join ChallengeStatus cs on cs.challenge.challengeId = c.challengeId and cs.walkie.userId = :userId where cs.walkie is null and c.category = :category") .setParameter("userId", userId) .setParameter("category", category) .getResultList(); } - public List getProgressChallenges(Long userId) { - return em.createQuery("select c.name, cs.progress, c.challengeId from ChallengeStatus cs left join Challenge c on cs.challenge.challengeId = c.challengeId where cs.walkie.userId = :userId") + public List getProgressChallenges(Long userId) { + return em.createQuery("select new com.whyranoid.walkie.dto.response.ChallengePreviewDto(c.challengeId, c.category, c.name, cs.status, cs.progress, c.newFlag ) from ChallengeStatus cs left join Challenge c on cs.challenge.challengeId = c.challengeId where cs.walkie.userId = :userId") .setParameter("userId", userId) .getResultList(); } - public List getChallengeDetail(Long challengeId, Long userId) { - return em.createQuery("select c, cs.progress, cs.status, cs.walkie from ChallengeStatus cs left join Challenge c on c.challengeId = cs.challenge.challengeId where c.challengeId = :challengeId and cs.walkie.userId = :userId") + public Object getChallengeDetail(Long challengeId, Long userId) { + return em.createQuery("select new com.whyranoid.walkie.dto.ChallengeDto(c.challengeId, c.category, c.badge, c.content, c.name, c.img, cs.status, cs.progress) from ChallengeStatus cs left join Challenge c on c.challengeId = cs.challenge.challengeId where c.challengeId = :challengeId and cs.walkie.userId = :userId") .setParameter("challengeId", challengeId) .setParameter("userId", userId) - .getResultList(); + .getSingleResult(); } public List getChallengeMember(Long challengeId, Long userId) { diff --git a/src/main/java/com/whyranoid/walkie/service/BadgeService.java b/src/main/java/com/whyranoid/walkie/service/BadgeService.java new file mode 100644 index 0000000..60b05ae --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/service/BadgeService.java @@ -0,0 +1,15 @@ +package com.whyranoid.walkie.service; + +import com.whyranoid.walkie.repository.BadgeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +@RequiredArgsConstructor +public class BadgeService { + private final BadgeRepository badgeRepository; + + +} diff --git a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java index fc3d09b..731acc7 100644 --- a/src/main/java/com/whyranoid/walkie/service/ChallengeService.java +++ b/src/main/java/com/whyranoid/walkie/service/ChallengeService.java @@ -4,9 +4,11 @@ import com.whyranoid.walkie.domain.ChallengeStatus; import com.whyranoid.walkie.domain.Walkie; import com.whyranoid.walkie.dto.ChallengeDetailDto; +import com.whyranoid.walkie.dto.ChallengeDto; import com.whyranoid.walkie.dto.request.ChallengeStatusChangeRequest; import com.whyranoid.walkie.dto.request.ChallengeStatusCreateRequest; import com.whyranoid.walkie.dto.response.ApiResponse; +import com.whyranoid.walkie.dto.response.ChallengePreviewDto; import com.whyranoid.walkie.repository.ChallengeRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -23,30 +25,34 @@ public class ChallengeService{ private final ChallengeRepository challengeRepository; - public List getNewChallenges(Long userId) { + public List getNewChallenges(Long userId) { return challengeRepository.getNewChallenges(userId); } - public List getPopularChallenges() { + public List getPopularChallenges() { return challengeRepository.getPopularChallenges(); } - public List getChallengesByCategory(Long userId, char category) { + public List getChallengesByCategory(Long userId, char category) { return challengeRepository.getChallengesByCategory(userId, category); } - public List getProgressChallenges(Long userId) { + public List getProgressChallenges(Long userId) { return challengeRepository.getProgressChallenges(userId); } public ChallengeDetailDto getChallengeDetail(Long challengeId, Long userId) { - List cs = challengeRepository.getChallengeDetail(challengeId, userId); + Object cs = challengeRepository.getChallengeDetail(challengeId, userId); List walkies = challengeRepository.getChallengeMember(challengeId, userId); - return ChallengeDetailDto.builder() - .challenges(cs) - .walkies(walkies) - .build(); + if(cs == null) { + return null; + } else { + return ChallengeDetailDto.builder() + .challenge((ChallengeDto)cs) + .walkies(walkies) + .build(); + } } public ApiResponse createChallengeStatus(ChallengeStatusCreateRequest challengeStatusCreateRequest) { @@ -73,7 +79,6 @@ public ApiResponse createChallengeStatus(ChallengeStatusCreateRequest challengeS } public ApiResponse updateChallengeStatus(ChallengeStatusChangeRequest challengeStatusChangeRequest) { -// try { Character requestStatus = challengeStatusChangeRequest.getStatus(); Long requestStatusId = challengeStatusChangeRequest.getStatusId(); // 챌린지 중도 포기해서 삭제하려는 경우 @@ -89,13 +94,5 @@ else if(requestStatus == 'C') { .status(200) .message("성공") .build(); -// } -// catch(Exception e) { -// return ApiResponse.builder() -// .status(400) -// .message("실패") -// .build(); -// } - } }