/**
* Class representing user data access for managing user information in local storage.
*/
class UserDataAccess {
///////////////////////////////////////////////
// PRIVATE INSTANCE VARIABLES (start with #)
///////////////////////////////////////////////
/**
* A dummy dataset used to populate the localStorage database initially.
* @private
* @type {Array<{id: number, firstName: string, lastName: string, email: string}>}
*/
#dummyData = [
{id: 1, firstName: "Jane", lastName: "Doe", email: "jdoe@acme.com"},
{id: 2, firstName: "Tony", lastName: "Thompsom", email: "tony@acme.com"},
{id: 3, firstName: "Jesse", lastName: "Jones", email: "jesse@acme.com"}
];
//////////////////////////////////
// CONSTRUCTOR
//////////////////////////////////
/**
* Creates an instance of UserDataAccess.
* Initializes the localStorage with dummy data if no user data exists.
*/
constructor() {
// check to see if 'userData' key is already in the localStorage database
// if not, then create it, and populate it with the dummy data
if (!localStorage.getItem("userData")) {
localStorage.setItem("userData", JSON.stringify(this.#dummyData));
}
}
//////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////
/**
* Retrieves all users from localStorage.
* @returns {Array<{id: number, firstName: string, lastName: string, email: string}>} An array of user objects.
*/
getAllUsers() {
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
return users;
}
/**
* Retrieves a user by their unique ID from localStorage.
* @param {number} id - The ID of the user to retrieve.
* @returns {Object|null} The user object if found, or null if not found.
*/
getUserById(id) {
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
const user = users.find((u) => u.id == id);
return user || null;
}
/**
* Inserts a new user into the localStorage database.
* @param {Object} newUser - The new user object to insert.
* @param {number} newUser.id - The unique ID for the user (will be set inside the method).
* @param {string} newUser.firstName - The first name of the user.
* @param {string} newUser.lastName - The last name of the user.
* @param {string} newUser.email - The email of the user.
*/
insertUser(newUser) {
// We really should validate newUser before inserting it!
// Set the new user's id:
newUser.id = this.#getMaxId() + 1;
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
users.push(newUser);
localStorage.setItem("userData", JSON.stringify(users));
}
/**
* Updates an existing user in the localStorage database.
* @param {Object} updatedUser - The user object with updated information.
* @param {number} updatedUser.id - The unique ID of the user to update.
* @param {string} updatedUser.firstName - The updated first name.
* @param {string} updatedUser.lastName - The updated last name.
* @param {string} updatedUser.email - The updated email.
*/
updateUser(updatedUser) {
// again, we should validate updatedUser before putting it in the database
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
const indexOfUserToUpdate = users.findIndex(u => updatedUser.id == u.id);
users[indexOfUserToUpdate] = updatedUser;
localStorage.setItem("userData", JSON.stringify(users));
}
/**
* Deletes a user from the localStorage database by their unique ID.
* @param {number} id - The ID of the user to delete.
*/
deleteUser(id) {
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
const indexOfUserToRemove = users.findIndex(u => id == u.id);
users.splice(indexOfUserToRemove, 1);
localStorage.setItem("userData", JSON.stringify(users));
}
//////////////////////////////////
// PRIVATE METHODS (start with #)
//////////////////////////////////
/**
* Gets the highest user ID from the localStorage database.
* @private
* @returns {number} The maximum user ID.
*/
#getMaxId() {
const str = localStorage.getItem("userData");
const users = JSON.parse(str);
let maxId = 0;
for (let x = 0; x < users.length; x++) {
if (users[x].id > maxId) {
maxId = users[x].id;
}
}
return maxId;
}
}