Skip to content
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/339Lr3BJ)
### How the tests work (and Docker requirement)

This project ships with an end‑to‑end CLI integration test suite that uses Testcontainers to spin up a temporary MySQL database.
Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
mysql:
image: mysql:9.5.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: testdb
MYSQL_USER: user
MYSQL_PASSWORD: pass
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
Empty file modified mvnw
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<version>9.5.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -40,6 +45,11 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/com/example/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example;

public class Account {
private int user_id;
private String name, password, first_name, last_name, ssn;

public Account(int user_id, String name, String password, String first_name, String last_name, String ssn) {
this.user_id = user_id;
this.name = name;
this.password = password;
this.first_name = first_name;
this.last_name = last_name;
this.ssn = ssn;
}

public Account(String name, String password, String first_name, String last_name, String ssn) {
this.name = name;
this.password = password;
this.first_name = first_name;
this.last_name = last_name;
this.ssn = ssn;
}

public int getUser_id() {
return user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getFirst_name() {
return first_name;
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getLast_name() {
return last_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

public String getSsn() {
return ssn;
}

public void setSsn(String ssn) {
this.ssn = ssn;
}

@Override
public String toString() {
return "Account{" +
"user_id=" + user_id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", ssn='" + ssn + '\'' +
'}';
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/example/AccountRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example;

import java.util.List;
import java.util.Optional;

public interface AccountRepository {
List<String> findUsernames();
List<String> findPasswords();
Optional<Account> findByUsername(String username);
List<Account> findAccounts();
boolean createAccount(Account account);
int countAccounts();
boolean updatePassword(int id, String password);
boolean deleteAccount(int id);
}
176 changes: 176 additions & 0 deletions src/main/java/com/example/AccountRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.example;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class AccountRepositoryImpl implements AccountRepository {
private final DataSource dataSource;
Connection connection;

public AccountRepositoryImpl(DataSource dataSource) {
this.dataSource = dataSource;
}


@Override
public List<String> findUsernames() {
List<String> usernames = new ArrayList<>();
String sql = "select * from account";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
usernames.add(rs.getString("name"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return usernames;
}

@Override
public List<String> findPasswords() {
List<String> passwords = new ArrayList<>();
String sql = "select * from account";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
passwords.add(rs.getString("password"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return passwords;
}

@Override
public Optional<Account> findByUsername(String username) {
String sql = "select * from account where name = ?";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Account account = new Account(
rs.getInt("user_id"),
rs.getString("name"),
rs.getString("password"),
rs.getString("first_name"),
rs.getString("last_name"),
rs.getString("ssn")
);
return Optional.of(account);
}
else return Optional.empty();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
public List<Account> findAccounts() {
List<Account> accounts = new ArrayList<>();
String sql = "select * from account";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Account a = new Account(
rs.getInt("user_id"),
rs.getString("name"),
rs.getString("password"),
rs.getString("first_name"),
rs.getString("last_name"),
rs.getString("ssn")
);
accounts.add(a);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return accounts;
}

@Override
public boolean createAccount(Account account) {
String sql = "insert into account(name, password, first_name, last_name, ssn) values (?, ?, ?, ?, ?)";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, account.getName());
ps.setString(2, account.getPassword());
ps.setString(3, account.getFirst_name());
ps.setString(4, account.getLast_name());
ps.setString(5, account.getSsn());
if (ps.executeUpdate() == 1) {
return true;
}


} catch (SQLException e) {
return false;
}
return false;
}

@Override
public int countAccounts() {
int count = 0;
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement("select count(*) from account");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return count;
}
@Override
public boolean updatePassword(int id, String password) {
String sql = "update account set password=? where user_id=?";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, password);
ps.setInt(2, id);
int rowsAffected = ps.executeUpdate();

if (rowsAffected > 0) {
return true;
}

} catch (SQLException e) {
return false;
}
return false;
}

@Override
public boolean deleteAccount(int id) {
String sql = "delete from account where user_id=?";
try {
connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
int rowsAffected = ps.executeUpdate();
if (rowsAffected > 0) {
return true;
}
} catch (SQLException e) {
return false;
}
return false;
}

}
Loading
Loading