Example #1
0
  /**
   * createUser Creates a new user in the database.
   *
   * @param newUser The user instance to be inserted.
   * @return Returns the user inserted from the database, or null if error.
   */
  @Override
  public User createUser(User newUser) {
    // format the string
    String query = "INSERT INTO Users(Email, FacebookID, Username, Fullname, RoleID, Status)";
    query += " VALUES ('%1$s', '%2$s', '%3$s', '%4$s', %5$d, %6$d)";

    query =
        String.format(
            query,
            newUser.getEmail(),
            newUser.getFacebookID(),
            newUser.getUsername(),
            newUser.getFullname(),
            newUser.getRoleID(),
            (newUser.getStatus() ? 0 : 1));

    // if everything worked, inserted id will have the identity key
    // or primary key
    int insertedId = DataService.executeCreate(query);
    if (insertedId > 0) {
      return getUserById(insertedId);
    }

    return null;
  }
Example #2
0
  /**
   * updateUser Updates a user in the database.
   *
   * @param newUser The user instance to be updated.
   * @return Returns true or false if the user was updated or not.
   */
  @Override
  public boolean updateUser(User newUser) {
    // format the string
    String query =
        "UPDATE Users SET Email = '%1$s', FacebookID = '%2$s', Username = '******', RoleID = %4$d, Status = %5$d, Fullname = '%7$s' WHERE UserID = %6$d";

    query =
        String.format(
            query,
            newUser.getEmail(),
            newUser.getFacebookID(),
            newUser.getUsername(),
            newUser.getRoleID(),
            (newUser.getStatus() ? 0 : 1),
            newUser.getUserID(),
            newUser.getFullname());

    // if everything worked, inserted id will have the identity key
    // or primary key
    return DataService.executeUpdate(query);
  }