-
Notifications
You must be signed in to change notification settings - Fork 0
Migrations and DBrepository refactory #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8fcc075
15a2bad
22e8941
0d5a0b6
580bee0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| # Default ignored files | ||
| /shelf/ | ||
| /workspace.xml | ||
| /target/ | ||
| logback.xml | ||
| logs/ | ||
| .idea |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,9 @@ | |
|
|
||
| public class DriverManagerDB { | ||
|
|
||
| private final String url = "jdbc:postgresql://localhost:5432/findb"; // put into properties file | ||
| private final String username = "simon"; | ||
| private final String password = "password"; | ||
| private final String url = System.getenv("DBurl"); // put into properties file | ||
| private final String username = System.getenv("DBuser"); | ||
| private final String password = System.getenv("DBpassword"); | ||
|
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SCREAMING_SNAKE_CASE |
||
|
|
||
| public DriverManagerDB() throws SQLException { | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package org.example.Repositories; | ||
|
|
||
|
|
||
| import org.example.DriverManagerDB; | ||
| import org.example.Model.Customer; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.sql.*; | ||
|
|
||
| public class CustomerEntityRepository { | ||
|
|
||
| DriverManagerDB drivermanagerdb = new DriverManagerDB(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You keep doing this, but it's not right. When are you planning to apply dependency inversion principle? |
||
|
|
||
| Connection connection = drivermanagerdb.connect(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this logic every unique repository will have its own active database connection regardless if it's used or now. Use dependency inversion. |
||
|
|
||
| final Logger logger = LoggerFactory.getLogger(TransfersRepository.class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. Dependency inversion. |
||
|
|
||
| public CustomerEntityRepository() throws SQLException { | ||
| } | ||
|
|
||
| public void addCustomerRecord(String name, int money, String password) throws SQLException { | ||
| String sql = "INSERT INTO customers (nickname, money, password) VALUES (?, ?, ?)"; | ||
|
|
||
| PreparedStatement ps = connection.prepareStatement( | ||
| sql, | ||
| Statement.RETURN_GENERATED_KEYS);{ | ||
|
|
||
| ps.setString(1, name); | ||
| ps.setInt(2, money); | ||
| ps.setString(3, password); | ||
|
|
||
| int rows = ps.executeUpdate(); // returns 1 | ||
| logger.trace("Number of rows inserted: {}", rows); | ||
| if (rows == 0){ | ||
| logger.error("Error"); | ||
| } | ||
| try (ResultSet keys = ps.getGeneratedKeys()) { | ||
| if (keys.next()) { | ||
| long id = keys.getLong(1); // ← your SERIAL / IDENTITY value | ||
| logger.info("New customer id = {}", id); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public Customer returnRecord(int customerId) throws SQLException { | ||
|
|
||
| int id = customerId; | ||
| String name = null; | ||
| int money = 0; | ||
| String cusPassword = null; | ||
| String sql = "SELECT id, nickname, money, password FROM customers WHERE id = ?"; | ||
|
|
||
| try ( | ||
| PreparedStatement ps = connection.prepareStatement(sql)) { | ||
| ps.setLong(1, id); | ||
|
|
||
| ResultSet rs = ps.executeQuery(); | ||
|
|
||
| while (rs.next()) { | ||
| id = rs.getInt("id"); // error here probably | ||
| name = rs.getString("nickname"); | ||
| money = rs.getInt("money"); | ||
| cusPassword = rs.getString("password"); | ||
|
Comment on lines
+62
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't you return a Customer here instead of at the bottom of the function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| //TODO: make this code with error logging | ||
| } catch (SQLException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| return new Customer(id, name, money, cusPassword); | ||
| } | ||
|
|
||
| public int findCustomerByUsername(String name){ | ||
| int id = 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, you have a Customer class, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the naming, yes it was indeed incorrect |
||
| String sql = "SELECT id FROM customers WHERE nickname = ?"; | ||
| try ( | ||
| PreparedStatement ps = connection.prepareStatement(sql)) { | ||
| ps.setString(1, name); | ||
|
|
||
| ResultSet rs = ps.executeQuery(); | ||
|
|
||
| while (rs.next()) { | ||
| id = rs.getInt("id"); | ||
| return id; | ||
| } | ||
| } catch (SQLException e) { | ||
| logger.error("error: {}", e); | ||
| } | ||
| return id; | ||
| } | ||
|
|
||
|
|
||
| public boolean updateCustomerFunds(int customerId, int newAmount) { //void | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returns boolean but comment here states void? Also, despite previous times I made comments about it you continue returning true/false as some sort of sanity check for yourself but this kind of convention isn't used anywhere outside of command-line programs, and even then it's not true/false but 1/2/3 and the shell handles the return values as special codes to interpret whether the program succeeded or failed. In your case your functions aren't programs—they're just functions that return an arbitrary value that means nothing. Stop doing this, because you're already getting yourself used to doing something that will give you lots of problems in the future. |
||
|
|
||
| String sql = "UPDATE customers SET money = ? WHERE id = ? "; | ||
| try ( // | ||
| PreparedStatement ps = connection.prepareStatement(sql)) { | ||
|
|
||
| ps.setInt(1, newAmount); | ||
| ps.setInt(2,customerId); | ||
|
|
||
| ps.executeUpdate(); | ||
|
|
||
| } catch (SQLException e) { | ||
| System.out.println(e.getMessage()); | ||
| return false; | ||
| } | ||
| return true; | ||
|
Comment on lines
+107
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why return anything? What's the point? |
||
| } | ||
|
|
||
| public boolean checkIfHasFriend(int senderId, int receiverId) throws SQLException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| String sql = "SELECT FROM friendships WHERE customer_id = ? AND friend_id = ?"; | ||
| PreparedStatement ps = connection.prepareStatement(sql); | ||
|
|
||
| ps.setLong(1, senderId); | ||
| ps.setLong(2, receiverId); | ||
|
|
||
| ResultSet rs = ps.executeQuery(); | ||
| boolean result = rs.next(); | ||
|
|
||
| if (!result) { | ||
| ps.setLong(1, receiverId); | ||
| ps.setLong(2, senderId); | ||
|
|
||
| rs = ps.executeQuery(); | ||
| return rs.next(); | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Environmental variables use SCREAMING_SNAKE_CASE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed