Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# Default ignored files
/shelf/
/workspace.xml
/target/
logback.xml
logs/
.idea
4 changes: 0 additions & 4 deletions .idea/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions .idea/compiler.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/encodings.xml

This file was deleted.

20 changes: 0 additions & 20 deletions .idea/jarRepositories.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

29 changes: 0 additions & 29 deletions logs/app.log

This file was deleted.

50 changes: 48 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>AppForTests</artifactId>
<artifactId>AppwithPostgreSQL</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
Expand All @@ -14,6 +19,15 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -64,6 +78,11 @@
<artifactId>logback-classic</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -79,6 +98,33 @@
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>11.10.0</version>
<configuration>
<serverId>flyway-db</serverId>
<url>jdbc:postgresql://172.31.224.1:5432/findb</url>
<!-- where V1__*.sql live -->
<locations>
<location>filesystem:src/main/resources/db/migration</location>
</locations>
</configuration>
<dependencies>
<!-- 1️⃣ PostgreSQL database module -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>11.10.0</version>
</dependency>
<!-- 2️⃣ JDBC driver (Flyway won’t ship it for licensing reasons) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2 changes: 1 addition & 1 deletion src/main/java/org/example/Currencies/CurrencyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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");
Copy link
Collaborator

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed



public HashMap getCurrencies() throws IOException {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/example/DriverManagerDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SCREAMING_SNAKE_CASE


public DriverManagerDB() throws SQLException {
}
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/org/example/Repositories/CustomerEntityRepository.java
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();
Copy link
Collaborator

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?


Connection connection = drivermanagerdb.connect();
Copy link
Collaborator

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.


final Logger logger = LoggerFactory.getLogger(TransfersRepository.class);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

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?

Copy link
Owner Author

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

}
//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;
Copy link
Collaborator

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.

Copy link
Owner Author

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

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
Copy link
Collaborator

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.


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
Copy link
Collaborator

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?

}

public boolean checkIfHasFriend(int senderId, int receiverId) throws SQLException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be hasFriend.

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;
}
}

Loading