-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathJdbcAccountRepository.java
More file actions
103 lines (88 loc) · 3.49 KB
/
JdbcAccountRepository.java
File metadata and controls
103 lines (88 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.example.repo;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Optional;
public class JdbcAccountRepository implements AccountRepository {
private final DataSource ds;
public JdbcAccountRepository(DataSource ds) {
this.ds = ds;
}
@Override
public Optional<Long> authenticate(String name, String password) {
String sql = "SELECT user_id FROM account WHERE name = ? AND password = ?";
try (Connection c = ds.getConnection();
PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, password);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) return Optional.of(rs.getLong(1));
return Optional.empty();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public long createAccount(String firstName, String lastName, String ssn, String password) {
String name = makeUsername(firstName, lastName);
String sql = "INSERT INTO account (name, first_name, last_name, ssn, password) VALUES (?, ?, ?, ?, ?)";
try (Connection c = ds.getConnection();
PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, name);
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, ssn);
ps.setString(5, password);
ps.executeUpdate();
try (ResultSet keys = ps.getGeneratedKeys()) {
if (keys.next()) return keys.getLong(1);
}
try (PreparedStatement ps2 = c.prepareStatement("SELECT user_id FROM account WHERE name = ?")) {
ps2.setString(1, name);
try (ResultSet rs = ps2.executeQuery()) {
if (rs.next()) return rs.getLong(1);
}
}
throw new IllegalStateException("Could not get new user id");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void updatePassword(long userId, String newPassword) {
String sql = "UPDATE account SET password = ? WHERE user_id = ?";
try (Connection c = ds.getConnection();
PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, newPassword);
ps.setLong(2, userId);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(long userId) {
String sql = "DELETE FROM account WHERE user_id = ?";
try (Connection c = ds.getConnection();
PreparedStatement ps = c.prepareStatement(sql)) {
ps.setLong(1, userId);
ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static String makeUsername(String first, String last) {
String f = first == null ? "" : first.trim();
String l = last == null ? "" : last.trim();
return cap(take3(f)) + cap(take3(l));
}
private static String take3(String s) {
if (s.isEmpty()) return "XXX";
return s.length() <= 3 ? s : s.substring(0, 3);
}
private static String cap(String s) {
if (s.isEmpty()) return s;
String low = s.toLowerCase();
return Character.toUpperCase(low.charAt(0)) + low.substring(1);
}
}