|
| 1 | +package itx.examples.jdbc.services; |
| 2 | + |
| 3 | +import itx.examples.jdbc.dto.UserData; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.PreparedStatement; |
| 9 | +import java.sql.ResultSet; |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +public class DataServiceImpl implements DataService { |
| 17 | + |
| 18 | + private static final Logger LOG = LoggerFactory.getLogger(DataServiceImpl.class); |
| 19 | + |
| 20 | + private final Connection connection; |
| 21 | + |
| 22 | + public DataServiceImpl(Connection connection) { |
| 23 | + this.connection = connection; |
| 24 | + disableAutocommit(); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Optional<UserData> findById(String id) throws DataServiceException { |
| 29 | + try { |
| 30 | + String query = "SELECT * FROM users WHERE id=?"; |
| 31 | + PreparedStatement statement = connection.prepareStatement(query); |
| 32 | + statement.setString(1, id); |
| 33 | + ResultSet resultSet = statement.executeQuery(); |
| 34 | + while (resultSet.next()) { |
| 35 | + String userId = resultSet.getString("id"); |
| 36 | + String email = resultSet.getString("email"); |
| 37 | + String password = resultSet.getString("password"); |
| 38 | + return Optional.of(new UserData(userId, email, password)); |
| 39 | + } |
| 40 | + this.connection.commit(); |
| 41 | + return Optional.empty(); |
| 42 | + } catch (SQLException e) { |
| 43 | + transactionRollback(); |
| 44 | + throw new DataServiceException(e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public UserData save(String email, String password) throws DataServiceException { |
| 50 | + try { |
| 51 | + String query = "INSERT INTO users ( id, email, password ) VALUES ( ?, ?, ? )"; |
| 52 | + String id = UUID.randomUUID().toString(); |
| 53 | + PreparedStatement statement = connection.prepareStatement(query); |
| 54 | + statement.setString(1, id); |
| 55 | + statement.setString(2, email); |
| 56 | + statement.setString(3, password); |
| 57 | + statement.execute(); |
| 58 | + this.connection.commit(); |
| 59 | + return new UserData(id, email, password); |
| 60 | + } catch (SQLException e) { |
| 61 | + transactionRollback(); |
| 62 | + throw new DataServiceException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Collection<UserData> findAll() throws DataServiceException { |
| 68 | + Collection<UserData> userDataCollection = new ArrayList<>(); |
| 69 | + try { |
| 70 | + String query = "SELECT * FROM users"; |
| 71 | + PreparedStatement statement = connection.prepareStatement(query); |
| 72 | + ResultSet resultSet = statement.executeQuery(); |
| 73 | + while (resultSet.next()) { |
| 74 | + String id = resultSet.getString("id"); |
| 75 | + String email = resultSet.getString("email"); |
| 76 | + String password = resultSet.getString("password"); |
| 77 | + userDataCollection.add(new UserData(id, email, password)); |
| 78 | + } |
| 79 | + this.connection.commit(); |
| 80 | + } catch (SQLException e) { |
| 81 | + transactionRollback(); |
| 82 | + throw new DataServiceException(e); |
| 83 | + } |
| 84 | + return userDataCollection; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void deleteById(String id) throws DataServiceException { |
| 89 | + try { |
| 90 | + if (findById(id).isEmpty()) { |
| 91 | + throw new DataServiceException(); |
| 92 | + } |
| 93 | + String query = "DELETE FROM users WHERE id=?"; |
| 94 | + PreparedStatement statement = connection.prepareStatement(query); |
| 95 | + statement.setString(1, id); |
| 96 | + statement.execute(); |
| 97 | + this.connection.commit(); |
| 98 | + } catch (SQLException e) { |
| 99 | + transactionRollback(); |
| 100 | + throw new DataServiceException(e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void update(UserData userData) throws DataServiceException { |
| 106 | + try { |
| 107 | + if (findById(userData.getId()).isEmpty()) { |
| 108 | + throw new DataServiceException(); |
| 109 | + } |
| 110 | + String query = "UPDATE users SET email=?, password=? WHERE id=?"; |
| 111 | + PreparedStatement statement = connection.prepareStatement(query); |
| 112 | + statement.setString(1, userData.getEmail()); |
| 113 | + statement.setString(2, userData.getPassword()); |
| 114 | + statement.setString(3, userData.getId()); |
| 115 | + statement.execute(); |
| 116 | + this.connection.commit(); |
| 117 | + } catch (SQLException e) { |
| 118 | + transactionRollback(); |
| 119 | + throw new DataServiceException(e); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private void transactionRollback() { |
| 124 | + try { |
| 125 | + this.connection.rollback(); |
| 126 | + } catch (SQLException e) { |
| 127 | + LOG.error("Transaction rollback failed", e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void disableAutocommit() { |
| 132 | + try { |
| 133 | + this.connection.setAutoCommit(false); |
| 134 | + } catch (SQLException e) { |
| 135 | + LOG.error("Autocommit = false failed", e); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments