1
- package com .onevizion .test .oneviziontest .book ;
1
+ package com .onevizion .test .oneviziontest .book . dao ;
2
2
3
+ import com .onevizion .test .oneviziontest .book .model .AuthorBookTitleSymbolCount ;
4
+ import com .onevizion .test .oneviziontest .book .model .AuthorBooks ;
5
+ import com .onevizion .test .oneviziontest .book .model .Book ;
3
6
import org .springframework .jdbc .core .JdbcTemplate ;
4
7
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
5
8
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
8
11
import java .util .Arrays ;
9
12
import java .util .List ;
10
13
14
+ import static com .onevizion .test .oneviziontest .book .model .Book .*;
15
+
11
16
@ Repository
12
17
public class BookDao {
13
18
@@ -23,10 +28,10 @@ public List<Book> getBooksByTitleDesc() {
23
28
String getBookByTitleDescQuery = "SELECT * FROM BOOK ORDER BY title DESC" ;
24
29
return jdbcTemplate .query (getBookByTitleDescQuery ,
25
30
(result , rowNum ) -> new Book (
26
- result .getLong ("id" ),
27
- result .getString ("title" ),
28
- result .getString ("author" ),
29
- result .getString ("description" )));
31
+ result .getLong (BOOK_FIELD_ID ),
32
+ result .getString (BOOK_FIELD_TITLE ),
33
+ result .getString (BOOK_FIELD_AUTHOR ),
34
+ result .getString (BOOK_FIELD_DESCRIPTION )));
30
35
}
31
36
32
37
public List <AuthorBooks > getAuthorBooks () {
@@ -36,7 +41,7 @@ public List<AuthorBooks> getAuthorBooks() {
36
41
String books = result .getString ("book_list" );
37
42
List <String > bookList = Arrays .stream (books .split ("," )).toList ();
38
43
return new AuthorBooks (
39
- result .getString ("author" ),
44
+ result .getString (BOOK_FIELD_AUTHOR ),
40
45
bookList
41
46
);
42
47
});
@@ -59,9 +64,19 @@ SELECT b.author, SUM(LENGTH(b.title) - LENGTH(REPLACE(b.title, :symbol, ''))) AS
59
64
""" ;
60
65
MapSqlParameterSource params = new MapSqlParameterSource ();
61
66
params .addValue ("symbol" , s );
62
- return namedParamJdbcTemplate .query (getAuthorBookCountBySymbolOccurrenceQuery , params , (result , rowNumb ) -> new AuthorBookTitleSymbolCount (
63
- result .getString ("author" ),
64
- result .getInt ("symbol_count" )
65
- ));
67
+ return namedParamJdbcTemplate .query (getAuthorBookCountBySymbolOccurrenceQuery , params , (result , rowNumb ) ->
68
+ new AuthorBookTitleSymbolCount (
69
+ result .getString (BOOK_FIELD_AUTHOR ),
70
+ result .getInt ("symbol_count" )
71
+ ));
72
+ }
73
+
74
+ public List <Book > getAllBooks () {
75
+ String getAllBooksQuery = "SELECT * FROM BOOK" ;
76
+ return jdbcTemplate .query (getAllBooksQuery , (result , rowNum ) ->
77
+ new Book (result .getLong (BOOK_FIELD_ID ),
78
+ result .getString (BOOK_FIELD_TITLE ),
79
+ result .getString (BOOK_FIELD_AUTHOR ),
80
+ result .getString (BOOK_FIELD_DESCRIPTION )));
66
81
}
67
82
}
0 commit comments