Compare commits

...

2 Commits

Author SHA1 Message Date
maciejrusek
d41b918f59 Added Dao for User with SQLite 2026-04-09 20:50:02 +02:00
maciejrusek
87cbfbd506 Changes 2026-04-08 21:10:35 +02:00
13 changed files with 232 additions and 7 deletions

3
.gitignore vendored
View File

@@ -37,3 +37,6 @@ build/
### Mac OS ###
.DS_Store
*.log
*.db

13
pom.xml
View File

@@ -33,6 +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>

View File

@@ -1,3 +1,5 @@
package client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

View File

@@ -1,3 +1,6 @@
package server;
import user.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -64,7 +67,7 @@ public class ChatServer implements Runnable {
String username = msg.substring(6).strip();
user = getOrCreateUser(username);
clientSockets.add(socket);
logger.info("Client sie połączył");
logger.info("client.Client sie połączył");
}
}
}
@@ -81,6 +84,7 @@ public class ChatServer implements Runnable {
try {
serverSocket = new ServerSocket(port);
logger.error("Test");
while (true) {
Socket clientSocket = serverSocket.accept();

View File

@@ -0,0 +1,14 @@
package server;
public class Controller {
public void route(String path) {
}
}

View File

@@ -1,11 +1,12 @@
package server;
public class Main {
public static void main(String[] args) {
ChatServer chatServer = new ChatServer(9000);
Thread serverThread = new Thread(chatServer);
serverThread.start();
}
}

View File

@@ -0,0 +1,4 @@
package server;
public class Service {
}

View File

@@ -1,5 +1,6 @@
public class User {
package user;
public class User {
private String username;
public User(String username) {
@@ -13,4 +14,11 @@ public class User {
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
'}';
}
}

View File

@@ -0,0 +1,26 @@
package user;
import java.util.Optional;
public class UserController {
private UserService service;
public UserController(UserService service) {
this.service = service;
}
public Optional<User> createUser(
String username,
String password
) {
return service.createUser(username, password);
}
public Optional<User> getUser(
String username,
String password
) {
return service.getUser(username, password);
}
}

View File

@@ -0,0 +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();
}
}
}

View File

@@ -0,0 +1,46 @@
package user;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Optional;
public class UserService {
private static final Logger logger = LogManager.getLogger();
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
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();
}
}
}

View File

@@ -4,7 +4,7 @@
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="all.log" immediateFlush="true" append="true" >
<File name="File" fileName="logs/all.log" immediateFlush="true" append="true" >
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>

View File

@@ -1,3 +1,4 @@
import server.ChatServer;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;