Skip to content
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

feat: new endpoint with single field patch #7

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/Http/Controllers/Api/V1/AuthenticatedUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/SettingsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
14 changes: 0 additions & 14 deletions public/css/app/custom-stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
14 changes: 0 additions & 14 deletions storage/app/public/effects.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down