Source: user-data-access-ai.js

/**
 * A class to manage user data in local storage.
 * It allows retrieving, inserting, updating, and deleting users, as well as handling data persistence.
 * 
 * @class
 */
class UserDataAccess {

    ///////////////////////////////////////////////
    // PRIVATE INSTANCE VARIABLES (start with #)
    ///////////////////////////////////////////////

    /**
     * The dummy data used to populate the local storage when there is no existing user data.
     * 
     * @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
    ////////////////////////////////////

    /**
     * Initializes the UserDataAccess class. It checks if user data exists in localStorage, 
     * and if not, it creates and populates it with the dummy data.
     */
    constructor(){
        if(!localStorage.getItem("userData")){
            localStorage.setItem("userData", JSON.stringify(this.#dummyData));
        }
    }

    ////////////////////////////////////
    // PUBLIC METHODS
    ////////////////////////////////////

    /**
     * Retrieves all the users from localStorage.
     * 
     * @returns {Array<{id: number, firstName: string, lastName: string, email: string}>} List of all users.
     */
    getAllUsers(){
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        return users;
    }

    /**
     * Retrieves a user by their ID from localStorage.
     * 
     * @param {number} id - The ID of the user to retrieve.
     * @returns {Object|null} The user object, or null if no user is found with the provided ID.
     */
    getUserById(id){
        const str = localStorage.getItem("userData");
        const users = JSON.parse(str);
        const user = users.find((u) => u.id == id);
        return user;
    }

    /**
     * Inserts a new user into localStorage. The new user will have a unique ID generated by the system.
     * 
     * @param {Object} newUser - The new user to insert.
     * @param {string} newUser.firstName - The first name of the new user.
     * @param {string} newUser.lastName - The last name of the new user.
     * @param {string} newUser.email - The email of the new user.
     */
    insertUser(newUser){
        // We really should validate newUser before inserting it!
        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 localStorage.
     * 
     * @param {Object} updatedUser - The updated user object.
     * @param {number} updatedUser.id - The ID of the user being updated.
     * @param {string} updatedUser.firstName - The first name of the updated user.
     * @param {string} updatedUser.lastName - The last name of the updated user.
     * @param {string} updatedUser.email - The email of the updated user.
     */
    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 localStorage by their 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 #)
    ////////////////////////////////////

    /**
     * Retrieves the highest user ID from localStorage.
     * 
     * @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;
    }

}