Skip to content

Commit

Permalink
Merge pull request #197 from Team-Smeme/sohyeon_#191
Browse files Browse the repository at this point in the history
[ADD] 획득한 뱃지에 타입 데이터 추가
  • Loading branch information
thguss authored Jan 30, 2024
2 parents 6e6ff7d + aff6927 commit 6748419
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.smeme.server.dto.badge;

import com.smeme.server.model.badge.Badge;
import com.smeme.server.model.badge.BadgeType;
import lombok.AccessLevel;
import lombok.Builder;

@Builder(access = AccessLevel.PRIVATE)
public record AcquiredBadgeResponseDTO(
String name,
String imageUrl,
BadgeType type
) {
public static AcquiredBadgeResponseDTO of(Badge badge) {
return AcquiredBadgeResponseDTO.builder()
.name(badge.getName())
.imageUrl(badge.getImageUrl())
.type(badge.getType())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@

import java.util.List;

import com.smeme.server.dto.badge.AcquiredBadgeResponseDTO;
import com.smeme.server.model.badge.Badge;

public record CreatedDiaryResponseDTO(
Long diaryId,
List<BadgeDTO> badges
List<AcquiredBadgeResponseDTO> badges
) {
public static CreatedDiaryResponseDTO of(Long diaryId, List<Badge> badges) {
return new CreatedDiaryResponseDTO(diaryId, badges.stream().map(BadgeDTO::of).toList());
}

public record BadgeDTO(
String name,
String imageUrl
) {
public static BadgeDTO of(Badge badge) {
return new BadgeDTO(badge.getName(), badge.getImageUrl());
}
return new CreatedDiaryResponseDTO(diaryId, badges.stream().map(AcquiredBadgeResponseDTO::of).toList());
}
}


Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package com.smeme.server.dto.member;

import com.smeme.server.dto.badge.AcquiredBadgeResponseDTO;
import com.smeme.server.model.badge.Badge;

import java.util.List;

public record MemberUpdateResponseDTO(
List<BadgeDTO> badges
List<AcquiredBadgeResponseDTO> badges
) {
public static MemberUpdateResponseDTO of(List<Badge> badges) {
return new MemberUpdateResponseDTO(badges.stream()
.map(badge -> new BadgeDTO(badge.getName(), badge.getImageUrl())).toList());
}

record BadgeDTO(
String name,
String imageUrl
) {
return new MemberUpdateResponseDTO(badges.stream().map(AcquiredBadgeResponseDTO::of).toList());
}
}

88 changes: 46 additions & 42 deletions src/main/resources/static/docs/open-api-3.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@
"content" : {
"application/json;charset=UTF-8" : {
"schema" : {
"$ref" : "#/components/schemas/api-v2-diaries-486735900"
"$ref" : "#/components/schemas/api-v2-diaries-609397821"
},
"examples" : {
"일기 작성 성공 Example" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"일기 작성 성공\",\n \"data\" : {\n \"diaryId\" : 1,\n \"badges\" : [ {\n \"name\" : \"뱃지 이름1\",\n \"imageUrl\" : \"image-url1\"\n }, {\n \"name\" : \"뱃지 이름2\",\n \"imageUrl\" : \"image-url2\"\n } ]\n }\n}"
"value" : "{\n \"success\" : true,\n \"message\" : \"일기 작성 성공\",\n \"data\" : {\n \"diaryId\" : 1,\n \"badges\" : [ {\n \"name\" : \"뱃지 이름\",\n \"imageUrl\" : \"badge-image-url\",\n \"type\" : \"EVENT\"\n }, {\n \"name\" : \"뱃지 이름\",\n \"imageUrl\" : \"badge-image-url\",\n \"type\" : \"EVENT\"\n }, {\n \"name\" : \"뱃지 이름\",\n \"imageUrl\" : \"badge-image-url\",\n \"type\" : \"EVENT\"\n }, {\n \"name\" : \"뱃지 이름\",\n \"imageUrl\" : \"badge-image-url\",\n \"type\" : \"EVENT\"\n }, {\n \"name\" : \"뱃지 이름\",\n \"imageUrl\" : \"badge-image-url\",\n \"type\" : \"EVENT\"\n } ]\n }\n}"
}
}
}
Expand Down Expand Up @@ -867,6 +867,50 @@
}
}
},
"api-v2-diaries-609397821" : {
"type" : "object",
"properties" : {
"data" : {
"type" : "object",
"properties" : {
"badges" : {
"type" : "array",
"description" : "획득한 뱃지 리스트",
"items" : {
"type" : "object",
"properties" : {
"imageUrl" : {
"type" : "string",
"description" : "뱃지 이미지 url"
},
"name" : {
"type" : "string",
"description" : "뱃지 이름"
},
"type" : {
"type" : "string",
"description" : "뱃지 타입"
}
}
}
},
"diaryId" : {
"type" : "number",
"description" : "생성한 일기 id"
}
},
"description" : "응답 데이터"
},
"success" : {
"type" : "boolean",
"description" : "응답 성공 여부"
},
"message" : {
"type" : "string",
"description" : "응답 메시지"
}
}
},
"api-v2-topics-random459952356" : {
"type" : "object",
"properties" : {
Expand Down Expand Up @@ -964,46 +1008,6 @@
}
}
},
"api-v2-diaries-486735900" : {
"type" : "object",
"properties" : {
"data" : {
"type" : "object",
"properties" : {
"badges" : {
"type" : "array",
"description" : "획득한 뱃지 리스트",
"items" : {
"type" : "object",
"properties" : {
"imageUrl" : {
"type" : "string",
"description" : "뱃지 이미지 url"
},
"name" : {
"type" : "string",
"description" : "뱃지 이름"
}
}
}
},
"diaryId" : {
"type" : "number",
"description" : "생성한 일기 id"
}
},
"description" : "응답 데이터"
},
"success" : {
"type" : "boolean",
"description" : "응답 성공 여부"
},
"message" : {
"type" : "string",
"description" : "응답 메시지"
}
}
},
"api-v2-diaries1551664804" : {
"type" : "object",
"properties" : {
Expand Down
21 changes: 13 additions & 8 deletions src/test/java/com/smeme/server/controller/DiaryControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.smeme.server.controller;

import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.smeme.server.dto.badge.AcquiredBadgeResponseDTO;
import com.smeme.server.dto.diary.CreatedDiaryResponseDTO;
import com.smeme.server.dto.diary.DiariesResponseDTO;
import com.smeme.server.dto.diary.DiariesResponseDTO.DiaryDTO;
import com.smeme.server.dto.diary.DiaryRequestDTO;
import com.smeme.server.dto.diary.DiaryResponseDTO;
import com.smeme.server.model.badge.BadgeType;
import com.smeme.server.util.ApiResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +21,7 @@
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static com.smeme.server.util.ApiResponse.success;
Expand Down Expand Up @@ -51,7 +54,7 @@ class DiaryControllerTest extends BaseControllerTest {
void success_create_diary() throws Exception {
// given
DiaryRequestDTO request = new DiaryRequestDTO("Hello SMEEM!", 1L);
CreatedDiaryResponseDTO response = new CreatedDiaryResponseDTO(1L, badges());
CreatedDiaryResponseDTO response = new CreatedDiaryResponseDTO(1L, acquiredBadges());
ResponseEntity<ApiResponse> result = ResponseEntity
.created(URI.create("localhost:8080/api/v2/diaries/1"))
.body(success("일기 작성 성공", response));
Expand Down Expand Up @@ -87,7 +90,8 @@ void success_create_diary() throws Exception {
fieldWithPath("data.diaryId").type(NUMBER).description("생성한 일기 id"),
fieldWithPath("data.badges[]").type(ARRAY).description("획득한 뱃지 리스트"),
fieldWithPath("data.badges[].name").type(STRING).description("뱃지 이름"),
fieldWithPath("data.badges[].imageUrl").type(STRING).description("뱃지 이미지 url")
fieldWithPath("data.badges[].imageUrl").type(STRING).description("뱃지 이미지 url"),
fieldWithPath("data.badges[].type").type(STRING).description("뱃지 타입")
)
.build()
)
Expand Down Expand Up @@ -265,12 +269,13 @@ void success_get_diaries() throws Exception {
.andExpect(status().isOk());
}

private List<CreatedDiaryResponseDTO.BadgeDTO> badges() {
List<CreatedDiaryResponseDTO.BadgeDTO> badges = new ArrayList<>();
for (int i = 0; i < 2; i++) {
badges.add(new CreatedDiaryResponseDTO.BadgeDTO("뱃지 이름" + (i + 1), "image-url" + (i + 1)));
}
return badges;
private List<AcquiredBadgeResponseDTO> acquiredBadges() {
return Stream.iterate(1, i -> i + 1).limit(5)
.map(i -> acquiredBadge()).toList();
}

private AcquiredBadgeResponseDTO acquiredBadge() {
return new AcquiredBadgeResponseDTO("뱃지 이름", "badge-image-url", BadgeType.EVENT);
}

private List<DiaryDTO> diaries() {
Expand Down

0 comments on commit 6748419

Please sign in to comment.