Skip to content

Commit

Permalink
FLASH-164 refactor: problemId를 모두 UUID로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
ChoiWonYu committed Jul 16, 2024
1 parent da86f91 commit 4817c27
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.first.flash.climbing.solution.domain.dto.SolutionCreateRequestDto;
import com.first.flash.climbing.solution.exception.exceptions.SolutionNotFoundException;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -20,22 +21,22 @@ public class SolutionService {
private final SolutionRepository solutionRepository;

@Transactional
public SolutionResponseDto saveSolution(final Long problemId,
public SolutionResponseDto saveSolution(final UUID problemId,
final SolutionCreateRequestDto createRequestDto) {
Solution solution = Solution.of(createRequestDto, problemId);
return SolutionResponseDto.toDto(solutionRepository.save(solution));
}

public Solution findSolutionById(final Long id) {
return solutionRepository.findById(id)
.orElseThrow(() -> new SolutionNotFoundException(id));
.orElseThrow(() -> new SolutionNotFoundException(id));
}

public SolutionsResponseDto findAllSolutionsByProblemId(final Long problemId) {
public SolutionsResponseDto findAllSolutionsByProblemId(final UUID problemId) {
List<SolutionResponseDto> solutions = solutionRepository.findAllByProblemId(problemId)
.stream()
.map(SolutionResponseDto::toDto)
.toList();
.stream()
.map(SolutionResponseDto::toDto)
.toList();
return new SolutionsResponseDto(solutions, new SolutionMetaResponseDto(solutions.size()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public interface SolutionRepository {

Solution save(final Solution solution);

Optional<Solution> findById(final Long id);

List<Solution> findAllByProblemId(final Long problemId);
List<Solution> findAllByProblemId(final UUID problemId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.first.flash.climbing.solution.domain.Solution;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SolutionJpaRepository extends JpaRepository<Solution, Long> {
Expand All @@ -11,5 +12,5 @@ public interface SolutionJpaRepository extends JpaRepository<Solution, Long> {

Optional<Solution> findById(final Long id);

List<Solution> findByProblemId(final Long problemId);
List<Solution> findByProblemId(final UUID problemId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.first.flash.climbing.solution.domain.SolutionRepository;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand All @@ -24,7 +25,7 @@ public Optional<Solution> findById(final Long id) {
}

@Override
public List<Solution> findAllByProblemId(final Long problemId) {
public List<Solution> findAllByProblemId(final UUID problemId) {
return solutionJpaRepository.findByProblemId(problemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -32,7 +33,7 @@ public class SolutionController {
@ApiResponse(responseCode = "404", description = "해설을 찾을 수 없음")
})
@GetMapping("problems/{problemId}/solutions")
public ResponseEntity<SolutionsResponseDto> getSolutions(@PathVariable final Long problemId) {
public ResponseEntity<SolutionsResponseDto> getSolutions(@PathVariable final UUID problemId) {
SolutionsResponseDto response = solutionService.findAllSolutionsByProblemId(
problemId);

Expand All @@ -44,10 +45,11 @@ public ResponseEntity<SolutionsResponseDto> getSolutions(@PathVariable final Lon
@ApiResponse(responseCode = "201", description = "성공적으로 해설 영상을 업로드함")
})
@PostMapping("problems/{problemId}/solutions")
public ResponseEntity<SolutionResponseDto> createSolution(@PathVariable final Long problemId,
public ResponseEntity<SolutionResponseDto> createSolution(@PathVariable final UUID problemId,
@RequestBody final SolutionCreateRequestDto solutionCreateRequestDto) {

return ResponseEntity.status(HttpStatus.CREATED)
.body(solutionService.saveSolution(problemId, solutionCreateRequestDto));
.body(
solutionService.saveSolution(problemId, solutionCreateRequestDto));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import com.first.flash.climbing.solution.domain.dto.SolutionCreateRequestDto;
import com.first.flash.climbing.solution.exception.exceptions.SolutionNotFoundException;
import com.first.flash.climbing.solution.infrastructure.FakeSolutionRepository;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SolutionServiceTest {

private final static Long DEFAULT_PROBLEM_ID = 1L;
private final static UUID DEFAULT_PROBLEM_ID = UUID.randomUUID();
private final static Long NON_EXISTENT_SOLUTION_ID = 999L;

private SolutionRepository solutionRepository;
Expand Down Expand Up @@ -87,4 +88,4 @@ void init() {
softly.assertThat(solutionsResponse.meta().count()).isEqualTo(2);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public class FakeSolutionRepository implements SolutionRepository {

private final static Long DEFAULT_OPTIONAL_WEIGHT = 0L;
private final static Long DEFAULT_PROBLEM_ID = 1L;
private final static UUID DEFAULT_PROBLEM_ID = UUID.randomUUID();

final private Map<Long, Solution> db = new HashMap<>();
private Long id = 0L;
Expand All @@ -32,9 +33,9 @@ public Optional<Solution> findById(Long id) {
}

@Override
public List<Solution> findAllByProblemId(Long problemId) {
public List<Solution> findAllByProblemId(final UUID problemId) {
return db.values().stream()
.filter(solution -> solution.getProblemId().equals(problemId))
.toList();
.filter(solution -> solution.getProblemId().equals(problemId))
.toList();
}
}

0 comments on commit 4817c27

Please sign in to comment.