Skip to content

Commit

Permalink
Merge pull request #145 from ItRecode/feature/BE-196
Browse files Browse the repository at this point in the history
BE-196 μž„μ‹œ κ³ μ •λ ˆμ½”λ“œ API 개발
  • Loading branch information
kdomo authored Feb 5, 2023
2 parents de085b9 + 25bcfca commit c5f8807
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.recordit.server.dto.record.WriteRecordResponseDto;
import com.recordit.server.dto.record.memory.MemoryRecordRequestDto;
import com.recordit.server.dto.record.memory.MemoryRecordResponseDto;
import com.recordit.server.dto.record.mix.MixRecordResponseDto;
import com.recordit.server.exception.ErrorMessage;
import com.recordit.server.service.RecordService;

Expand Down Expand Up @@ -195,4 +196,9 @@ public ResponseEntity<List<RandomRecordResponseDto>> getRandomRecord(
) {
return ResponseEntity.ok(recordService.getRandomRecord(randomRecordRequestDto));
}

@GetMapping("/mix")
public ResponseEntity<MixRecordResponseDto> getMixRecords() {
return ResponseEntity.ok().body(recordService.getMixRecords());
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/recordit/server/dto/record/mix/MixRecordDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.recordit.server.dto.record.mix;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@ApiModel
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MixRecordDto {
@ApiModelProperty(notes = "믹슀 λ ˆμ½”λ“œ 아이디")
private Long recordId;

@ApiModelProperty(notes = "믹슀 λ ˆμ½”λ“œ 색상이름")
private String colorName;

@ApiModelProperty(notes = "믹슀 λ ˆμ½”λ“œ μ•„μ΄μ½˜μ΄λ¦„")
private String iconName;

@ApiModelProperty(notes = "믹슀 λ ˆμ½”λ“œ λŒ“κΈ€ 아이디")
private Long commentId;

@ApiModelProperty(notes = "믹슀 λ ˆμ½”λ“œ λŒ“κΈ€ λ‚΄μš©")
private String commentContent;

@Builder
public MixRecordDto(
Long recordId,
String colorName,
String iconName,
Long commentId,
String commentContent
) {
this.recordId = recordId;
this.colorName = colorName;
this.iconName = iconName;
this.commentId = commentId;
this.commentContent = commentContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.recordit.server.dto.record.mix;

import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@ApiModel
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MixRecordResponseDto {
@ApiModelProperty(notes = "λ―ΉμŠ€λ ˆμ½”λ“œ 리슀트")
private List<MixRecordDto> mixRecordDto;

@Builder
public MixRecordResponseDto(List<MixRecordDto> mixRecordDto) {
this.mixRecordDto = mixRecordDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.recordit.server.exception.record;

public class FixRecordNotExistException extends RuntimeException {
public FixRecordNotExistException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ public ResponseEntity<ErrorMessage> handleInvalidPageParameterException(
return ResponseEntity.badRequest()
.body(ErrorMessage.of(exception, HttpStatus.BAD_REQUEST));
}

@ExceptionHandler(FixRecordNotExistException.class)
public ResponseEntity<ErrorMessage> handleFixRecordNotExistException(
FixRecordNotExistException exception) {
return ResponseEntity.badRequest()
.body(ErrorMessage.of(exception, HttpStatus.BAD_REQUEST));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findAllByRecordAndParentCommentIsNull(Record record, Pageable pageable);

Long countByRecordId(Long recordId);

@EntityGraph(attributePaths = {"record", "record.recordColor", "record.recordIcon"})
List<Comment> findByRecord(Record fixRecord);
}
36 changes: 36 additions & 0 deletions src/main/java/com/recordit/server/service/RecordService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.recordit.server.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -30,7 +32,10 @@
import com.recordit.server.dto.record.WriteRecordResponseDto;
import com.recordit.server.dto.record.memory.MemoryRecordRequestDto;
import com.recordit.server.dto.record.memory.MemoryRecordResponseDto;
import com.recordit.server.dto.record.mix.MixRecordDto;
import com.recordit.server.dto.record.mix.MixRecordResponseDto;
import com.recordit.server.exception.member.MemberNotFoundException;
import com.recordit.server.exception.record.FixRecordNotExistException;
import com.recordit.server.exception.record.NotMatchLoginUserWithRecordWriterException;
import com.recordit.server.exception.record.RecordColorNotFoundException;
import com.recordit.server.exception.record.RecordIconNotFoundException;
Expand All @@ -54,6 +59,8 @@
@RequiredArgsConstructor
public class RecordService {

private final int MIX_RECORD_COMMENT_SIZE = 10;
private final long FIX_RECORD_PK_VALUE = 31L;
private final int FIRST_PAGE = 0;

private final ImageFileRepository imageFileRepository;
Expand Down Expand Up @@ -290,4 +297,33 @@ public List<RandomRecordResponseDto> getRandomRecord(
commentRepository.countByRecordId(record.getId())
)).collect(Collectors.toList());
}

@Transactional(readOnly = true)
public MixRecordResponseDto getMixRecords() {
Record fixRecord = recordRepository.findById(FIX_RECORD_PK_VALUE)
.orElseThrow(() -> new FixRecordNotExistException("μ„œλ²„μ— κ³ μ • λ ˆμ½”λ“œκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."));

List<MixRecordDto> commentList = commentRepository.findByRecord(fixRecord).stream()
.map(comment -> MixRecordDto.builder()
.commentId(comment.getId())
.colorName(comment.getRecord().getRecordColor().getName())
.iconName(comment.getRecord().getRecordIcon().getName())
.commentContent(comment.getContent())
.recordId(comment.getRecord().getId())
.build()
).collect(Collectors.toList());

Random random = new Random();
List<MixRecordDto> randomCommentList = new ArrayList<>();

if (commentList.size() != 0) {
for (int i = 0; i < MIX_RECORD_COMMENT_SIZE; i++) {
randomCommentList.add(commentList.get(random.nextInt(commentList.size())));
}
}

return MixRecordResponseDto.builder()
.mixRecordDto(randomCommentList)
.build();
}
}
39 changes: 38 additions & 1 deletion src/test/java/com/recordit/server/service/RecordServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import com.recordit.server.dto.record.WriteRecordRequestDto;
import com.recordit.server.dto.record.memory.MemoryRecordRequestDto;
import com.recordit.server.exception.member.MemberNotFoundException;
import com.recordit.server.exception.record.FixRecordNotExistException;
import com.recordit.server.exception.record.NotMatchLoginUserWithRecordWriterException;
import com.recordit.server.exception.record.RecordColorNotFoundException;
import com.recordit.server.exception.record.RecordIconNotFoundException;
import com.recordit.server.exception.record.RecordNotFoundException;
import com.recordit.server.exception.record.category.RecordCategoryNotFoundException;
import com.recordit.server.repository.CommentRepository;
import com.recordit.server.repository.ImageFileRepository;
import com.recordit.server.repository.MemberRepository;
import com.recordit.server.repository.RecordCategoryRepository;
Expand Down Expand Up @@ -73,6 +75,9 @@ class RecordServiceTest {
@Mock
private RecordRepository recordRepository;

@Mock
private CommentRepository commentRepository;

@Mock
private Member mockMember;

Expand Down Expand Up @@ -519,4 +524,36 @@ class 랜덀_λ ˆμ½”λ“œλ₯Ό_죠회_ν• _λ•Œ {
.doesNotThrowAnyException();
}
}
}

@Nested
@DisplayName("믹슀_λ ˆμ½”λ“œλ₯Ό_쑰회_ν• _λ•Œ")
class 믹슀_λ ˆμ½”λ“œλ₯Ό_쑰회_ν• _λ•Œ {
@Test
@DisplayName("μ„œλ²„μ— κ³ μ •λ ˆμ½”λ“œκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ„λ•Œ μ˜ˆμ™Έλ₯Ό λ˜μ§„λ‹€")
void μ„œλ²„μ—_κ³ μ •λ ˆμ½”λ“œκ°€_μ‘΄μž¬ν•˜μ§€_μ•Šμ„λ•Œ_μ˜ˆμ™Έλ₯Ό_λ˜μ§„λ‹€() {
//given
given(recordRepository.findById(anyLong()))
.willReturn(Optional.empty());

//when, then
assertThatThrownBy(() -> recordService.getMixRecords())
.isInstanceOf(FixRecordNotExistException.class)
.hasMessage("μ„œλ²„μ— κ³ μ • λ ˆμ½”λ“œκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
}

@Test
@DisplayName("정상적이라면 μ˜ˆμ™Έλ₯Ό λ˜μ§€μ§€ μ•ŠλŠ”λ‹€")
void 정상적이라면_μ˜ˆμ™Έλ₯Ό_λ˜μ§€μ§€_μ•ŠλŠ”λ‹€() {
//given
given(recordRepository.findById(anyLong()))
.willReturn(Optional.of(mockRecord));

given(commentRepository.findByRecord(mockRecord))
.willReturn(new ArrayList<>());

//when, then
assertThatCode(() -> recordService.getMixRecords())
.doesNotThrowAnyException();
}
}
}

0 comments on commit c5f8807

Please sign in to comment.