Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/#90-scream-preview-jungeun #92

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .gradle/8.8/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.8/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.8/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/file-system.probe
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public ResponseEntity<ApiResponse<?>> createScream(

// 아우성 조회
@GetMapping
public ResponseEntity<ApiResponse<?>> getAllScreams(LocalDateTime updatedAt) {
ResponseEntity<ApiResponse<?>> result = screamService.getAllScreams(updatedAt);
return result;
public ResponseEntity<ApiResponse<?>> getAllScreams(@RequestParam(required = false) Long minutesAgo) {
if (minutesAgo == null) {
minutesAgo = 60L; // 기본값 60분
}
return screamService.getAllScreams(minutesAgo);
}


}
12 changes: 12 additions & 0 deletions src/main/java/com/winner_cat/domain/scream/dto/ScreamListDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public class ScreamListDto {
public static class ScreamResponse {
private String content; // 아우성
private LocalDateTime updatedAt;
private Long minutesAgo; // n분 전
private String timeAgo; // n분 전 또는 n시간 전

public void setMinutesAgo(Long minutesAgo) {
this.minutesAgo = minutesAgo;
}

public void setTimeAgo(String timeAgo) {
this.timeAgo = timeAgo;
}
}

// 아우성 조회
Expand All @@ -25,3 +35,5 @@ public SearchScreamsRes(List<ScreamListDto.ScreamResponse> screams) {
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

@Repository
public interface ScreamRepository extends JpaRepository<Scream, Long> {
@Query("SELECT s FROM Scream s WHERE s.updatedAt BETWEEN :startOfDay AND :endOfDay")
@Query("SELECT s FROM Scream s WHERE s.updatedAt BETWEEN :startOfDay AND :endOfDay ORDER BY s.updatedAt DESC")
List<Scream> findAllByUpdatedAtBetween(
@Param("startOfDay") LocalDateTime startOfDay,
@Param("endOfDay") LocalDateTime endOfDay
);
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

public interface ScreamService {
ResponseEntity<ApiResponse<?>> createScream(ScreamCreateDto.Req req);
ResponseEntity<ApiResponse<?>> getAllScreams(LocalDateTime updatedAt);
ResponseEntity<ApiResponse<?>> getAllScreams(Long minutesAgo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand All @@ -37,22 +38,35 @@ public ResponseEntity<ApiResponse<?>> createScream(ScreamCreateDto.Req req){
}

// 아우성 조회
public ResponseEntity<ApiResponse<?>> getAllScreams(LocalDateTime updatedAt) {
public ResponseEntity<ApiResponse<?>> getAllScreams(Long minutesAgo) {
// 오늘 날짜의 시작과 끝을 정의
LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

List<Scream> screams = screamRepository.findAllByUpdatedAtBetween(startOfDay, endOfDay);
List<ScreamListDto.ScreamResponse> screamResponses = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
for (Scream scream : screams) {
long minutesDifference = Duration.between(scream.getUpdatedAt(), now).toMinutes();
String timeAgo;
if (minutesDifference < 60) {
timeAgo = minutesDifference + "분 전";
} else {
long hoursDifference = minutesDifference / 60;
timeAgo = hoursDifference + "시간 전";
}
screamResponses.add(ScreamListDto.ScreamResponse.builder()
.content(scream.getContent())
.updatedAt(scream.getUpdatedAt())
.minutesAgo(minutesDifference)
.timeAgo(timeAgo)
.build());
}

ScreamListDto.SearchScreamsRes searchScreamsRes = new ScreamListDto.SearchScreamsRes(screamResponses);
return ResponseEntity.ok(ApiResponse.onSuccess(searchScreamsRes));
}


}
Loading