From 51a4dae912d99f401453fb6be48172987611b7f6 Mon Sep 17 00:00:00 2001 From: danielhe4rt Date: Tue, 3 Sep 2024 13:33:55 -0300 Subject: [PATCH] feat: new endpoint with single field patch --- .../Api/V1/AuthenticatedUserController.php | 26 +++++++++ app/Http/Requests/SettingsRequest.php | 28 ++++++++++ public/css/app/custom-stylesheet.css | 14 ----- routes/api.php | 2 + storage/app/public/effects.css | 14 ----- .../V1/AuthenticatedUserControllerTest.php | 54 +++++++++++++++++++ 6 files changed, 110 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Api/V1/AuthenticatedUserController.php b/app/Http/Controllers/Api/V1/AuthenticatedUserController.php index b3c174c..74263f0 100644 --- a/app/Http/Controllers/Api/V1/AuthenticatedUserController.php +++ b/app/Http/Controllers/Api/V1/AuthenticatedUserController.php @@ -58,4 +58,30 @@ public function putSettings(SettingsRequest $request): JsonResponse return response()->json($settings); } + + public function patchSettings(SettingsRequest $request): JsonResponse + { + $validatedSettings = $request->validated(); + + $request + ->user() + ->settings() + ->updateOrCreate([ + 'channel_id' => $validatedSettings['channel_id'], + ], $validatedSettings); + + /** @var User $user */ + $user = $request + ->user() + ->refresh(); + + /** @var Settings $settings */ + $settings = $request->user()->settings()->where('channel_id', '=', $validatedSettings['channel_id']) + ->with('occupation', 'color', 'effect') + ->first(); + + $this->client->updateUser($user, $settings); + + return response()->json($settings); + } } diff --git a/app/Http/Requests/SettingsRequest.php b/app/Http/Requests/SettingsRequest.php index 5553a2d..195edff 100644 --- a/app/Http/Requests/SettingsRequest.php +++ b/app/Http/Requests/SettingsRequest.php @@ -10,6 +10,34 @@ public function rules(): array { $acceptedPronouns = collect(config('extension.pronouns'))->pluck('slug')->join(','); + return match ($this->method()) { + 'PUT' => $this->putValidation($acceptedPronouns), + 'PATCH' => $this->patchValidation($acceptedPronouns) + }; + } + + /** + * @return array[] + */ + public function patchValidation(string $acceptedPronouns): array + { + return [ + 'occupation_id' => ['exists:occupations,id'], + 'channel_id' => ['string'], + 'enabled' => ['boolean'], + 'color_id' => ['exists:settings_colors,id'], + 'effect_id' => ['exists:settings_effects,id'], + 'timezone' => ['string'], + 'locale' => ['string'], + 'pronouns' => ['string', 'in:'.$acceptedPronouns], + ]; + } + + /** + * @return array[] + */ + public function putValidation(string $acceptedPronouns): array + { return [ 'occupation_id' => ['exists:occupations,id'], 'channel_id' => ['required', 'string'], diff --git a/public/css/app/custom-stylesheet.css b/public/css/app/custom-stylesheet.css index 7e2778b..ab89cba 100644 --- a/public/css/app/custom-stylesheet.css +++ b/public/css/app/custom-stylesheet.css @@ -19,20 +19,6 @@ background: linear-gradient(90deg, #FF6D1B, #FFEE55, #5BFF89, #4D8AFF, #6B5FFF, #FF64F9, #FF6565); -webkit-background-clip: text; -webkit-text-fill-color: transparent; - -webkit-animation: gradient-default-animate 3s infinite linear; - -moz-animation: gradient-default-animate 3s infinite linear; - background-size: 200%; - animation: gradient-default-animate 3s infinite linear; -} - -@-webkit-keyframes gradient-default-animate { - 0% {background-position: 0;} - 100% {background-position: 200%;} -} - .gradient-netherlands { - background: linear-gradient(90deg, #21468B, #FFFFFF, #AE1C28, #21468B); - -webkit-background-clip: text; - -webkit-text-fill-color: transparent; -webkit-animation: gradient-default-animate 2s infinite linear; -moz-animation: gradient-default-animate 2s infinite linear; background-size: 200%; diff --git a/routes/api.php b/routes/api.php index 5b2c689..b95039c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,5 +20,7 @@ ->name('auth.my-settings'); Route::put('/update-settings', [AuthenticatedUserController::class, 'putSettings']) ->name('auth.update-settings'); + Route::patch('/update-settings', [AuthenticatedUserController::class, 'patchSettings']) + ->name('auth.update-single-setting'); }); }); diff --git a/storage/app/public/effects.css b/storage/app/public/effects.css index 7e2778b..ab89cba 100644 --- a/storage/app/public/effects.css +++ b/storage/app/public/effects.css @@ -19,20 +19,6 @@ background: linear-gradient(90deg, #FF6D1B, #FFEE55, #5BFF89, #4D8AFF, #6B5FFF, #FF64F9, #FF6565); -webkit-background-clip: text; -webkit-text-fill-color: transparent; - -webkit-animation: gradient-default-animate 3s infinite linear; - -moz-animation: gradient-default-animate 3s infinite linear; - background-size: 200%; - animation: gradient-default-animate 3s infinite linear; -} - -@-webkit-keyframes gradient-default-animate { - 0% {background-position: 0;} - 100% {background-position: 200%;} -} - .gradient-netherlands { - background: linear-gradient(90deg, #21468B, #FFFFFF, #AE1C28, #21468B); - -webkit-background-clip: text; - -webkit-text-fill-color: transparent; -webkit-animation: gradient-default-animate 2s infinite linear; -moz-animation: gradient-default-animate 2s infinite linear; background-size: 200%; diff --git a/tests/Feature/Http/Controllers/Api/V1/AuthenticatedUserControllerTest.php b/tests/Feature/Http/Controllers/Api/V1/AuthenticatedUserControllerTest.php index dfc8443..d571c26 100644 --- a/tests/Feature/Http/Controllers/Api/V1/AuthenticatedUserControllerTest.php +++ b/tests/Feature/Http/Controllers/Api/V1/AuthenticatedUserControllerTest.php @@ -110,6 +110,60 @@ public function testGetSettings(?string $payload, int $count) ]); } + #[DataProvider('settingsByFieldDataProvider')] + public function test_user_can_update_a_single_field_put(array $payload): void + { + // prepare + $user = User::factory()->create(); + + $payload['channel_id'] = 'global'; + + $this->partialMock(ConsumerClient::class, function ($mock) { + $mock->shouldReceive('updateUser') + ->once() + ->andReturn(true); + }); + + // act + $response = $this + ->actingAs($user) + ->patchJson(route('auth.update-single-setting'), $payload); + + // assert + $response->assertOk(); + + $this->assertDatabaseHas(Settings::class, [ + 'user_id' => $user->getKey(), + ...$payload, + ]); + } + + public static function settingsByFieldDataProvider() + { + return [ + 'update_color' => [ + 'payload' => [ + 'color_id' => 2, + ], + ], + 'update_effect' => [ + 'payload' => [ + 'effect_id' => 2, + ], + ], + 'update_occupation' => [ + 'payload' => [ + 'occupation_id' => 2, + ], + ], + 'update_pronouns' => [ + 'payload' => [ + 'pronouns' => 'she-her', + ], + ], + ]; + } + public static function settingsDataProvider() { return [