Skip to content

Commit

Permalink
Merge pull request #146 from ItRecode/develop
Browse files Browse the repository at this point in the history
version 0.1.1 배포
  • Loading branch information
kdomo authored Feb 5, 2023
2 parents ad975cf + c5f8807 commit 1d705a9
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/recordit/server/controller/RecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import org.springframework.web.multipart.MultipartFile;

import com.recordit.server.dto.record.ModifyRecordRequestDto;
import com.recordit.server.dto.record.RandomRecordRequestDto;
import com.recordit.server.dto.record.RandomRecordResponseDto;
import com.recordit.server.dto.record.RecordByDateRequestDto;
import com.recordit.server.dto.record.RecordByDateResponseDto;
import com.recordit.server.dto.record.RecordDetailResponseDto;
import com.recordit.server.dto.record.WriteRecordRequestDto;
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 @@ -171,4 +174,31 @@ public ResponseEntity<Long> modifyRecord(
) {
return ResponseEntity.ok().body(recordService.modifyRecord(recordId, modifyRecordRequestDto, attachments));
}

@ApiOperation(
value = "레코드 랜덤 조회",
notes = "레코드를 랜덤으로 조회합니다."
)
@ApiResponses({
@ApiResponse(
code = 200, message = "레코드 랜덤 조회 성공",
response = RandomRecordResponseDto.class
),
@ApiResponse(
code = 400,
message = "잘못 된 요청",
response = ErrorMessage.class
)
})
@GetMapping("/random")
public ResponseEntity<List<RandomRecordResponseDto>> getRandomRecord(
@ModelAttribute @Valid RandomRecordRequestDto randomRecordRequestDto
) {
return ResponseEntity.ok(recordService.getRandomRecord(randomRecordRequestDto));
}

@GetMapping("/mix")
public ResponseEntity<MixRecordResponseDto> getMixRecords() {
return ResponseEntity.ok().body(recordService.getMixRecords());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.recordit.server.dto.record;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@ApiModel
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class RandomRecordRequestDto {
@ApiParam(value = "카테고리 ID", required = true, example = "1")
@NotNull
private Long recordCategoryId;

@ApiParam(value = "댓글 리스트의 사이즈", required = true, example = "5")
@NotNull
private Integer size;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.recordit.server.dto.record;

import com.recordit.server.domain.Record;

import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class RandomRecordResponseDto {
@ApiParam(value = "레코드 ID", required = true)
private Long recordId;

@ApiParam(value = "레코드 제목", required = true)
private String title;

@ApiParam(value = "레코드 컬러명", required = true)
private String colorName;

@ApiParam(value = "레코드 아이콘명", required = true)
private String iconName;

@ApiParam(value = "댓글 개수", required = true)
private Long commentCount;

private RandomRecordResponseDto(Long recordId, String title, String colorName, String iconName, Long commentCount) {
this.recordId = recordId;
this.title = title;
this.colorName = colorName;
this.iconName = iconName;
this.commentCount = commentCount;
}

public static RandomRecordResponseDto of(
Record record,
Long commentCount
) {
return new RandomRecordResponseDto(
record.getId(),
record.getTitle(),
record.getRecordColor().getName(),
record.getRecordIcon().getName(),
commentCount
);
}
}
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 @@ -25,5 +25,10 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

Long countAllByParentComment(Comment parentComment);

List<Comment> findAllByRecord(Record record, Pageable pageable);
List<Comment> findAllByRecordAndParentCommentIsNull(Record record, Pageable pageable);

Long countByRecordId(Long recordId);

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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -52,4 +53,13 @@ Page<Record> findAllByWriterAndCreatedAtBetweenOrderByCreatedAtDesc(

@Query("select r from RECORD r join fetch r.writer where r.id = :id")
Optional<Record> findByIdFetchWriter(Long id);

@Query(value = "select * from RECORD r "
+ "where r.DELETED_AT is null "
+ "and r.RECORD_CATEGORY_ID IN ("
+ "select c.RECORD_CATEGORY_ID "
+ "from RECORD_CATEGORY c where c.PARENT_RECORD_CATEGORY_ID = :categoryId"
+ ") "
+ "order by RAND() limit :size", nativeQuery = true)
List<Record> findRandomRecordByRecordCategoryId(Integer size, Long categoryId);
}
60 changes: 59 additions & 1 deletion 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 All @@ -21,14 +23,19 @@
import com.recordit.server.domain.RecordColor;
import com.recordit.server.domain.RecordIcon;
import com.recordit.server.dto.record.ModifyRecordRequestDto;
import com.recordit.server.dto.record.RandomRecordRequestDto;
import com.recordit.server.dto.record.RandomRecordResponseDto;
import com.recordit.server.dto.record.RecordByDateRequestDto;
import com.recordit.server.dto.record.RecordByDateResponseDto;
import com.recordit.server.dto.record.RecordDetailResponseDto;
import com.recordit.server.dto.record.WriteRecordRequestDto;
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 @@ -52,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 @@ -192,7 +201,7 @@ public MemoryRecordResponseDto getMemoryRecords(MemoryRecordRequestDto memoryRec
// key
findRecord,
// value
commentRepository.findAllByRecord(
commentRepository.findAllByRecordAndParentCommentIsNull(
findRecord,
PageRequest.of(
FIRST_PAGE,
Expand Down Expand Up @@ -268,4 +277,53 @@ public Long modifyRecord(

return record.modify(modifyRecordRequestDto, recordColor, recordIcon);
}

@Transactional
public List<RandomRecordResponseDto> getRandomRecord(
RandomRecordRequestDto randomRecordRequestDto
) {
if (!recordRepository.existsById(randomRecordRequestDto.getRecordCategoryId())) {
throw new RecordCategoryNotFoundException("카테고리 정보를 찾을 수 없습니다.");
}

List<Record> recordList = recordRepository.findRandomRecordByRecordCategoryId(
randomRecordRequestDto.getSize(),
randomRecordRequestDto.getRecordCategoryId()
);

return recordList.stream()
.map(record -> RandomRecordResponseDto.of(
record,
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();
}
}
Loading

0 comments on commit 1d705a9

Please sign in to comment.