const db = require('../db/db');
const sql = require('../db/sqlProvider').users;
const User = require('../models/User');

class UserDAO {
  static all() {
    return db.map(sql.all, [], row => new User(row));
  }
  static where(queries) {
    // TODO support multiple queries
  }
  static find(id) {
    return db.one(sql.find, ['id', id])
             .then(row => new User(row))
             .catch(err => err);
  }
  static findBy(keyValue) {
    const key = Object.keys(keyValue)[0];
    const value = keyValue[key];
    return db.one(sql.find, [key, value])
             .then(row => new User(row))
             .catch(err => err);
  }
  static save({ email, password, username }) {
    return db.one(sql.create, [email, password, username])
             .then(row => new User(row))
             .catch(err => err);
  }
  static update({ id, email, username, firstName, lastName, bio, password }) {
    return db.one(sql.save, [id, email, username, firstName, lastName, bio, password])
             .then(row => new User(row))
             .catch(err => err);
  }
  static delete(id) {
    return db.none(sql.delete, [id]);
  }
}

module.exports = UserDAO;

@@ -513,16 +513,106 @@
 e%5Bkey%5D;%0A
+    const uniqueKeys = %5B'id', 'email', 'username'%5D;%0A    if (uniqueKeys.includes(key)) %7B%0A  
     retu
@@ -650,32 +650,34 @@
 %5D)%0A             
+  
 .then(row =%3E new
@@ -680,32 +680,138 @@
  new User(row))%0A
+               .catch(err =%3E err);%0A    %7D%0A    return db.map(sql.where, %5Bkey, value%5D, row =%3E new User(row))%0A
              .ca

return one for unique columns, array for non-unique