Skip to content

Commit

Permalink
optimize search functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HliasMpGH committed Sep 7, 2024
1 parent 4df6419 commit 003e0b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
37 changes: 19 additions & 18 deletions gui/src/main/java/gr/bibliotech/data/BookDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.Types;

import javax.sql.DataSource;

Expand All @@ -27,9 +28,9 @@ public class BookDAO {
* @return a list of books
*/
public List<Book> getBooks() {
List<Book> books = jdbcTemplate.query("SELECT * FROM BOOKS", new BookMapper());

return books;
return jdbcTemplate.query(
"SELECT * FROM BOOKS", new BookMapper()
);
}

/**
Expand All @@ -38,21 +39,21 @@ public List<Book> getBooks() {
* @return a list of books that match the term
*/
public List<Book> getMatchingBooks(String term) {
List<Book> matchingBooks = new ArrayList<>();

// the pattern to search by
Pattern pattern = Pattern.compile(term, Pattern.CASE_INSENSITIVE | Pattern.LITERAL);

for (Book book : getBooks()) {
// try to match the pattern in the title and author of Book
Matcher titleString = pattern.matcher(book.getTitle());
Matcher authorSting = pattern.matcher(book.getAuthor());

if (titleString.find() || authorSting.find()) {
matchingBooks.add(book); // the book matches the search
}
}
return matchingBooks;
String query = ""
+ " SELECT * FROM BOOKS"
+ " WHERE LOWER(TITLE)"
+ " LIKE LOWER(?)"
+ " OR LOWER(AUTHOR)"
+ " LIKE LOWER(?)";

String matchTerm = "%" + term + "%";

return jdbcTemplate.query(
query,
new Object[] {matchTerm, matchTerm}, // the parameters to bind
new int[] {Types.VARCHAR, Types.VARCHAR}, // the sql types of the parameters
new BookMapper() // the record mapper
);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions gui/src/main/java/gr/bibliotech/data/UserDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class UserDAO {
* @return a list of users.
*/
public List<User> getUsers() {
List<User> users = jdbcTemplate.query("SELECT * FROM USERS", new UserMapper());

return users;
return jdbcTemplate.query(
"SELECT * FROM USERS", new UserMapper()
);
}

/**
Expand All @@ -44,9 +44,9 @@ public List<User> getUsers() {
public boolean existsUserName(String username) {
String query = "SELECT COUNT(*) > 0 FROM USERS WHERE username = ?";

Boolean existsName = jdbcTemplate.queryForObject(query, Boolean.class, username);

return existsName;
return jdbcTemplate.queryForObject(
query, Boolean.class, username
);
}

/**
Expand All @@ -58,13 +58,13 @@ public boolean existsUserName(String username) {
public boolean existsUserMail(String email) {
String query = "SELECT COUNT(*) > 0 FROM USERS WHERE email = ?";

Boolean existsMail = jdbcTemplate.queryForObject(query, Boolean.class, email);

return existsMail;
return jdbcTemplate.queryForObject(
query, Boolean.class, email
);
}

/**
* Used to Authenticate a users' Credentials upon Signing In
* Used to Authenticate a users' Credentials upon Signing In
* @param username the username to match
* @param password the password to match with association to the username
* @return the user that matches the username & password association
Expand Down

0 comments on commit 003e0b9

Please sign in to comment.