-
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?
Conversation
…lue FALSE for every customer
| public class CurrencyAPI { | ||
|
|
||
| private static final String url_str = "https://v6.exchangerate-api.com/v6/79f34354ceb4dc1276fecca5/latest/USD"; | ||
| private static final String url_str = System.getenv("currencyAPI"); |
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
| 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"); |
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.
SCREAMING_SNAKE_CASE
|
|
||
| public class CustomerEntityRepository { | ||
|
|
||
| DriverManagerDB drivermanagerdb = new DriverManagerDB(); |
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.
You keep doing this, but it's not right. When are you planning to apply dependency inversion principle?
|
|
||
| DriverManagerDB drivermanagerdb = new DriverManagerDB(); | ||
|
|
||
| Connection connection = drivermanagerdb.connect(); |
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.
With this logic every unique repository will have its own active database connection regardless if it's used or now. Use dependency inversion.
|
|
||
| Connection connection = drivermanagerdb.connect(); | ||
|
|
||
| final Logger logger = LoggerFactory.getLogger(TransfersRepository.class); |
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.
Ditto. Dependency inversion.
| id = rs.getInt("id"); // error here probably | ||
| name = rs.getString("nickname"); | ||
| money = rs.getInt("money"); | ||
| cusPassword = rs.getString("password"); |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
public Customer returnRecord(int customerId) throws SQLException {
String sql = "SELECT id, nickname, money, password FROM customers WHERE id = ?";
try (
PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setLong(1, customerId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
return new Customer(rs.getInt("id"), rs.getString("nickname"), rs.getInt("money"), rs.getString("password"));
}
//TODO: make this code with error logging
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return null;
}
You are indeed correct sir
| } | ||
|
|
||
| public int findCustomerByUsername(String name){ | ||
| int id = 0; |
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.
0 should not be a default return value. You should use nullable Integer class and return null if it does not find a customer.
Also, you have a Customer class, and findCustomerByUsername I assume I'll get some sort of object back... but I get an integer? It's false naming.
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.
I've changed the naming, yes it was indeed incorrect
| } | ||
|
|
||
|
|
||
| public boolean updateCustomerFunds(int customerId, int newAmount) { //void |
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.
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.
| return false; | ||
| } | ||
| 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.
Why return anything? What's the point?
| return true; | ||
| } | ||
|
|
||
| public boolean checkIfHasFriend(int senderId, int receiverId) throws SQLException { |
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.
Should be hasFriend.
| @@ -0,0 +1,5 @@ | |||
| CREATE TABLE transaction_category ( | |||
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.
I see V2 and V3.... where's V1? Where's V0?
| int money = 18500; | ||
| String password = "pass4"; | ||
| dbcontroller.addCustomerRecord(name,money, password); | ||
| customerentityrepository.addCustomerRecord(name,money, password); |
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.
camelCase for variables. I notice you sometimes remember to do this and sometimes forget.
| // int getmoney = customer.getMoneyInCents(); | ||
|
|
||
| Customer returnedCustomer = dbcontroller.returnRecord(customerId); | ||
| Customer returnedCustomer = customerentityrepository.returnRecord(customerId); |
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.
camelCase
| int newAmount = 27000; | ||
|
|
||
| dbcontroller.updateCustomerFunds(customerId, newAmount); | ||
| customerentityrepository.updateCustomerFunds(customerId, newAmount); |
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.
camelCase
| String name = "jennifer"; | ||
|
|
||
| int returnedId = dbcontroller.findCustomerByUsername(name); | ||
| int returnedId = customerentityrepository.findCustomerByUsername(name); |
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.
camelCase
| int receiverId = 3; | ||
|
|
||
| dbcontroller.insertIntoFriendships(senderId, receiverId); | ||
| dbrepository.insertIntoFriendships(senderId, receiverId); |
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.
camelCase
| @Test | ||
| public void checkIfHasFriendTest() throws SQLException { | ||
| boolean isfriend = dbcontroller.checkIfHasFriend(7, 5); | ||
| boolean isfriend = customerentityrepository.checkIfHasFriend(7, 5); |
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.
camelCase
| @Test | ||
| public void returnAuditRecordTest() throws SQLException { | ||
| DBRepository dbcontroller = new DBRepository(); | ||
| TransfersRepository dbcontroller = new TransfersRepository(); |
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.
camelCase
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.
Needs improvement.
Removing .idea from version history is good.
I have a hard time understanding the purpose of this PR. It's labeled migrations but there's only two and there's lots of unrelated code. Why? Create separate PRs or rename to include all this.
Summary
I added Flyway migrations tool, connected it to the postgreSQL database. I added a new table to the database to test out if migrations work correctly. I refactored DBRepository, I made this class into two separate repositories, CustomerEntityRepository and TransfersRepository.