-
Notifications
You must be signed in to change notification settings - Fork 0
#23 - user profile setup #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6009c27
add api for profiles
KacperWalenga 152cc3b
Add avatars
KacperWalenga 960fc7b
code suggestions
KacperWalenga 9ec0bd8
update .env.ci
KacperWalenga 00251ac
tests
KacperWalenga b52fc36
tests
KacperWalenga e117983
tests
KacperWalenga 5b43c1c
tests
KacperWalenga 50e0f7b
Update app/Actions/Profile/UpdateProfileAction.php
KacperWalenga f4ffd46
Update app/Models/User.php
KacperWalenga aa24de1
Update database/migrations/0001_01_01_000000_create_users_table.php
KacperWalenga 5ee7dea
Update app/Actions/Avatars/ChangeAvatarAction.php
KacperWalenga 258dffd
fix birth_date
KacperWalenga 58e27a2
fix index profiles
KacperWalenga fa0dd53
Update app/Actions/Avatars/GetDefaultAvatarAction.php
KacperWalenga 4b32271
change gender type in db
KacperWalenga c3011ff
Merge remote-tracking branch 'origin/#23-User-Profile-Setup' into #23…
KacperWalenga 4844b9e
fix code style
KacperWalenga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Avatars; | ||
|
|
||
| use Illuminate\Http\UploadedFile; | ||
|
|
||
| class ChangeAvatarAction | ||
| { | ||
| public function execute(UploadedFile $uploadedFile, int $userId): bool | ||
| { | ||
| $uploadedFile->storeAs("", $userId . ".png", "avatars"); | ||
|
|
||
| return true; | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Avatars; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class DeleteAvatarAction | ||
| { | ||
| public function execute(int $userId): bool | ||
| { | ||
| $filename = $userId . ".png"; | ||
|
|
||
| if (Storage::disk("avatars")->exists($filename)) { | ||
| Storage::disk("avatars")->delete($filename); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Avatars; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class GetAvatarAction | ||
| { | ||
| public function execute(int $userId): ?string | ||
| { | ||
| $filename = $userId . ".png"; | ||
|
|
||
| if (Storage::disk("avatars")->exists($filename)) { | ||
| return Storage::disk("avatars")->get($filename); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Avatars; | ||
|
|
||
| use Identicon\Generator\SvgGenerator; | ||
| use Identicon\Identicon; | ||
|
|
||
| class GetDefaultAvatarAction | ||
| { | ||
| public function execute(int $userId): ?string | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $identicon = new Identicon(new SvgGenerator()); | ||
|
|
||
| return $identicon->getImageData((string)$userId, 300); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Profile; | ||
|
|
||
| use Strava\Models\User; | ||
|
|
||
| class UpdateProfileAction | ||
| { | ||
| public function execute(User $user, array $data): ?User | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $user->fill($data); | ||
| $user->save(); | ||
|
|
||
| return $user->fresh(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Enums; | ||
|
|
||
| enum Gender: string | ||
| { | ||
| case Male = "male"; | ||
| case Female = "female"; | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Helpers; | ||
|
|
||
| use Identicon\Identicon; | ||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class IdenticonHelper | ||
| { | ||
| private Identicon $identicon; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->identicon = new Identicon(); | ||
| } | ||
|
|
||
| public static function url(string|int $name): string | ||
| { | ||
| return url("/api/profiles/{$name}/avatar"); | ||
| } | ||
|
|
||
| public function create(string|int $filename, string $data): string | ||
| { | ||
| $image = $this->identicon->getImageData($data, 300); | ||
|
|
||
| return $this->save($filename, $image); | ||
| } | ||
|
|
||
| private function save(string|int $name, string $imageData): string | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $path = $name . ".png"; | ||
|
|
||
| Storage::disk("avatars")->put($path, $imageData); | ||
|
|
||
| return Storage::disk("avatars")->url($path); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Profile; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\Response; | ||
| use Strava\Actions\Avatars\ChangeAvatarAction; | ||
| use Strava\Actions\Avatars\DeleteAvatarAction; | ||
| use Strava\Actions\Avatars\GetAvatarAction; | ||
| use Strava\Actions\Avatars\GetDefaultAvatarAction; | ||
| use Strava\Actions\Profile\UpdateProfileAction; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Strava\Http\Requests\ChangeAvatarRequest; | ||
| use Strava\Http\Requests\UpdateProfileRequest; | ||
| use Strava\Http\Resources\UserResource; | ||
|
|
||
| class ProfileController extends Controller | ||
| { | ||
| public function update(UpdateProfileRequest $request, UpdateProfileAction $updateProfileAction): UserResource | ||
| { | ||
| $validated = $request->validated(); | ||
|
|
||
| $user = $request->user(); | ||
| $updated = $updateProfileAction->execute($user, $validated); | ||
|
|
||
| return UserResource::make($updated); | ||
| } | ||
|
|
||
| public function show(Request $request): UserResource | ||
| { | ||
| $user = $request->user(); | ||
|
|
||
| return UserResource::make($user); | ||
| } | ||
|
|
||
| public function changeAvatar(ChangeAvatarRequest $request, ChangeAvatarAction $changeAvatarAction): UserResource | ||
| { | ||
| $user = $request->user(); | ||
|
|
||
| $changeAvatarAction->execute( | ||
| $request->file("avatar"), | ||
| $user->id, | ||
| ); | ||
|
|
||
| return UserResource::make($user); | ||
| } | ||
|
|
||
| public function getAvatar(int $userId, GetAvatarAction $getAvatarAction, GetDefaultAvatarAction $getDefaultAvatarAction): Response | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $avatar = $getAvatarAction->execute($userId); | ||
|
|
||
| if ($avatar) { | ||
| return response($avatar) | ||
| ->header("Content-Type", "image/png") | ||
| ->header("Cache-Control", "public, max-age=31536000"); | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $defaultAvatar = $getDefaultAvatarAction->execute($userId); | ||
|
|
||
| return response($defaultAvatar) | ||
| ->header("Content-Type", "image/svg+xml") | ||
| ->header("Cache-Control", "public, max-age=86400"); | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function deleteAvatar(Request $request, DeleteAvatarAction $deleteProfileAction): UserResource | ||
| { | ||
| $user = $request->user(); | ||
|
|
||
| $deleteProfileAction->execute($user->id); | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return UserResource::make($user); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Profile; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Strava\Http\Resources\UserResource; | ||
| use Strava\Models\User; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class ProfilesController extends Controller | ||
| { | ||
| public function index(): AnonymousResourceCollection | ||
| { | ||
| $users = User::query()->select([ | ||
| "id", | ||
| "name", | ||
| "first_name", | ||
| "last_name", | ||
| "birth_date", | ||
| "height", | ||
| "weight", | ||
| "gender", | ||
| ])->paginate(10); | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return UserResource::collection($users); | ||
| } | ||
|
|
||
| public function show(int $id): JsonResponse | ||
| { | ||
| $user = User::query()->find($id); | ||
|
|
||
| if ($user === null) { | ||
| return response()->json([], Response::HTTP_NOT_FOUND); | ||
| } | ||
|
|
||
| return response()->json(UserResource::make($user), Response::HTTP_OK); | ||
| } | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class ChangeAvatarRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
| public function authorize(): bool | ||
| { | ||
| return auth()->check(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "avatar" => ["required", "image", "mimes:png", "max:2048"], // max 2MB | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests; | ||
|
|
||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Validation\Rules\Enum; | ||
| use Strava\Enums\Gender; | ||
|
|
||
| class UpdateProfileRequest extends FormRequest | ||
| { | ||
| public function authorize(): bool | ||
| { | ||
| return auth()->check(); | ||
| } | ||
|
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "first_name" => ["nullable", "string", "max:255"], | ||
| "last_name" => ["nullable", "string", "max:255"], | ||
| "birth_date" => ["nullable", "date", "before:today"], | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "height" => ["nullable", "integer", "min:50", "max:300"], | ||
| "weight" => ["nullable", "numeric", "min:10", "max:300"], | ||
| "gender" => ["nullable", new Enum(Gender::class)], | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.