Source: user-data-access-comments.js

/**
 * Class for managing user data in local storage.
 */
class UserDataAccess {

    ///////////////////////////////////////////////
    // PRIVATE INSTANCE VARIABLES (start with #)
    ///////////////////////////////////////////////
  
    /** 
     * Dummy user data to populate the local storage database.
     * @type {Array<Object>}
     * @private
     */
    #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
    //////////////////////////////////
  
    /**
     * Initializes the UserDataAccess instance.
     * Ensures that local storage has the 'userData' key populated with dummy data if not already set.
     */
    constructor() {
      if (!localStorage.getItem("userData")) {
        localStorage.setItem("userData", JSON.stringify(this.#dummyData));
      }
    }
  
    //////////////////////////////////
    // PUBLIC METHODS
    //////////////////////////////////
  
    /**
     * Retrieves all users from local storage.
     * @returns {Array<Object>} Array of user objects.
     */
    getAllUsers() {
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      return users;
    }
  
    /**
     * Retrieves a user by their ID.
     * @param {number} id - The ID of the user to retrieve.
     * @returns {Object|null} The user object or null if not found.
     */
    getUserById(id) {
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      return users.find((u) => u.id == id) || null;
    }
  
    /**
     * Inserts a new user into the local storage database.
     * @param {Object} newUser - The new user object to insert.
     */
    insertUser(newUser) {
      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 local storage database.
     * @param {Object} updatedUser - The user object with updated data.
     */
    updateUser(updatedUser) {
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      const indexOfUserToUpdate = users.findIndex(u => updatedUser.id == u.id);
      if (indexOfUserToUpdate !== -1) {
        users[indexOfUserToUpdate] = updatedUser;
        localStorage.setItem("userData", JSON.stringify(users));
      }
    }
  
    /**
     * Deletes a user by their ID from the local storage database.
     * @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);
      if (indexOfUserToRemove !== -1) {
        users.splice(indexOfUserToRemove, 1);
        localStorage.setItem("userData", JSON.stringify(users));
      }
    }
  
    //////////////////////////////////
    // PRIVATE METHODS (start with #)
    //////////////////////////////////
  
    /**
     * Retrieves the maximum user ID from the current database.
     * @returns {number} The highest user ID.
     * @private
     */
    #getMaxId() {
      const str = localStorage.getItem("userData");
      const users = JSON.parse(str);
      return users.reduce((maxId, user) => Math.max(maxId, user.id), 0);
    }
}