77import ru .javaops .webapp .sql .ConnectionFactory ;
88
99import java .sql .*;
10+ import java .util .ArrayList ;
1011import java .util .List ;
1112
1213public class SqlStorage implements IStorage {
@@ -18,8 +19,8 @@ public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
1819
1920 @ Override
2021 public void clear () {
21- try (Connection conn = connectionFactory .getConnection ();
22- PreparedStatement ps = conn .prepareStatement ("DELETE FROM resume" )){
22+ try (Connection conn = connectionFactory .getConnection ();
23+ PreparedStatement ps = conn .prepareStatement ("DELETE FROM resume" )) {
2324 ps .execute ();
2425 } catch (SQLException e ) {
2526 throw new StorageException (e );
@@ -29,10 +30,10 @@ public void clear() {
2930 @ Override
3031 public Resume get (String uuid ) {
3132 try (Connection conn = connectionFactory .getConnection ();
32- PreparedStatement ps = conn .prepareStatement ("SELECT * FROM resume r WHERE r.uuid = ?" )) {
33+ PreparedStatement ps = conn .prepareStatement ("SELECT * FROM resume r WHERE r.uuid = ?" )) {
3334 ps .setString (1 , uuid );
3435 ResultSet rs = ps .executeQuery ();
35- if (!rs .next ()) {
36+ if (!rs .next ()) {
3637 throw new NotExistStorageException (uuid );
3738 }
3839 return new Resume (uuid , rs .getString ("full_name" ));
@@ -43,15 +44,16 @@ public Resume get(String uuid) {
4344
4445 @ Override
4546 public void update (Resume resume ) {
46- if (get (resume .getUuid ()).equals (resume )) {
47- try (Connection conn = connectionFactory .getConnection ();
48- PreparedStatement ps = conn .prepareStatement ("UPDATE resume SET full_name = ? WHERE uuid = ?" )) {
49- ps .setString (1 , resume .getFullName ());
50- ps .setString (2 , resume .getUuid ());
51- ps .executeUpdate ();
52- } catch (SQLException e ) {
53- throw new StorageException (e );
47+ try (Connection conn = connectionFactory .getConnection ();
48+ PreparedStatement ps = conn .prepareStatement ("UPDATE resume SET full_name = ? WHERE uuid = ?" )) {
49+ ps .setString (1 , resume .getFullName ());
50+ ps .setString (2 , resume .getUuid ());
51+ if (ps .executeUpdate () == 0 ) {
52+ throw new NotExistStorageException (resume .getUuid ());
5453 }
54+ } catch (SQLException e ) {
55+ throw new StorageException (e );
56+
5557 }
5658 }
5759
@@ -69,20 +71,32 @@ public void save(Resume resume) {
6971
7072 @ Override
7173 public void delete (String uuid ) {
72- if (get (uuid ).getUuid ().equals (uuid )) {
73- try (Connection conn = connectionFactory .getConnection ();
74- PreparedStatement ps = conn .prepareStatement ("DELETE FROM resume r WHERE r.uuid = ?" )) {
75- ps .setString (1 , uuid );
76- ps .executeUpdate ();
77- } catch (SQLException e ) {
74+ try (Connection conn = connectionFactory .getConnection ();
75+ PreparedStatement ps = conn .prepareStatement ("DELETE FROM resume r WHERE r.uuid = ?" )) {
76+ ps .setString (1 , uuid );
77+ if (ps .executeUpdate () == 0 ) {
7878 throw new NotExistStorageException (uuid );
7979 }
80+ } catch (SQLException e ) {
81+ throw new StorageException (e );
8082 }
8183 }
8284
8385 @ Override
8486 public List <Resume > getAllSorted () {
85- return null ;
87+ try (Connection conn = connectionFactory .getConnection ();
88+ PreparedStatement ps = conn .prepareStatement ("SELECT uuid, full_name FROM resume ORDER BY uuid ASC " )) {
89+ ResultSet rs = ps .executeQuery ();
90+ List <Resume > resumes = new ArrayList <>();
91+ while (rs .next ()) {
92+ String uuid = rs .getString (1 ).replaceAll ("\\ s" , "" );
93+ String fullName = rs .getString (2 );
94+ resumes .add (new Resume (uuid , fullName ));
95+ }
96+ return resumes ;
97+ } catch (SQLException e ) {
98+ throw new StorageException (e );
99+ }
86100 }
87101
88102 @ Override
0 commit comments