Skip to content
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.mariadb.jdbc:mariadb-java-client:3.4.1'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.3.4'
implementation 'org.jboss.logging:jboss-logging:3.5.0.Final' //새로 추가 debug문제로 인해
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public ResponseEntity<Void> deleteCard(@PathVariable UUID cardId) {
}

@GetMapping("/by-date")
public List<CardListResponse> getCardsByCreatedAt(@RequestParam("createdAt") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate createdAt) {
return cardService.getCardsByCreatedAt(createdAt);
public ResponseEntity<List<CardListResponse>> getCardsByCreatedAt(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate createdAt) {
List<CardListResponse> cards = cardService.getCardsByCreatedAt(createdAt);
return ResponseEntity.ok(cards);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.time.LocalDate;
import java.util.List;

@RestController
Expand All @@ -20,16 +21,42 @@ public class StatsController {

private final StatsService statsService;

// @GetMapping("/latest-activities")
// public ResponseEntity<List<LatestActivityResponse>> getTodayActivities(
// @RequestParam String filterByDate,
// @RequestParam String groupBy
// ) {
// if (!"today".equals(filterByDate) || !"spaceId".equals(groupBy)) {
// throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid filterByDate or groupBy parameter");
// }
//
// List<LatestActivityResponse> activities = statsService.getTodayActivitiesGroupedBySpace();
// return ResponseEntity.ok(activities);
// }

@GetMapping("/latest-activities")
public ResponseEntity<List<LatestActivityResponse>> getTodayActivities(
public ResponseEntity<List<LatestActivityResponse>> getActivities(
@RequestParam String filterByDate,
@RequestParam String groupBy
@RequestParam String groupBy,
@RequestParam(required = false) String from,
@RequestParam(required = false) String to
) {
if (!"today".equals(filterByDate) || !"spaceId".equals(groupBy)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid filterByDate or groupBy parameter");
if (!"spaceId".equals(groupBy)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid groupBy parameter");
}

if ("today".equals(filterByDate)) {
return ResponseEntity.ok(statsService.getTodayActivitiesGroupedBySpace());
} else if ("range".equals(filterByDate)) {
if (from == null || to == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Missing 'from' or 'to' parameter for date range");
}

LocalDate fromDate = LocalDate.parse(from);
LocalDate toDate = LocalDate.parse(to);
return ResponseEntity.ok(statsService.getActivitiesByDateRangeGroupedBySpace(fromDate, toDate));
}

List<LatestActivityResponse> activities = statsService.getTodayActivitiesGroupedBySpace();
return ResponseEntity.ok(activities);
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid filterByDate parameter");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.gp_backend_data.card.domain.entity;

import com.example.gp_backend_data.space.domain.entity.Space;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
Expand Down Expand Up @@ -48,5 +49,6 @@ public class Card {

@OneToMany(mappedBy = "card", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Cardlink> cardlinks;

}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand All @@ -17,7 +18,10 @@ public interface CardRepository extends JpaRepository<Card, byte[]> {
void deleteByCardId(byte[] cardId);
@Query("SELECT DISTINCT c FROM Card c " + "JOIN c.cardlinks cl " + "WHERE cl.chatId = :threadId")
Page<Card> findByCardlinkedThreadId(@Param("threadId") byte[] threadId, Pageable pageable);
List<Card> findByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
@Query("SELECT c FROM Card c WHERE DATE(c.createdAt) = CURRENT_DATE")
List<Card> findTodayCreatedCards();
@Query("SELECT c FROM Card c WHERE DATE(c.createdAt) = :createdDate")
List<Card> findAllByCreatedDate(@Param("createdDate") LocalDate createdDate);
// @Query("SELECT c FROM Card c WHERE DATE(c.createdAt) = CURRENT_DATE")
// List<Card> findTodayCreatedCards();
@Query("SELECT c FROM Card c WHERE c.createdAt >= :start AND c.createdAt < :end")
List<Card> findCardsCreatedBetween(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,24 @@ public void deleteCard(UUID cardId) {

//오늘 생성된 지식카드 목록 조회
@Transactional
public List<CardListResponse> getCardsByCreatedAt(LocalDate createdAt) {
// createdAt의 하루 시작과 끝 구하기
LocalDateTime startOfDay = createdAt.atStartOfDay();
LocalDateTime endOfDay = createdAt.atTime(LocalTime.MAX);
public List<CardListResponse> getCardsByCreatedAt(LocalDate date) {
List<Card> cards = cardRepository.findAllByCreatedDate(date);

List<Card> cards = cardRepository.findByCreatedAtBetween(startOfDay, endOfDay);
if (cards.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NO_CONTENT);
}

return cards.stream()
.map(card -> CardListResponse.builder()
return cards.stream().map(card ->
CardListResponse.builder()
.cardId(uuidHelper.convertByteArrayToUUID(card.getCardId()))
.spaceId(uuidHelper.convertByteArrayToUUID(card.getSpaceId()))
.title(card.getTitle())
.createdAt(card.getCreatedAt())
.build())
.collect(Collectors.toList());
.build()
).collect(Collectors.toList());
}


//특정 스레드에 하이라이트된 하이링크 목록 조회
@Transactional
public List<Card> getCardlinkedCards(UUID threadId, int count, int page, String sortBy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -32,15 +34,68 @@ public class StatsService {
private final CardlinkRepository cardlinkRepository;
private final UUIDHelper uuidHelper;

// public List<LatestActivityResponse> getTodayActivitiesGroupedBySpace() {
// List<Card> todayCards = cardRepository.findTodayCreatedCards();
//
// if (todayCards.isEmpty()) {
// throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No activity found for today.");
// }
//
// // 그룹핑
// Map<String, List<Card>> groupedBySpace = todayCards.stream()
// .collect(Collectors.groupingBy(card -> uuidHelper.convertByteArrayToUUID(card.getSpaceId()).toString()));
//
// List<LatestActivityResponse> results = new ArrayList<>();
//
// for (Map.Entry<String, List<Card>> entry : groupedBySpace.entrySet()) {
// UUID spaceId = UUID.fromString(entry.getKey());
// Space space = spaceRepository.findById(uuidHelper.convertUUIDToByteArray(spaceId))
// .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Space not found"));
//
// List<CardSpaceResponse> cardResponses = entry.getValue().stream().map(card -> {
// List<CardlinkResponse> cardlinks = cardlinkRepository.findByCard(card).stream()
// .map(link -> CardlinkResponse.builder()
// .cardlinkId(uuidHelper.convertByteArrayToUUID(link.getCardlinkId()))
// .chatId(uuidHelper.convertByteArrayToUUID(link.getChatId()))
// .content(link.getContent())
// .build())
// .toList();
//
// return CardSpaceResponse.builder()
// .cardId(uuidHelper.convertByteArrayToUUID(card.getCardId()))
// .title(card.getTitle())
// .aiSummary(card.getAiSummary())
// .createdAt(card.getCreatedAt())
// .cardlinks(cardlinks)
// .build();
// }).toList();
//
// results.add(LatestActivityResponse.builder()
// .spaceId(spaceId)
// .spaceName(space.getTitle())
// .cards(cardResponses)
// .build());
// }
//
// return results;
// }

public List<LatestActivityResponse> getTodayActivitiesGroupedBySpace() {
List<Card> todayCards = cardRepository.findTodayCreatedCards();
LocalDate today = LocalDate.now();
return getActivitiesByDateRangeGroupedBySpace(today, today);
}

public List<LatestActivityResponse> getActivitiesByDateRangeGroupedBySpace(LocalDate from, LocalDate to) {
LocalDateTime start = from.atStartOfDay();
LocalDateTime end = to.plusDays(1).atStartOfDay(); // exclusive upper bound

List<Card> cards = cardRepository.findCardsCreatedBetween(start, end);

if (todayCards.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No activity found for today.");
if (cards.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No activity found for selected date range.");
}

// 그룹핑
Map<String, List<Card>> groupedBySpace = todayCards.stream()
Map<String, List<Card>> groupedBySpace = cards.stream()
.collect(Collectors.groupingBy(card -> uuidHelper.convertByteArrayToUUID(card.getSpaceId()).toString()));

List<LatestActivityResponse> results = new ArrayList<>();
Expand Down
Loading