0

in our project we have an SQL Database with a table representing all users

CREATE TABLE `user_table` (
`firstname` varchar(20) NOT NULL,
  `lastName` varchar(20) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(24) NOT NULL,
  `accessToken` varchar(64) NOT NULL,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) 

and we have a class UserDAO. We need to add methods such as

public boolean areUserCredentialsValid(String email, String password) {...}
public boolean isAccessTokenValid(String accessToken) {...}
public boolean isEmailInUse(String email) {...}

I think the names are pretty self-explicative: we want to perform operations on the entire table in order to return boolean values, so we thought it may not be correct to add these methods in UserDAO, as they do not perform CRUD operations on a single User, instead they perform R-only operations on the entire User Table, so we were thinking about a new class AllUsersDAO but it does not seem convincing. What is the best solution here and why?

I don't know if we are overthinking this, but this is a project for an University course and we are evaluated especially for our software engineering abilities, so we want to be sure to adopt the best solution.

Thank you all in advance!

RaffoSorr
  • 405
  • 1
  • 5
  • 14

4 Answers4

0

From my point of view, you can put CRUD operations in UserDao, but the methods you mentioned should be put in the UserService to implement the methods you mentioned by calling the methods in UserDao in the UserService.

jiangcw
  • 1
  • 1
0

Try to think a service class that use the UserDao for the CRUD operation. The UserDao need to be a simple class that offer the basic operation for data access. The function you are thinking are more a Service function on the data. So try someting this:

public class UserService {

private UserDao userDao;

public boolean areUserCredentialsValid(String email, String password) {...}
public boolean isAccessTokenValid(String accessToken) {...}
public boolean isEmailInUse(String email)
}

In addition, method like "isEmailInUse" sound to me like a DB costraint, try to look for a data check maybe is a better way instead to load 1k + Users in ram to check it.

FedeXu
  • 50
  • 3
0

All is fine, you may put that methods to dao because all this methods related to user, it's better to have dao, and separate class that get all users from dao and search through users to find already used tokens, emails and etc.

fxrbfg
  • 1,756
  • 1
  • 11
  • 17
0

Based on the method signature that you have given i can see that you are passing one email id or email/password or token at a time hence you can define these methods in your service class and you can use this service method for each user.say a UserService class which accepts these credentials then you can use the dao method to query for an object with these credentials if it returns an object you can return true if it returns null you can return false.

public class UserServiceImpl
{
    public boolean isEmailInUse(String email) {
    User user=....;// jdbc code to find a user with this email
    if(user!=null)
      return true;
    else
      return false;
    }
}