]> WPIA git - gigi.git/commitdiff
Add api for adding email addresses.
authorFelix Dörre <felix@dogcraft.de>
Wed, 23 Jul 2014 01:04:23 +0000 (03:04 +0200)
committerFelix Dörre <felix@dogcraft.de>
Thu, 24 Jul 2014 23:44:27 +0000 (01:44 +0200)
src/org/cacert/gigi/EmailAddress.java

index fda01ed06b69de28005be3555b2760b283a199e7..646b09550b78d089b5b86756018815fe3366053f 100644 (file)
@@ -3,14 +3,13 @@ package org.cacert.gigi;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-
 import org.cacert.gigi.database.DatabaseConnection;
 
 public class EmailAddress {
-       String address;
-       int id;
-       User owner;
-       String hash = null;
+       private String address;
+       private int id;
+       private User owner;
+       private String hash = null;
 
        private EmailAddress(int id) throws SQLException {
                PreparedStatement ps = DatabaseConnection.getInstance().prepare(
@@ -28,6 +27,29 @@ public class EmailAddress {
                rs.close();
        }
 
+       public EmailAddress(String address, User owner, String hash) {
+               this.address = address;
+               this.owner = owner;
+               this.hash = hash;
+       }
+
+       public void insert() {
+               if (id != 0) {
+                       throw new IllegalStateException("already inserted.");
+               }
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "INSERT INTO `email` SET memid=?, hash=?, email=?");
+                       ps.setInt(1, owner.getId());
+                       ps.setString(2, hash);
+                       ps.setString(3, address);
+                       ps.execute();
+                       id = DatabaseConnection.lastInsertId(ps);
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+       }
+
        public int getId() {
                return id;
        }