Skip to content

Commit

Permalink
#8 Feat: 선언 페이지 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
5jisoo committed Feb 12, 2024
1 parent 0c382d1 commit 277745a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gdsc.remine.declaration.controller;

import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElement;
import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElements;
import com.gdsc.remine.declaration.service.DeclarationService;
import com.gdsc.remine.global.api_payload.ApiResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,4 +21,9 @@ public ApiResponse<PrivateDeclarationElement> uploadDeclaration(
) {
return ApiResponse.onSuccess(declarationService.uploadDeclaration(audioFile, content));
}

@GetMapping("")
public ApiResponse<PrivateDeclarationElements> getDeclarationInWeek() {
return ApiResponse.onSuccess(declarationService.getDeclarationInWeek());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.gdsc.remine.declaration.domain.Declaration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface DeclarationRepository extends JpaRepository<Declaration, Long> {
Expand All @@ -13,4 +15,18 @@ public interface DeclarationRepository extends JpaRepository<Declaration, Long>
"order by d.createdDate desc " +
"limit 9 ")
List<Declaration> findRecentDeclarations();

@Query("select d from Declaration d " +
"where d.createdDate >= :oneWeekAgo and d.member.id = :memberId " +
"order by d.createdDate desc ")
List<Declaration> findInRecent7Day(
@Param("oneWeekAgo") final LocalDateTime oneWeekAgo,
@Param("memberId") final Long loginMemberId
);

@Query("select count(d) from Declaration d " +
"where d.createdDate >= :oneDayAgo ")
Long countInRecent1Day(
@Param("oneDayAgo") final LocalDateTime oneDayAgo
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.gdsc.remine.declaration.dto.response;

import com.gdsc.remine.declaration.domain.Declaration;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PrivateDeclarationElements {
private Long todayParticipantsCount;
private List<PrivateDeclarationElement> declarationList;

public static PrivateDeclarationElements from(
Long todayParticipantsCount,
List<Declaration> declarationList
) {
final List<PrivateDeclarationElement> declarationElementList = declarationList.stream().map(
PrivateDeclarationElement::new
).collect(Collectors.toList());

return new PrivateDeclarationElements(todayParticipantsCount, declarationElementList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gdsc.remine.declaration.domain.Declaration;
import com.gdsc.remine.declaration.domain.repository.DeclarationRepository;
import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElement;
import com.gdsc.remine.declaration.dto.response.PrivateDeclarationElements;
import com.gdsc.remine.global.gcs.FilePath;
import com.gdsc.remine.global.gcs.service.GoogleCloudStorageService;
import com.gdsc.remine.login.jwt.util.AuthTokensGenerator;
Expand All @@ -12,6 +13,9 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
@RequiredArgsConstructor
public class DeclarationService {
Expand All @@ -36,4 +40,33 @@ public PrivateDeclarationElement uploadDeclaration(
.declaration(saved)
.build();
}

public PrivateDeclarationElements getDeclarationInWeek() {
final Long loginMemberId = authTokensGenerator.getLoginMemberId();

final LocalDateTime oneWeekAgo = getOneWeekAgoDate();
final List<Declaration> declarationList = declarationRepository.findInRecent7Day(
oneWeekAgo,
loginMemberId
);

final LocalDateTime oneDayAgo = getOneDayAgoDate();
final Long count = declarationRepository.countInRecent1Day(oneDayAgo);

return PrivateDeclarationElements.from(
count,
declarationList
);
}

private LocalDateTime getOneWeekAgoDate() {
LocalDateTime oneWeekAgo = LocalDateTime.now().minusDays(6);
return oneWeekAgo
.minusHours(oneWeekAgo.getHour())
.minusMinutes(oneWeekAgo.getMinute());
}

private LocalDateTime getOneDayAgoDate() {
return LocalDateTime.now().minusDays(1);
}
}

0 comments on commit 277745a

Please sign in to comment.