Skip to content

Commit

Permalink
fix: Fix mission Category Response
Browse files Browse the repository at this point in the history
  • Loading branch information
jinsu4755 committed Dec 21, 2023
1 parent 2d105e6 commit f87d7c8
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.universe.uni.controller;

import com.universe.uni.controller.docs.MissionControllerV1Contract;
import com.universe.uni.dto.response.MissionCategoryWithContentsDto;
import com.universe.uni.service.MissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/mission")
public class MissionCategoryControllerV1 implements MissionControllerV1Contract {
private final MissionService missionService;

@GetMapping("/{missionCategoryId}")
@ResponseStatus(HttpStatus.OK)
@Override
public MissionCategoryWithContentsDto getSelectedMissionCategory(@PathVariable Long missionCategoryId) {
return missionService.getSelectedMissionCategory(missionCategoryId);
}

@GetMapping()
@ResponseStatus(HttpStatus.OK)
@Override
public List<MissionCategoryWithContentsDto> getAllMissionCategories() {
return missionService.getMissionCategories();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.universe.uni.controller.docs;

import com.universe.uni.dto.response.MissionCategoryResponseDto;
import com.universe.uni.dto.response.MissionCategoryWithContentsDto;
import com.universe.uni.exception.dto.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;

@Tag(
name = "Mission",
description = "사용자의 미션 관련 API 입니다."
)
public interface MissionControllerV1Contract {

@Operation(
summary = "미션 카테고리 상세 조회",
description = "사용자는 미션 카테고리 아이디를 통해 상세 정보를 조회할 수 있다.",
responses = {
@ApiResponse(
responseCode = "200",
description = "성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = MissionCategoryResponseDto.class)
)
),
@ApiResponse(
responseCode = "404-UE5003",
description = "존재하지 않는 미션 카테고리입니다",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponse.class)
)
),
@ApiResponse(
responseCode = "500-UE500",
description = "서버 내부 오류입니다.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponse.class)
)
)
}
)
@GetMapping("/{missionCategoryId}")
@ResponseStatus(HttpStatus.OK)
MissionCategoryWithContentsDto getSelectedMissionCategory(
@Parameter(required = true)
@PathVariable Long missionCategoryId
);

@Operation(
summary = "미션 카테고리 전체 조회",
description = "사용자는 게임 생성시 미션의 모든 카테고리를 확인할 수 있다.",
responses = {
@ApiResponse(
responseCode = "200",
description = "성공",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(
schema = @Schema(implementation = MissionCategoryResponseDto.class)
)
)
),
@ApiResponse(
responseCode = "500-UE500",
description = "서버 내부 오류입니다.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ErrorResponse.class)
)
)
}
)
@GetMapping()
@ResponseStatus(HttpStatus.OK)
List<MissionCategoryWithContentsDto> getAllMissionCategories();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.universe.uni.dto.response;

import com.universe.uni.domain.entity.MissionCategory;
import com.universe.uni.dto.MissionContentDto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

import java.util.List;

@Schema(description = "미션 카테고리 정보 DTO")
@Getter
public class MissionCategoryWithContentsDto extends MissionCategoryDto {

private final List<MissionContentDto> missionContentsDto;

public MissionCategoryWithContentsDto(
MissionCategory missionCategory,
List<MissionContentDto> missionContentsDto
) {
super(missionCategory);
this.missionContentsDto = missionContentsDto;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/universe/uni/service/MissionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.universe.uni.domain.entity.MissionContent;
import com.universe.uni.dto.MissionContentDto;
import com.universe.uni.dto.response.MissionCategoryResponseDto;
import com.universe.uni.dto.response.MissionCategoryWithContentsDto;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.repository.MissionCategoryRepository;
import com.universe.uni.repository.MissionContentRepository;
Expand Down Expand Up @@ -90,4 +91,27 @@ private MissionContentDto fromMissionContentToMissionContentResponseDto(MissionC
.build();
}

public MissionCategoryWithContentsDto getSelectedMissionCategory(Long missionCategoryId) {
MissionCategory missionCategory = missionCategoryRepository.findById(missionCategoryId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_MISSION_CATEGORY_EXCEPTION));
return new MissionCategoryWithContentsDto(missionCategory, getAllMissionContents(missionCategory.getId()));
}

public List<MissionCategoryWithContentsDto> getMissionCategories() {
List<MissionCategory> missionCategoryList = missionCategoryRepository.findAll();

return missionCategoryList
.stream()
.map(missionCategory -> new MissionCategoryWithContentsDto(missionCategory, getAllMissionContents(missionCategory.getId())))
.toList();
}

private List<MissionContentDto> getAllMissionContents(Long missionCategoryId) {
List<MissionContent> missionContentList = missionContentRepository.findByMissionCategoryId(missionCategoryId);

return missionContentList.stream()
.map(this::fromMissionContentToMissionContentResponseDto)
.toList();
}

}

0 comments on commit f87d7c8

Please sign in to comment.