Added Dao for User with SQLite
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -39,3 +39,4 @@ build/
|
||||
.DS_Store
|
||||
|
||||
*.log
|
||||
*.db
|
||||
7
pom.xml
7
pom.xml
@@ -33,12 +33,19 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Server -->
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.51.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Client -->
|
||||
<dependency>
|
||||
<groupId>com.googlecode.lanterna</groupId>
|
||||
<artifactId>lanterna</artifactId>
|
||||
<version>3.1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -10,11 +10,17 @@ public class UserController {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public Optional<User> loginIntoUser(
|
||||
public Optional<User> createUser(
|
||||
String username,
|
||||
String password
|
||||
) {
|
||||
return service.loginIntoUser(username, password);
|
||||
return service.createUser(username, password);
|
||||
}
|
||||
|
||||
public Optional<User> getUser(
|
||||
String username,
|
||||
String password
|
||||
) {
|
||||
return service.getUser(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,103 @@
|
||||
package user;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public static final String DRIVER = "org.sqlite.JDBC";
|
||||
public static final String DB_URL = "jdbc:sqlite:chat.db";
|
||||
|
||||
private Connection conn;
|
||||
private Statement stat;
|
||||
|
||||
public UserDao() {
|
||||
try {
|
||||
conn = DriverManager.getConnection(DB_URL);
|
||||
stat = conn.createStatement();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
createTables();
|
||||
}
|
||||
|
||||
public boolean createTables() {
|
||||
String createUsers = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(255) UNIQUE, password VARCHAR(255) )";
|
||||
try {
|
||||
stat.execute(createUsers);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Boolean checkIfUsernameExists(String username) {
|
||||
try {
|
||||
PreparedStatement preparedStatement = conn.prepareStatement(
|
||||
"SELECT * FROM users WHERE username=(?);"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, username);
|
||||
ResultSet result = preparedStatement.executeQuery();
|
||||
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Optional<User> insertUser(String username, String password) {
|
||||
try {
|
||||
PreparedStatement preparedStatement = conn.prepareStatement(
|
||||
"INSERT INTO users VALUES (NULL, ?, ?);"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, username);
|
||||
preparedStatement.setString(2, password); // FIXME, later hash or smth..
|
||||
preparedStatement.execute();
|
||||
|
||||
User user = new User(username);
|
||||
return Optional.of(user);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<User> getUser(String username, String password) {
|
||||
try {
|
||||
PreparedStatement preparedStatement = conn.prepareStatement(
|
||||
"SELECT * FROM users WHERE username=(?);"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, username);
|
||||
ResultSet result = preparedStatement.executeQuery();
|
||||
|
||||
// FIXME: Hasło tu też ogarnąć
|
||||
String passwordFromDB;
|
||||
|
||||
while (result.next()) {
|
||||
passwordFromDB = result.getString("password");
|
||||
|
||||
if (password.equals(passwordFromDB)) {
|
||||
return Optional.of(new User(username));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,29 @@ public class UserService {
|
||||
this.userDao = userDao;
|
||||
}
|
||||
|
||||
public Optional<User> loginIntoUser(
|
||||
public Optional<User> createUser(
|
||||
String username,
|
||||
String pass
|
||||
) {
|
||||
try {
|
||||
|
||||
if (!userDao.checkIfUsernameExists(username)) {
|
||||
return userDao.insertUser(username, pass);
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error creating user: {}", e.toString());
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<User> getUser(
|
||||
String username,
|
||||
String pass
|
||||
) {
|
||||
try {
|
||||
return userDao.getUser(username, pass);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while login into user: {}", e.toString());
|
||||
return Optional.empty();
|
||||
|
||||
Reference in New Issue
Block a user