Skip to content

Commit

Permalink
Merge pull request #19 from SWM-Flash/feature/FLASH-164-UUIDConverter…
Browse files Browse the repository at this point in the history
…-converter

문제의 id converter 다른 애그리거트에서도 사용할 수 있도록 수정
  • Loading branch information
ChoiWonYu authored Jul 16, 2024
2 parents 26c14da + 6943a79 commit 44e6dc7
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.first.flash.climbing.problem.domain.vo.DifficultyInfo;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.util.UUID;
Expand All @@ -24,7 +23,6 @@ public class Problem {
private static final Boolean DEFAULT_IS_EXPIRED = false;

@Id
@Convert(converter = UUIDConverter.class)
@Column(columnDefinition = "BINARY(16)")
private UUID id;
private String imageUrl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.first.flash.climbing.problem.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
Expand Down Expand Up @@ -35,7 +34,6 @@ public class QueryProblem {
private static final int DIFFICULTY_LEVEL_WEIGHT = 2;

@Id
@Convert(converter = UUIDConverter.class)
@Column(columnDefinition = "BINARY(16)")
private UUID id;
private String imageUrl;
Expand Down
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,10 +2,12 @@

import com.first.flash.climbing.solution.domain.dto.SolutionCreateRequestDto;
import com.first.flash.climbing.solution.domain.vo.SolutionDetail;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -23,18 +25,19 @@ public class Solution {
private Long id;
SolutionDetail solutionDetail;
private Long optionalWeight;
private Long problemId;
@Column(columnDefinition = "BINARY(16)")
private UUID problemId;

protected Solution(final String uploader, final String review, final String instagramId,
final String videoUrl, final Long problemId) {
final String videoUrl, final UUID problemId) {

this.solutionDetail = SolutionDetail.of(uploader, review, instagramId, videoUrl);
this.optionalWeight = DEFAULT_OPTIONAL_WEIGHT;
this.problemId = problemId;
}

public static Solution of(final SolutionCreateRequestDto createRequestDto,
final Long problemId) {
final UUID problemId) {

return new Solution(createRequestDto.uploader(), createRequestDto.review(),
createRequestDto.instagramId(), createRequestDto.videoUrl(), problemId);
Expand Down
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
@@ -1,12 +1,12 @@
package com.first.flash.climbing.problem.domain;
package com.first.flash.global.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.UUID;

@Converter
@Converter(autoApply = true)
public class UUIDConverter implements AttributeConverter<UUID, byte[]> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
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
.fromString("0000-0000-0000-0000-0000");
private final static Long NON_EXISTENT_SOLUTION_ID = 999L;

private SolutionRepository solutionRepository;
Expand Down Expand Up @@ -87,4 +89,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,13 @@
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
.fromString("0000-0000-0000-0000-0000");

final private Map<Long, Solution> db = new HashMap<>();
private Long id = 0L;
Expand All @@ -32,9 +34,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 44e6dc7

Please sign in to comment.