Skip to content

Commit

Permalink
Merge pull request #313 from msdio/feature/backend/longTyping-sort
Browse files Browse the repository at this point in the history
긴글 목록 정렬 기능 구현
  • Loading branch information
tkddls23 authored May 2, 2023
2 parents eb60ec9 + 40bfa99 commit 9be75c5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 24 deletions.
2 changes: 2 additions & 0 deletions backend/src/main/java/com/project/tamago/common/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class Constant {
public static final String SLACK_ALARM_FORMAT = "[SlackAlarm] %s";
public static final int PRACTICE_SHORT_TYPING_SIZE = 30;
public static final int EXAM_SHORT_TYPING_SIZE = 10;
public static final int ITEMS_PER_PAGE = 10;
public static final int LINES_PER_PAGE = 20;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.project.tamago.common.enums;

import org.springframework.data.domain.Sort;

import lombok.Getter;

@Getter
public enum SortOrder {
LATEST("latest"),
OLDEST("oldest"),
VIEW_COUNT("viewCount");

private final String desc;

SortOrder(String desc) {
this.desc = desc;
}

public static Sort getSort(String sortBy) {
if (LATEST.desc.equals(sortBy)) {
return Sort.by("createdDate").descending().and(Sort.by("updatedDate").descending());
}
if (OLDEST.desc.equals(sortBy)) {
return Sort.by("createdDate").ascending().and(Sort.by("updatedDate").ascending());
}
if (VIEW_COUNT.desc.equals(sortBy)) {
return Sort.by("viewCount").descending().and(Sort.by("updatedDate").descending());
}
throw new IllegalArgumentException("Invalid sortBy parameter: " + sortBy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
import com.project.tamago.common.annotation.Ip;
import com.project.tamago.common.annotation.Login;
import com.project.tamago.common.enums.Language;
import com.project.tamago.common.response.CustomResponse;
import com.project.tamago.common.enums.ResponseCode;
import com.project.tamago.common.exception.CustomException;
import com.project.tamago.common.exception.InvalidParameterException;
import com.project.tamago.common.response.CustomResponse;
import com.project.tamago.dto.LoginResolverDto;
import com.project.tamago.dto.requestDto.LongTypingReqDto;
import com.project.tamago.dto.responseDto.LongTypingDetailResDto;
import com.project.tamago.dto.responseDto.LongTypingResDto;
import com.project.tamago.dto.responseDto.ShortTypingListResDto;
import com.project.tamago.common.exception.CustomException;
import com.project.tamago.common.enums.ResponseCode;
import com.project.tamago.dto.requestDto.LongTypingReqDto;

import com.project.tamago.service.LongTypingService;
import com.project.tamago.service.ShortTypingService;

Expand All @@ -48,12 +47,14 @@ public CustomResponse<ShortTypingListResDto> findShortTypings(@RequestParam Stri

@GetMapping("/long")
public CustomResponse<LongTypingResDto> findLongTypings(
@RequestParam(required = false, defaultValue = "1") int page) {
return new CustomResponse<>(longTypingService.findLongTypings(page));
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "latest") String sortBy) {
return new CustomResponse<>(longTypingService.findLongTypings(page, sortBy));
}

@GetMapping("/long/detail")
public CustomResponse<LongTypingDetailResDto> findLongTypingDetail(@Ip String ip, @RequestParam Integer longTypingId,
public CustomResponse<LongTypingDetailResDto> findLongTypingDetail(@Ip String ip,
@RequestParam Integer longTypingId,
@RequestParam(required = false, defaultValue = "1") int page) {
return new CustomResponse<>(longTypingService.findLongTypingDetail(ip, longTypingId, page));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.tamago.service;

import static com.project.tamago.common.Constant.*;
import static com.project.tamago.common.enums.ResponseCode.*;

import java.time.Duration;
Expand All @@ -12,13 +13,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.tamago.common.enums.SortOrder;
import com.project.tamago.common.exception.CustomException;
import com.project.tamago.domain.LongTyping;
import com.project.tamago.domain.User;
import com.project.tamago.dto.LongTypingDto;
import com.project.tamago.dto.mapper.DataMapper;
import com.project.tamago.dto.requestDto.LongTypingReqDto;
import com.project.tamago.dto.responseDto.LongTypingDetailResDto;
import com.project.tamago.dto.LongTypingDto;
import com.project.tamago.common.exception.CustomException;
import com.project.tamago.dto.responseDto.LongTypingResDto;
import com.project.tamago.repository.LongTypingRepository;
import com.project.tamago.repository.RegisterRepository;
Expand All @@ -40,14 +42,14 @@ public class LongTypingService {
private final RedisTemplate<String, Object> redisTemplate;

@Transactional(readOnly = true)
public LongTypingResDto findLongTypings(int page) {
PageRequest pageRequest = PageRequest.of(page - 1, 20);
public LongTypingResDto findLongTypings(int page, String sortBy) {
PageRequest pageRequest = PageRequest.of(page - 1, ITEMS_PER_PAGE, SortOrder.getSort(sortBy));
Page<LongTyping> longTypingPage = longTypingRepository.findAll(pageRequest);
List<LongTypingDto> longTypings = longTypingPage.stream()
.map(DataMapper.INSTANCE::LongTypingToLongTypingResDto)
.collect(Collectors.toList());
int totalPage = longTypingPage.getTotalPages();

int totalPage = longTypingPage.getTotalPages();
return new LongTypingResDto(totalPage, longTypings);
}

Expand Down
8 changes: 4 additions & 4 deletions backend/src/main/java/com/project/tamago/util/TypingUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.project.tamago.util;

import static com.project.tamago.common.Constant.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -12,8 +14,6 @@

public class TypingUtil {

private static final int LINES_PER_PAGE = 20;

public static PageContentDto getPageContent(String content, Integer page) {
String[] contentLines = content.replaceAll("\r\n", "\n").split("\n");
int startIndex = (page - 1) * LINES_PER_PAGE;
Expand All @@ -23,8 +23,8 @@ public static PageContentDto getPageContent(String content, Integer page) {
}

public static PageContentDto getPageContent(String content) {
int totalPages = (int) Math.ceil((double) content.split("\r\n").length / LINES_PER_PAGE);
int randomPage = (int) (Math.random() * totalPages) + 1;
int totalPages = (int)Math.ceil((double)content.split("\r\n").length / LINES_PER_PAGE);
int randomPage = (int)(Math.random() * totalPages) + 1;
return getPageContent(content, randomPage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import org.springframework.security.test.context.support.WithMockUser;

import com.project.tamago.common.enums.Language;
import com.project.tamago.common.enums.SortOrder;
import com.project.tamago.domain.LongTyping;
import com.project.tamago.domain.User;
import com.project.tamago.dto.LongTypingDto;
import com.project.tamago.dto.PageContentDto;
import com.project.tamago.dto.mapper.DataMapper;
import com.project.tamago.dto.requestDto.LongTypingReqDto;
import com.project.tamago.dto.responseDto.LongTypingDetailResDto;
import com.project.tamago.dto.LongTypingDto;
import com.project.tamago.repository.LongTypingRepository;
import com.project.tamago.repository.RegisterRepository;
import com.project.tamago.repository.UserRepository;
Expand Down Expand Up @@ -96,7 +97,8 @@ public void testFindLongTypings() {
.collect(Collectors.toList());

// when
List<LongTypingDto> actualLongTypingDtos = longTypingService.findLongTypings(1).getLongTypings();
List<LongTypingDto> actualLongTypingDtos = longTypingService.findLongTypings(1, SortOrder.LATEST.getDesc())
.getLongTypings();

// then
assertEquals(expectedLongTypingDtos, actualLongTypingDtos);
Expand All @@ -121,13 +123,16 @@ public void testFindLongTypingSuccess() {
.viewCount(100)
.build();

PageContentDto expectedPageContentDto = new PageContentDto(2, "line 21\nline 22\nline 23\nline 24\nline 25\nline 26\nline 27\nline 28\nline 29\nline 30");
when(longTypingRepository.findByIdAndTotalPageGreaterThanEqual(longTypingId, page)).thenReturn(Optional.of(longTyping));
PageContentDto expectedPageContentDto = new PageContentDto(2,
"line 21\nline 22\nline 23\nline 24\nline 25\nline 26\nline 27\nline 28\nline 29\nline 30");
when(longTypingRepository.findByIdAndTotalPageGreaterThanEqual(longTypingId, page)).thenReturn(
Optional.of(longTyping));
when(redisTemplate.opsForValue().get(any(String.class)))
.thenReturn("ON"); // mock the behavior of redisTemplate

// when
LongTypingDetailResDto actualLongTypingDetailResDto = longTypingService.findLongTypingDetail(ip,longTypingId, page);
LongTypingDetailResDto actualLongTypingDetailResDto = longTypingService.findLongTypingDetail(ip, longTypingId,
page);

// then
assertEquals(expectedPageContentDto.getContent(), actualLongTypingDetailResDto.getContent());
Expand Down Expand Up @@ -173,12 +178,14 @@ public void testFindLongTypingFail() {
+ "line 18\n"
+ "line 19\n"
+ "line 20");
when(longTypingRepository.findByIdAndTotalPageGreaterThanEqual(longTypingId, page)).thenReturn(Optional.of(longTyping));
when(longTypingRepository.findByIdAndTotalPageGreaterThanEqual(longTypingId, page)).thenReturn(
Optional.of(longTyping));
when(redisTemplate.opsForValue().get(any(String.class)))
.thenReturn("ON"); // mock the behavior of redisTemplate

// when
LongTypingDetailResDto actualLongTypingDetailResDto = longTypingService.findLongTypingDetail(ip,longTypingId, page);
LongTypingDetailResDto actualLongTypingDetailResDto = longTypingService.findLongTypingDetail(ip, longTypingId,
page);

// then
assertNotEquals(expectedPageContentDto.getContent(), actualLongTypingDetailResDto.getContent());
Expand Down

0 comments on commit 9be75c5

Please sign in to comment.