Skip to content

Commit

Permalink
#32 [feat] 미션 카테고리 상세 조회하기 Api 구현 (#37)
Browse files Browse the repository at this point in the history
* feat: Add ResponseDto

* feat: Mission category GET api
  • Loading branch information
2zerozu authored Jul 12, 2023
1 parent 1f060dd commit bde4343
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/universe/uni/controller/MissionController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.universe.uni.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.universe.uni.dto.response.MissionCategoryResponseDto;
import com.universe.uni.service.MissionService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/mission")
public class MissionController {
private final MissionService missionService;

@GetMapping("/{missionCategoryId}")
@ResponseStatus(HttpStatus.OK)
public MissionCategoryResponseDto getMissionCategory(@PathVariable Long missionCategoryId) {
return missionService.getMissionCategory(missionCategoryId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.universe.uni.dto.response;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import lombok.Builder;

@JsonPropertyOrder({"id", "title", "description", "tip", "image", "missionContentList"})
@Builder
public record MissionCategoryResponseDto(
Long id,
String title,
String description,
String tip,
String image,
List<MissionContentResponseDto> missionContentList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.universe.uni.dto.response;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import lombok.Builder;

@JsonPropertyOrder({"id", "content"})
@Builder
public record MissionContentResponseDto(
Long id,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@

public interface MissionContentRepository extends JpaRepository<MissionContent, Long> {
List<MissionContent> findByMissionCategory(MissionCategory missionCategory);

List<MissionContent> findByMissionCategoryId(Long missionCategoryId);
}
45 changes: 42 additions & 3 deletions src/main/java/com/universe/uni/service/MissionService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.universe.uni.service;

import static com.universe.uni.exception.dto.ErrorType.*;
import static com.universe.uni.exception.dto.ErrorType.NOT_FOUND_MISSION_CATEGORY_EXCEPTION;
import static com.universe.uni.exception.dto.ErrorType.NOT_FOUND_MISSION_CONTENT;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.universe.uni.domain.entity.MissionCategory;
import com.universe.uni.domain.entity.MissionContent;
import com.universe.uni.dto.response.MissionCategoryResponseDto;
import com.universe.uni.dto.response.MissionContentResponseDto;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.repository.MissionCategoryRepository;
import com.universe.uni.repository.MissionContentRepository;
Expand All @@ -17,6 +22,7 @@

@Service
@RequiredArgsConstructor
@Transactional
public class MissionService {

private final MissionCategoryRepository missionCategoryRepository;
Expand All @@ -28,8 +34,7 @@ public MissionCategory getMissionCategoryById(Long missionCategoryId) {
}

public MissionContent getMissionContentByRandom(MissionCategory missionCategory) {
List<MissionContent> missionContentList = missionContentRepository
.findByMissionCategory(missionCategory);
List<MissionContent> missionContentList = missionContentRepository.findByMissionCategory(missionCategory);

try {
int randomIndex = new Random().nextInt(missionContentList.size());
Expand All @@ -38,4 +43,38 @@ public MissionContent getMissionContentByRandom(MissionCategory missionCategory)
throw new NotFoundException(NOT_FOUND_MISSION_CONTENT);
}
}

public MissionCategoryResponseDto getMissionCategory(Long missionCategoryId) {
MissionCategory missionCategory = missionCategoryRepository.findById(missionCategoryId)
.orElseThrow(() -> new NotFoundException(NOT_FOUND_MISSION_CATEGORY_EXCEPTION));
return fromMissionCategoryToMissionCategoryResponseDto(missionCategory);
}

private MissionCategoryResponseDto fromMissionCategoryToMissionCategoryResponseDto(
MissionCategory missionCategory) {

List<MissionContent> missionContents = missionContentRepository.findByMissionCategoryId(
missionCategory.getId());

List<MissionContentResponseDto> missionContentResponseDtoList = missionContents.stream()
.map(this::fromMissionContentToMissionContentResponseDto)
.collect(Collectors.toList());

return MissionCategoryResponseDto.builder()
.id(missionCategory.getId())
.title(missionCategory.getTitle())
.description(missionCategory.getDescription())
.tip(missionCategory.getTip())
.image(missionCategory.getImage())
.missionContentList(missionContentResponseDtoList)
.build();
}

private MissionContentResponseDto fromMissionContentToMissionContentResponseDto(MissionContent missionContent) {
return MissionContentResponseDto.builder()
.id(missionContent.getId())
.content(missionContent.getContent())
.build();
}

}

0 comments on commit bde4343

Please sign in to comment.