From 5584840145a6aab6028162b7b349a897dff427bc Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 2 Nov 2017 14:58:30 +0100 Subject: [PATCH] Insert a new user in local database --- .../client/data/UsersInfo/UserInfo.java | 10 +- .../data/UsersInfo/UsersInfosDbHelper.java | 102 ++++++++++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java index 2b00fb5..2d412c5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UserInfo.java @@ -15,7 +15,7 @@ public class UserInfo { private int id; private String firstName; private String lastName; - private String imageURL; + private String accountImageURL; /** * Set the ID of the user @@ -85,8 +85,8 @@ public class UserInfo { * * @param imageURL The URL of the image */ - public void setImageURL(String imageURL) { - this.imageURL = imageURL; + public void setAccountImageURL(String imageURL) { + this.accountImageURL = imageURL; } /** @@ -94,7 +94,7 @@ public class UserInfo { * * @return The image URL of the account of the user */ - public String getImageURL() { - return imageURL; + public String getAcountImageURL() { + return accountImageURL; } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java index 1dc89fa..3a5d455 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/UsersInfo/UsersInfosDbHelper.java @@ -1,5 +1,6 @@ package org.communiquons.android.comunic.client.data.UsersInfo; +import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -68,6 +69,107 @@ public class UsersInfosDbHelper { //Close cursor c.close(); + //Close the database + db.close(); + return number_entries > 0; } + + /** + * Insert a user in the database + * + * @param user The user to insert in the database + * @return -1 in case of failure / the id of the new entry else + */ + public int insert(UserInfo user){ + + //Get a database with write access + SQLiteDatabase db = dbHelper.getWritableDatabase(); + + //Prepare the insertion + ContentValues newValues = new ContentValues(); + newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ID, user.getId()); + newValues.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, user.getFirstName()); + newValues.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, user.getLastName()); + newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, user.getAcountImageURL()); + + //Insert it + long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues); + + //Close the database + db.close(); + + return (int) newRowId; + } + + /** + * Get a user from the database + * + * @param userID The ID of the user to retrieve + * @return UserInfo about requested user / null in case of filaure + */ + public UserInfo get(int userID){ + + UserInfo result; + + //Open database for access + SQLiteDatabase db = dbHelper.getReadableDatabase(); + + //Prepare the request on the database + String[] requestedFields = { + UsersInfoSchema.COLUMN_NAME_USER_ID, + UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, + UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, + UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE + }; + + //Set the conditions of the request + String selection = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?"; + String[] selectionArgs = { ""+userID }; + + //Sort order + String sortOrder = UsersInfoSchema._ID; + + //Perform the request + Cursor c = db.query( + UsersInfoSchema.TABLE_NAME, + requestedFields, + selection, + selectionArgs, + null, + null, + sortOrder + ); + + //Check for errors + if(c.getCount() < 1) + result = null; + else { + + //Initialize User object + result = new UserInfo(); + + c.moveToFirst(); + + //Extract the information and record them + result.setId( + c.getInt( + c.getColumnIndexOrThrow( + UsersInfoSchema.COLUMN_NAME_USER_ID + ) + ) + ); + + + //TODO : finish extraction + } + + //Close the cursor + c.close(); + + //Close the database + db.close(); + + return result; + } }