feat: allow users to delete/create own profile

This commit is contained in:
2022-04-08 19:40:35 +01:00
parent 85962de188
commit dcbff0f52e
7 changed files with 342 additions and 142 deletions

View File

@@ -4,15 +4,56 @@
namespace App\Services;
use App\Exceptions\ProfileAlreadyExistsException;
use App\Exceptions\ProfileCreationFailedException;
use App\Exceptions\ProfileNotFoundException;
use App\Profile;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class ProfileService
{
/**
* Creates a new profile for the specified $targetUser.
*
* @param User $targetUser The user to create the profile for.
* @return bool
* @throws ProfileAlreadyExistsException
* @throws ProfileCreationFailedException
*/
public function createProfile(User $targetUser): Profile {
if (is_null($targetUser->profile)) {
$profile = Profile::create([
'profileShortBio' => 'Write a one-liner about you here!',
'profileAboutMe' => 'Tell us a bit about you.',
'socialLinks' => '{}',
'userID' => $targetUser->id,
]);
if (is_null($profile)) {
throw new ProfileCreationFailedException(__('Could not create profile! Please try again later.'));
}
Log::info('Created profile for new user', [
'userid' => $targetUser->id
]);
return $profile;
}
throw new ProfileAlreadyExistsException(__('Profile already exists!'));
}
/**
* Updates the user's profile.
*
* @throws ProfileNotFoundException
*/
public function updateProfile($userID, Request $request) {
@@ -47,4 +88,26 @@ class ProfileService
throw new ProfileNotFoundException("This profile does not exist.");
}
/**
* Delete specified user's profile
*
* @param User $targetUser
* @return bool
* @throws ProfileNotFoundException
*/
public function deleteProfile(User $targetUser): bool
{
if (!is_null($targetUser->profile)) {
Log::alert('Deleted user profile', [
'userid' => $targetUser->id
]);
return $targetUser->profile->delete();
}
throw new ProfileNotFoundException(__('Attempting to delete non-existant profile!'));
}
}