11package com .vianavitor .simplelibrarygame .controller ;
22
33import com .vianavitor .simplelibrarygame .dto .ApiResponse ;
4+ import com .vianavitor .simplelibrarygame .dto .response .BookReadHistoryResponse ;
45import com .vianavitor .simplelibrarygame .dto .response .UsersBookReadHistoryResponse ;
56import com .vianavitor .simplelibrarygame .model .Book ;
67import com .vianavitor .simplelibrarygame .model .BookReadHistory ;
8+ import com .vianavitor .simplelibrarygame .model .Student ;
79import com .vianavitor .simplelibrarygame .service .BookReadHistoryService ;
810import jakarta .servlet .http .HttpServletRequest ;
911import org .springframework .beans .factory .annotation .Autowired ;
@@ -21,21 +23,25 @@ public class BookReadHistoryController {
2123 @ Autowired
2224 private BookReadHistoryService historyService ;
2325
24- @ PostMapping
25- public ResponseEntity <ApiResponse <Void >> register (@ RequestBody BookReadHistory history , HttpServletRequest request ) {
26- historyService .register (history );
27- ApiResponse <Void > response = ApiResponse .success (null , "Reading progress saved" , request .getRequestURI ());
28- return ResponseEntity .status (HttpStatus .CREATED ).body (response );
29- }
26+ private BookReadHistoryResponse BookReadHistoryToDto (BookReadHistory history ) {
27+ Book book = history .getBook ();
28+ Student student = history .getUser ();
3029
31- @ GetMapping
32- public ResponseEntity <ApiResponse <BookReadHistory >> getById (@ PathVariable Long id , HttpServletRequest request ) {
33- BookReadHistory result = historyService .getById (id );
34- ApiResponse <BookReadHistory > response = ApiResponse .success (result , request .getRequestURI ());
35- return ResponseEntity .ok (response );
30+ return new BookReadHistoryResponse (
31+ history .getId (),
32+ new BookReadHistoryResponse .StudentInfo (
33+ student .getId (),
34+ student .getName ()
35+ ),
36+ new UsersBookReadHistoryResponse .BookInfo (
37+ book .getId (),
38+ book .getTitle ()
39+ ),
40+ history .getLastUpdate (), history .getLastPageRead ()
41+ );
3642 }
3743
38- private UsersBookReadHistoryResponse bookReadHistoryToDto (BookReadHistory history ) {
44+ private UsersBookReadHistoryResponse bookReadHistoryToUsersDto (BookReadHistory history ) {
3945 Book book = history .getBook ();
4046
4147 return new UsersBookReadHistoryResponse (
@@ -48,27 +54,45 @@ private UsersBookReadHistoryResponse bookReadHistoryToDto(BookReadHistory histor
4854 );
4955 }
5056
57+ @ PostMapping
58+ public ResponseEntity <ApiResponse <Void >> register (@ RequestBody BookReadHistory history , HttpServletRequest request ) {
59+ historyService .register (history );
60+ ApiResponse <Void > response = ApiResponse .success (null , "Reading progress saved" , request .getRequestURI ());
61+ return ResponseEntity .status (HttpStatus .CREATED ).body (response );
62+ }
63+
64+ @ GetMapping
65+ public ResponseEntity <ApiResponse <BookReadHistoryResponse >> getById (@ PathVariable Long id , HttpServletRequest request ) {
66+ BookReadHistory result = historyService .getById (id );
67+ ApiResponse <BookReadHistoryResponse > response = ApiResponse .success (this .BookReadHistoryToDto (result ), request .getRequestURI ());
68+ return ResponseEntity .ok (response );
69+ }
70+
5171 @ GetMapping ("/student/{studentId}" )
5272 public ResponseEntity <ApiResponse <List <UsersBookReadHistoryResponse >>> getByStudent (@ PathVariable Long studentId , HttpServletRequest request ) {
5373 List <UsersBookReadHistoryResponse > list = historyService .getByStudent (studentId )
5474 .stream ()
55- .map (this ::bookReadHistoryToDto )
75+ .map (this ::bookReadHistoryToUsersDto )
5676 .toList ();
5777
5878 ApiResponse <List <UsersBookReadHistoryResponse >> response = ApiResponse .success (list , request .getRequestURI ());
5979 return ResponseEntity .ok (response );
6080 }
6181
6282 @ GetMapping ("/book/{bookId}" )
63- public ResponseEntity <ApiResponse <List <BookReadHistory >>> getByBook (@ PathVariable Long bookId , HttpServletRequest request ) {
64- List <BookReadHistory > list = historyService .getByBook (bookId );
65- ApiResponse <List <BookReadHistory >> response = ApiResponse .success (list , request .getRequestURI ());
83+ public ResponseEntity <ApiResponse <List <BookReadHistoryResponse >>> getByBook (@ PathVariable Long bookId , HttpServletRequest request ) {
84+ List <BookReadHistoryResponse > list = historyService .getByBook (bookId )
85+ .stream ()
86+ .map (this ::BookReadHistoryToDto )
87+ .toList ();
88+
89+ ApiResponse <List <BookReadHistoryResponse >> response = ApiResponse .success (list , request .getRequestURI ());
6690 return ResponseEntity .ok (response );
6791 }
6892
6993 @ GetMapping ("/student/{studentId}/last" )
7094 public ResponseEntity <ApiResponse <UsersBookReadHistoryResponse >> getLastByStudent (@ PathVariable Long studentId , HttpServletRequest request ) {
71- UsersBookReadHistoryResponse last = this .bookReadHistoryToDto (
95+ UsersBookReadHistoryResponse last = this .bookReadHistoryToUsersDto (
7296 historyService .getByStudentTheLastOne (studentId )
7397 );
7498
0 commit comments