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
getUserProfilepublic 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:
Method Declaration:
Declares the function
getUserProfilein theProfileControllerclass.
Request Handling:
Accepts a
Requestobject to handle HTTP requests.Retrieves the user ID from the request.
Retrieve User Profile:
Uses the
UserProfilemodel to fetch the user profile based on the provided user ID.
Retrieve User Account:
Uses the
UserAccountmodel to fetch the user account based on the same user ID.
Check if Both Exist:
Checks if both the user profile and user account exist.
Create Success Response:
If both exist, creates a success response with user information.
Return the Response:
Returns the response containing user information.
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
updateUserProfilepublic 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:
Method Declaration:
Declares the function
updateUserProfilein theProfileControllerclass.
Request Handling:
Accepts a
Requestobject to handle HTTP requests.Retrieves user ID, name, and company from the request input.
Retrieve User Profile:
Uses the
UserProfilemodel to fetch the user profile based on the provided user ID.
Update User Profile Information:
Updates the user profile information with the provided name and company.
Create Success Response:
Creates a success response indicating that the user profile has been updated.
Return the Response:
Returns the success response.
getUserInfo
getUserInfopublic 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:
Method Declaration:
Declares the function
getUserInfoin theProfileControllerclass.
Request Handling:
Accepts a
Requestobject to handle HTTP requests.Retrieves the user ID from the request input.
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.
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.
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.
Create Response:
Creates a response with counts of bookmarks, published maps, and maps.
Return the Response:
Returns the response containing counts of bookmarks, published maps, and maps.
Last updated