diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..a816432 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:12900/TodoApp + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..5cb71ef --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..0a0b3a2 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 00c31fd..6c868e0 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,14 @@ - Connection Pool - HikariCP - Migracje Flyway +### Tips: +``` shell +# Uruchomienie migracji +mvn clean flyway:migrate -Dflyway.user= -Dflyway.password= -Dflyway.url= +``` ## TODO: - [x] Fabryka połączenia do bazy danych -- [x] Testy bazy \ No newline at end of file +- [x] Testy bazy +- [x] Zmienić to na h2 w testach unit... +- [x] Dodać jakąs migracje danych, proste 2 tabelki jako 2 migracje. \ No newline at end of file diff --git a/pom.xml b/pom.xml index b38bf60..19f5920 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,16 @@ UTF-8 + + + + org.flywaydb + flyway-maven-plugin + 12.4.0 + + + + com.zaxxer @@ -27,6 +37,19 @@ 42.7.10 + + org.flywaydb + flyway-core + 12.4.0 + compile + + + org.flywaydb + flyway-database-postgresql + 12.4.0 + runtime + + io.github.cdimascio dotenv-java @@ -40,18 +63,26 @@ test - - org.testcontainers - postgresql - 1.19.8 - test - org.junit.jupiter junit-jupiter 5.9.3 test + + + com.h2database + h2 + 2.4.240 + test + + + + org.testcontainers + postgresql + 1.19.8 + test + \ No newline at end of file diff --git a/src/main/java/main.java b/src/main/java/main.java new file mode 100644 index 0000000..4eede29 --- /dev/null +++ b/src/main/java/main.java @@ -0,0 +1,18 @@ +import ui.MyFrame; + +import java.awt.*; + +public class main { + + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + + @Override + public void run() { + new MyFrame(); + } + }); + + } + +} diff --git a/src/main/java/models/Task.java b/src/main/java/models/Task.java index 9036a5f..acdaba3 100644 --- a/src/main/java/models/Task.java +++ b/src/main/java/models/Task.java @@ -5,16 +5,26 @@ import java.util.Objects; public class Task { + private User user; private String title; private String description; private Date createdAt; - public Task(String title, String description) { + public Task(String title, String description, User user) { + this.user = user; this.title = title; this.description = description; this.createdAt = new Date(); } + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + public String getTitle() { return title; } @@ -43,6 +53,7 @@ public class Task { public String toString() { return "Task{" + "title='" + title + '\'' + + ", user='" + user + '\'' + ", description='" + description + '\'' + ", createdAt=" + createdAt + '}'; @@ -52,11 +63,11 @@ public class Task { public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; Task task = (Task) o; - return Objects.equals(title, task.title) && Objects.equals(description, task.description) && Objects.equals(createdAt, task.createdAt); + return Objects.equals(user, task.user) && Objects.equals(title, task.title) && Objects.equals(description, task.description) && Objects.equals(createdAt, task.createdAt); } @Override public int hashCode() { - return Objects.hash(title, description, createdAt); + return Objects.hash(user, title, description, createdAt); } } diff --git a/src/main/java/repository/impl/TaskRepositoryImpl.java b/src/main/java/repository/impl/TaskRepositoryImpl.java new file mode 100644 index 0000000..ead8bbb --- /dev/null +++ b/src/main/java/repository/impl/TaskRepositoryImpl.java @@ -0,0 +1,9 @@ +package repository.impl; + +import repository.TaskRepository; + +public class TaskRepositoryImpl { + + + +} diff --git a/src/main/java/service/UserService.java b/src/main/java/service/UserService.java new file mode 100644 index 0000000..12b0b93 --- /dev/null +++ b/src/main/java/service/UserService.java @@ -0,0 +1,32 @@ +package service; + +import models.User; + +import java.util.Optional; + +public class UserService { + + public Optional login(String username, String password) { + try { + + + + return Optional.empty(); + } catch (Exception e) { + System.out.println(e.getMessage()); + return Optional.empty(); + } + } + + public Optional register(String username, String password) { + try { + + + return Optional.empty(); + } catch (Exception e) { + System.out.println(e.getMessage()); + return Optional.empty(); + } + } + +} diff --git a/src/main/java/ui/LoginPanel.java b/src/main/java/ui/LoginPanel.java new file mode 100644 index 0000000..c5151ae --- /dev/null +++ b/src/main/java/ui/LoginPanel.java @@ -0,0 +1,43 @@ +package ui; + +import models.User; +import service.UserService; +import util.Session; + +import javax.swing.*; +import java.awt.*; +import java.util.Optional; + +public class LoginPanel { + + public static JPanel getLoginPanel(){ + JPanel panel = new JPanel(); + + JTextField username = new JTextField("Login"); + JPasswordField password = new JPasswordField("Password"); + + panel.add(username, BorderLayout.NORTH); + panel.add(password, BorderLayout.CENTER); + + JButton loginButton = new JButton("Login"); + loginButton.addActionListener(e -> { + String usernameText = username.getText(); + String passwordText = new String(password.getPassword()); + + UserService service = new UserService(); + + Optional user = service.login(usernameText, passwordText); + if (user.isPresent()) { + Session.getInstance().setUser(user.get()); + // Wywoałeni jakeiś metody która by mi zmieniała Panele + // Home + } else { + JOptionPane.showMessageDialog(panel, "User not found"); + } + }); + + panel.add(loginButton, BorderLayout.SOUTH); + + return panel; + } +} diff --git a/src/main/java/ui/MyFrame.java b/src/main/java/ui/MyFrame.java new file mode 100644 index 0000000..544a2ef --- /dev/null +++ b/src/main/java/ui/MyFrame.java @@ -0,0 +1,36 @@ +package ui; + +import javax.swing.*; +import java.awt.*; + +public class MyFrame extends JFrame { + JPanel mainPanel = new JPanel(new CardLayout()); + + public void changePanel(String panel) { + + } + + public MyFrame() { + super("Todo App"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(800, 600); + setLocation(50, 50); + setLayout(new CardLayout()); + + JPanel startPanel = new JPanel(); + JButton loginButton = new JButton("Login"); + + startPanel.add(loginButton); + mainPanel.add(startPanel, "start"); + mainPanel.add(LoginPanel.getLoginPanel(), "login"); + + loginButton.addActionListener(e -> { + CardLayout cl = (CardLayout)mainPanel.getLayout(); + cl.show(mainPanel, "login"); + }); + + add(mainPanel); + + setVisible(true); + } +} diff --git a/src/main/java/util/Session.java b/src/main/java/util/Session.java new file mode 100644 index 0000000..8e520ae --- /dev/null +++ b/src/main/java/util/Session.java @@ -0,0 +1,28 @@ +package util; + +import models.User; + +public class Session { + private static Session instance; + private User user; + + public static Session getInstance() { + if (instance == null) { + instance = new Session(); + } + return instance; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public void logout() { + this.user = null; + } + +} diff --git a/src/main/resources/db/migration/V1.0__create_user_schema.sql b/src/main/resources/db/migration/V1.0__create_user_schema.sql new file mode 100644 index 0000000..b35f857 --- /dev/null +++ b/src/main/resources/db/migration/V1.0__create_user_schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS users ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + name VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL +); \ No newline at end of file diff --git a/src/main/resources/db/migration/V2.0__add_email_to_user.sql b/src/main/resources/db/migration/V2.0__add_email_to_user.sql new file mode 100644 index 0000000..200f576 --- /dev/null +++ b/src/main/resources/db/migration/V2.0__add_email_to_user.sql @@ -0,0 +1 @@ +ALTER TABLE users ADD email varchar(255) \ No newline at end of file diff --git a/src/main/resources/db/migration/V3.0__create_task_schema.sql b/src/main/resources/db/migration/V3.0__create_task_schema.sql new file mode 100644 index 0000000..a6cc0c0 --- /dev/null +++ b/src/main/resources/db/migration/V3.0__create_task_schema.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS tasks ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + user_id INT NOT NULL, + title varchar(50) NOT NULL, + description TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX idx_tasks_user_id ON tasks (user_id); + +ALTER TABLE tasks ADD FOREIGN KEY(user_id) REFERENCES users(id) ON UPDATE NO ACTION ON DELETE CASCADE; \ No newline at end of file diff --git a/src/test/java/DatabaseConnectionTest.java b/src/test/java/DatabaseConnectionTest.java index 87a7661..10565f7 100644 --- a/src/test/java/DatabaseConnectionTest.java +++ b/src/test/java/DatabaseConnectionTest.java @@ -10,28 +10,26 @@ import org.testcontainers.containers.PostgreSQLContainer; public class DatabaseConnectionTest { - static PostgreSQLContainer postgres = new PostgreSQLContainer<>( - "postgres:16-alpine" - ); +// static PostgreSQLContainer postgres = new PostgreSQLContainer<>( +// "postgres:16-alpine" +// ); - @BeforeAll - static void beforeAll() { - postgres.start(); - } - - @AfterAll - static void afterAll() { - postgres.stop(); - } +// @BeforeAll +// static void beforeAll() { +// postgres.start(); +// } +// +// @AfterAll +// static void afterAll() { +// postgres.stop(); +// } @Test public void shouldConnect() throws Exception { - try (Connection conn = DriverManager.getConnection( - postgres.getJdbcUrl(), - postgres.getUsername(), - postgres.getPassword() - )) { - assertFalse(conn.isClosed()); - } + Connection conn = DriverManager.getConnection( + "jdbc:h2:mem:testdb" + ); + + assertFalse(conn.isClosed()); } }