ProfileController.php

This handles user profile information, including fetching and updating user profiles, and retrieving counts of bookmarks, published maps, and maps associated with a user.

getUserProfile

public function getUserProfile(Request $request)
{
    // Get the user ID from the request input
    $userId = $request->input('userId');
    // Retrieve the user profile using the User ID from the UserProfile model
    $userProfile = UserProfile::where('user_id', $userId)
        ->first();
    // Retrieve the user account using the User ID from the UserAccount model
    $userAccount = UserAccount::where('id', $userId)
        ->first();
    // Check if both user profile and user account exist
    if (isset($userProfile) && isset($userAccount)) {
        // If both exist, create a success response with user information
        $response = [
            'SUCCESS' => 1,
            'USERNAME' => $userAccount->username,
            'NAME' => $userProfile->name,
            'EMAIL' => $userAccount->email,
            'COMPANY' => $userProfile->company,
        ];
        return $response;
    } else {
        // If either user profile or user account is not found, create a failure response
        $response = [
            'SUCCESS' => 0,
        ];
        return $response;
    }
}

To explain what this getUserProfile method does:

  1. Method Declaration:

    • Declares the function getUserProfile in the ProfileController class.

  2. Request Handling:

    • Accepts a Request object to handle HTTP requests.

    • Retrieves the user ID from the request.

  3. Retrieve User Profile:

    • Uses the UserProfile model to fetch the user profile based on the provided user ID.

  4. Retrieve User Account:

    • Uses the UserAccount model to fetch the user account based on the same user ID.

  5. Check if Both Exist:

    • Checks if both the user profile and user account exist.

  6. Create Success Response:

    • If both exist, creates a success response with user information.

  7. Return the Response:

    • Returns the response containing user information.

  8. Handle Case Where Either Profile or Account is Not Found:

    • If either the user profile or user account is not found, creates a failure response.

updateUserProfile

public function updateUserProfile(Request $request)
{
    // Get user ID, name, and company from the request input
    $userId = $request->input('userId');
    $name = $request->input('name');
    $company = $request->input('company');
    // Retrieve the user profile using the User ID
    $userProfile = UserProfile::where('user_id', $userId)->first();
    // Update user profile information
    $userProfile->name = strval($name);
    $userProfile->company = strval($company);
    $userProfile->save();
    // Create a success response
    $response = [
        'SUCCESS' => 1,
    ];
    return $response;
}

To explain what this updateUserProfile method does:

  1. Method Declaration:

    • Declares the function updateUserProfile in the ProfileController class.

  2. Request Handling:

    • Accepts a Request object to handle HTTP requests.

    • Retrieves user ID, name, and company from the request input.

  3. Retrieve User Profile:

    • Uses the UserProfile model to fetch the user profile based on the provided user ID.

  4. Update User Profile Information:

    • Updates the user profile information with the provided name and company.

  5. Create Success Response:

    • Creates a success response indicating that the user profile has been updated.

  6. Return the Response:

    • Returns the success response.

getUserInfo

public function getUserInfo(Request $request)
{
    // Get the user ID from the request input
    $userId = $request->input('userId');
    // Bookmark
    $directory = 'Bookmark';
    $fileBookmark = $directory . '/' . $userId . '.json';
    // Read the content of the bookmark file and decode it as JSON
    $jsonContent = file_get_contents($fileBookmark);
    $jsonData = json_decode($jsonContent, true);
    // Count the number of bookmarks
    $countBookmark = count($jsonData['DATA']);
    // Bookmark
    // PublishedMap
    $directory = 'PublishedMap';
    // Get a list of files in the PublishedMap directory, excluding '.' and '..'
    $files = scandir($directory);
    $files = array_diff($files, ['.', '..']);
    $jsonData = [];
    // Iterate through each file in the PublishedMap directory
    foreach ($files as $file) {
        $filePath = $directory . '/' . $file;
        // Read the content of the file and decode it as JSON if successful
        $fileContent = file_get_contents($filePath);
        if ($fileContent !== false) {
            $jsonData[] = json_decode($fileContent, true);
        }
    }
    // Count the number of published maps associated with the user
    $countPublishedMap = 0;
    foreach ($jsonData as $json) {
        if ($json['USER_ID'] == $userId) {
            $countPublishedMap = $countPublishedMap + 1;
        }
    }
    // PublishedMap
    // Map
    $directory = 'Map';
    $fileMap = $directory . '/' . $userId . '.json';
    // Read the content of the map file and decode it as JSON
    $jsonContent = file_get_contents($fileMap);
    $jsonData = json_decode($jsonContent, true);
    // Count the number of maps
    $countMap = count($jsonData['DATA']);
    // Map
    // Create a response with the counts of bookmarks, published maps, and maps
    $response = [
        'SUCCESS' => 1,
        'BOOKMARK_COUNT' => $countBookmark,
        'PUBLISHED_MAP_COUNT' => $countPublishedMap,
        'MAP_COUNT' => $countMap
    ];
    return $response;
}

To explain what this getUserInfo method does:

  1. Method Declaration:

    • Declares the function getUserInfo in the ProfileController class.

  2. Request Handling:

    • Accepts a Request object to handle HTTP requests.

    • Retrieves the user ID from the request input.

  3. Bookmark Handling:

    • Defines a directory named 'Bookmark' and constructs the file path for the user's bookmark file.

    • Reads the content of the bookmark file, decodes it as JSON, and counts the number of bookmarks.

  4. PublishedMap Handling:

    • Defines a directory named 'PublishedMap' and gets a list of files.

    • Reads the content of each file, decodes it as JSON, and counts the number of published maps associated with the user.

  5. Map Handling:

    • Defines a directory named 'Map' and constructs the file path for the user's map file.

    • Reads the content of the map file, decodes it as JSON, and counts the number of maps.

  6. Create Response:

    • Creates a response with counts of bookmarks, published maps, and maps.

  7. Return the Response:

    • Returns the response containing counts of bookmarks, published maps, and maps.

Last updated