Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

챌린지 api swagger에 적용 #13

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
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.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;
Expand All @@ -13,39 +21,81 @@

import java.util.List;

@Tag(name = "Challenge", description = "챌린지 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/challenge")
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 getChallengesByCategory(@RequestParam("userId") Long userId) {
// 본인 정보를 가져오는 로직이 필요
List<Challenge> newChallenges = challengeService.getChallengesByCategory(userId, 'L');
return new ResponseEntity<>(newChallenges, httpHeaders, HttpStatus.OK);
public ResponseEntity getNewChallenges(@RequestParam("userId") Long userId) {
List<ChallengePreviewDto> 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<ChallengePreviewDto> 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<ChallengePreviewDto> 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<Challenge> progressChallenges = challengeService.getProgressChallenges(userId);
List<ChallengePreviewDto> 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) {
List<ChallengeStatus> challenge = challengeService.getChallengeDetail(challengeId, userId);
ChallengeDetailDto challenge = challengeService.getChallengeDetail(challengeId, userId);
return new ResponseEntity<>(challenge, httpHeaders, HttpStatus.OK);
}

// 챌린지를 중도 포기할 때나 챌린지 조건에 도달하여 성공하였을 때
@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));
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/whyranoid/walkie/domain/Badge.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
28 changes: 28 additions & 0 deletions src/main/java/com/whyranoid/walkie/domain/BadgeCollection.java
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions src/main/java/com/whyranoid/walkie/domain/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ public class Challenge {

@Column(name = "calorie")
private int calorie;

@Column(name = "new_flag")
private int newFlag;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ChallengeStatus {

@NotNull
@Column(nullable = false)
private char status;
private char status = 'N';

@NotNull
@Column(nullable = false)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/whyranoid/walkie/domain/Walkie.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/whyranoid/walkie/dto/ChallengeDetailDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.whyranoid.walkie.dto;

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 {

ChallengeDto challenge;

List<Walkie> walkies;

@Builder
public ChallengeDetailDto(ChallengeDto challenge, List<Walkie> walkies) {
this.challenge = challenge;
this.walkies = walkies;
}
}
16 changes: 11 additions & 5 deletions src/main/java/com/whyranoid/walkie/dto/ChallengeDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,8 +12,10 @@
@Getter
public class ChallengeStatusChangeRequest {
@NotNull
@Schema(example = "3")
private Long statusId;
@NotNull
@Schema(example = "P")
private Character status;

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,9 +12,11 @@
@Getter
public class ChallengeStatusCreateRequest {
@NotNull
@Schema(example = "123")
private Long userId;

@NotNull
@Schema(example = "4")
private Long challengeId;

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.whyranoid.walkie.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class ApiResponse {

@Schema(example = "200")
Integer status;
@Schema(example = "성공!")
String message;
}
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions src/main/java/com/whyranoid/walkie/repository/BadgeRepository.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Loading