BookmarkController.php

This handles the storing and retrieving of JSON bookmark data associated with a user's ID.

storeBookmark

public function storeBookmark(Request $request)
{
    // Get the JSON data from the request
    $jsonData = $request->input('jsonData');
    // Decode the JSON data into an associative array
    $jsonData = json_decode($jsonData, true);
    // Specify the directory where bookmark files will be stored
    $directory = 'Bookmark';
    // Construct the file path based on the user's ID
    $fileBookmark = $directory . '/' . $jsonData['USER_ID'] . '.json';
    // Convert the array back to pretty-printed JSON format
    $prettyPrintedJson = json_encode($jsonData, JSON_PRETTY_PRINT);
    // Open the file for writing
    $file = fopen($fileBookmark, 'w');
    // Check if the file was successfully opened
    if ($file) {
        // Write the pretty-printed JSON data to the file
        fwrite($file, $prettyPrintedJson);
        // Close the file
        fclose($file);
    }
    // Prepare a success response
    $response = [
        'SUCCESS' => 1,
    ];
    // Return the response
    return $response;
}

To explain what this storeBookmark method does:

  1. Receive Request Data:

    • The function receives a request object ($request) which is an instance of the Illuminate\Http\Request class.

  2. Extract JSON Data:

    • Extracts the JSON data from the request using $request->input('jsonData').

  3. Decode JSON:

    • Decodes the JSON data into an associative array using json_decode($jsonData, true).

  4. Specify Directory:

    • Specifies a directory named 'Bookmark' where bookmark files will be stored.

  5. Construct File Path:

    • Constructs the file path based on the user's ID obtained from the JSON data.

  6. Convert to Pretty-Printed JSON:

    • Converts the associative array back to pretty-printed JSON format using json_encode($jsonData, JSON_PRETTY_PRINT).

  7. Open File for Writing:

    • Opens a file for writing using fopen($fileBookmark, 'w').

  8. Check File Opening:

    • Checks if the file was successfully opened.

  9. Write to File:

    • If the file is open, writes the pretty-printed JSON data to the file using fwrite($file, $prettyPrintedJson).

  10. Close File:

    • Closes the file using fclose($file).

  11. Prepare Success Response:

    • Prepares a success response array with the key 'SUCCESS' set to 1.

  12. Return Response:

    • Returns the success response array as the response for the function.

getBookmark

public function getBookmark(Request $request)
{
    // Get the user ID from the request
    $userId = $request->input('userId');
    // Specify the directory where bookmark files are stored
    $directory = 'Bookmark';
    // Construct the file path based on the user's ID
    $fileBookmark = $directory . '/' . $userId . '.json';
    // Read the content of the bookmark file
    $jsonContent = file_get_contents($fileBookmark);
    // Decode the JSON content into an associative array
    $jsonData = json_decode($jsonContent, true);
    // Prepare a response with success status and bookmark data
    $response = [
        'SUCCESS' => 1,
        'DATA' => $jsonData['DATA']
    ];
    // Return the response
    return $response;
}

To explain what this getBookmark method does:

  1. Receive Request Data:

    • The function receives a request object ($request) which is an instance of the Illuminate\Http\Request class.

  2. Extract User ID:

    • Extracts the user ID from the request using $request->input('userId').

  3. Specify Directory:

    • Specifies a directory named 'Bookmark' where bookmark files are stored.

  4. Construct File Path:

    • Constructs the file path based on the user's ID obtained from the request.

  5. Read File Content:

    • Reads the content of the bookmark file using file_get_contents($fileBookmark).

  6. Decode JSON:

    • Decodes the JSON content into an associative array using json_decode($jsonContent, true).

  7. Prepare Response:

    • Prepares a response array with the key 'SUCCESS' set to 1 and includes the bookmark data from the decoded JSON using $jsonData['DATA'].

  8. Return Response:

    • Returns the response array as the response for the function.

Last updated