-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from sanchitc05/sanchitc05/issue95
[FEATURE] Enhance Reporting and Analytics Capabilities
- Loading branch information
Showing
5 changed files
with
121 additions
and
14 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/libraryman_api/analytics/AnalyticsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.libraryman_api.analytics; | ||
|
||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/analytics") | ||
public class AnalyticsController { | ||
|
||
private final AnalyticsService analyticsService; | ||
|
||
public AnalyticsController(AnalyticsService analyticsService) { | ||
this.analyticsService = analyticsService; | ||
} | ||
|
||
@GetMapping("/overview") | ||
public ResponseEntity<Map<String, Object>> getLibraryOverview() { | ||
return ResponseEntity.ok(analyticsService.getLibraryOverview()); | ||
} | ||
|
||
@GetMapping("/popular-books") | ||
public ResponseEntity<List<Map<String, Object>>> getPopularBooks(@RequestParam(defaultValue = "10") int limit) { | ||
return ResponseEntity.ok(analyticsService.getPopularBooks(limit)); | ||
} | ||
|
||
@GetMapping("/borrowing-trends") | ||
public ResponseEntity<Map<String, Long>> getBorrowingTrends( | ||
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, | ||
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) { | ||
return ResponseEntity.ok(analyticsService.getBorrowingTrends(startDate, endDate)); | ||
} | ||
|
||
@GetMapping("/member-activity") | ||
public ResponseEntity<List<Map<String, Object>>> getMemberActivityReport() { | ||
return ResponseEntity.ok(analyticsService.getMemberActivityReport()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/libraryman_api/analytics/AnalyticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.libraryman_api.analytics; | ||
|
||
import com.libraryman_api.book.BookRepository; | ||
import com.libraryman_api.borrowing.BorrowingRepository; | ||
import com.libraryman_api.member.MemberRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class AnalyticsService { | ||
|
||
private final BookRepository bookRepository; | ||
private final BorrowingRepository borrowingRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
public AnalyticsService(BookRepository bookRepository, BorrowingRepository borrowingRepository, MemberRepository memberRepository) { | ||
this.bookRepository = bookRepository; | ||
this.borrowingRepository = borrowingRepository; | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
public Map<String, Object> getLibraryOverview() { | ||
Map<String, Object> overview = new HashMap<>(); | ||
overview.put("totalBooks", bookRepository.count()); | ||
overview.put("totalMembers", memberRepository.count()); | ||
overview.put("totalBorrowings", borrowingRepository.count()); | ||
return overview; | ||
} | ||
|
||
public List<Map<String, Object>> getPopularBooks(int limit) { | ||
return borrowingRepository.findMostBorrowedBooks(limit); | ||
} | ||
|
||
public Map<String, Long> getBorrowingTrends(LocalDate startDate, LocalDate endDate) { | ||
return borrowingRepository.getBorrowingTrendsBetweenDates(startDate, endDate); | ||
} | ||
|
||
public List<Map<String, Object>> getMemberActivityReport() { | ||
return memberRepository.getMemberActivityReport(); | ||
} | ||
} |
29 changes: 24 additions & 5 deletions
29
src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,37 @@ | ||
package com.libraryman_api.borrowing; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Repository | ||
public interface BorrowingRepository extends JpaRepository<Borrowings, Integer> { | ||
|
||
// Underscore (_) used for property traversal, navigating from Borrowings to Members entity via 'member' property | ||
Page<Borrowings> findByMember_memberId(int memberId, Pageable pageable); | ||
Page<Borrowings> findByMember_memberId(int memberId, Pageable pageable); { | ||
try { | ||
Page<Borrowings> borrowings = borrowingRepository.findByMember_memberId(memberId, pageable); | ||
|
||
List<Borrowings> findByBook_bookId(int bookId); | ||
if (borrowings.isEmpty()) { | ||
throw new ResourceNotFoundException("Member didn't borrow any book"); | ||
} | ||
return borrowings.map(this::EntityToDto); | ||
} catch (PropertyReferenceException ex) { | ||
throw new InvalidSortFieldException("The specified 'sortBy' value is invalid."); | ||
} | ||
} | ||
|
||
@Query(value = "SELECT b.title AS bookTitle, COUNT(*) AS borrowCount " + | ||
"FROM borrowings br JOIN books b ON br.book_id = b.book_id " + | ||
"GROUP BY b.book_id ORDER BY borrowCount DESC LIMIT :limit", nativeQuery = true) | ||
List<Map<String, Object>> findMostBorrowedBooks(int limit); | ||
|
||
@Query("SELECT FUNCTION('DATE', b.borrowDate) as date, COUNT(*) as count " + | ||
"FROM Borrowings b " + | ||
"WHERE b.borrowDate BETWEEN :startDate AND :endDate " + | ||
"GROUP BY FUNCTION('DATE', b.borrowDate)") | ||
Map<String, Long> getBorrowingTrendsBetweenDates(LocalDate startDate, LocalDate endDate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters