Skip to content

Commit

Permalink
Update Group Edit API
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Nov 11, 2024
1 parent 2b93713 commit f57dd32
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 239 deletions.
63 changes: 63 additions & 0 deletions app/assets/composable/useGroupEditApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ref } from 'vue'
import axios from 'axios'
import { Severity, type AlertInterface } from '@userfrosting/sprinkle-core/types'

/**
* Interfaces - What the API expects and what it returns
*/
interface GroupEditForm {
slug: string
name: string
description: string
icon: string
}

interface GroupEditResponse {
success: boolean
message: string
group: GroupEditForm
}

// TODO : Add validation
// 'schema://requests/group/edit-info.yaml'

/**
* API Composable
*/
export function useGroupEditApi() {
const loadingState = ref<Boolean>(false)
const apiError = ref<AlertInterface | null>(null)

async function submitGroupEdit(slug: string, data: GroupEditForm) {
loadingState.value = true
apiError.value = null
return axios
.put<GroupEditResponse>('/api/groups/g/' + slug, data)
.then((response) => {
return {
success: response.data.success,
message: response.data.message,
group: response.data.group
}
})
.catch((err) => {
apiError.value = {
...{
description: 'An error as occurred',
style: Severity.Danger,
closeBtn: true
},
...err.response.data
}

throw apiError.value
})
.finally(() => {
loadingState.value = false
})
}

return { submitGroupEdit, loadingState, apiError }
}

export type { GroupEditForm, GroupEditResponse }
20 changes: 12 additions & 8 deletions app/src/Controller/Group/GroupEditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
use Illuminate\Database\Connection;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Alert\AlertStream;
use UserFrosting\Config\Config;
use UserFrosting\Fortress\RequestSchema;
use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\Fortress\Transformer\RequestDataTransformer;
use UserFrosting\Fortress\Validator\ServerSideValidator;
use UserFrosting\I18n\Translator;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
Expand Down Expand Up @@ -50,7 +50,7 @@ class GroupEditAction
* Inject dependencies.
*/
public function __construct(
protected AlertStream $alert,
protected Translator $translator,
protected Authenticator $authenticator,
protected Config $config,
protected Connection $db,
Expand All @@ -71,8 +71,12 @@ public function __construct(
*/
public function __invoke(GroupInterface $group, Request $request, Response $response): Response
{
$this->handle($group, $request);
$payload = json_encode([], JSON_THROW_ON_ERROR);
$group = $this->handle($group, $request);
$payload = json_encode([
'success' => true,
'message' => $this->translator->translate('GROUP.UPDATE', $group->toArray()),
'group' => $group,
], JSON_THROW_ON_ERROR);
$response->getBody()->write($payload);

return $response->withHeader('Content-Type', 'application/json');
Expand All @@ -83,8 +87,10 @@ public function __invoke(GroupInterface $group, Request $request, Response $resp
*
* @param GroupInterface $group
* @param Request $request
*
* @return GroupInterface
*/
protected function handle(GroupInterface $group, Request $request): void
protected function handle(GroupInterface $group, Request $request): GroupInterface
{
// Get PUT parameters
$params = (array) $request->getParsedBody();
Expand Down Expand Up @@ -138,9 +144,7 @@ protected function handle(GroupInterface $group, Request $request): void
]);
});

$this->alert->addMessage('success', 'GROUP.UPDATE', [
'name' => $group->name,
]);
return $group;
}

/**
Expand Down
123 changes: 0 additions & 123 deletions app/src/Controller/Group/GroupEditModal.php

This file was deleted.

23 changes: 14 additions & 9 deletions app/tests/Controller/Group/GroupEditActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace UserFrosting\Sprinkle\Admin\Tests\Controller\Group;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use UserFrosting\Alert\AlertStream;
use UserFrosting\Sprinkle\Account\Database\Models\Group;
use UserFrosting\Sprinkle\Account\Database\Models\User;
use UserFrosting\Sprinkle\Account\Testing\WithTestUser;
Expand Down Expand Up @@ -116,7 +115,14 @@ public function testPage(): void

// Assert response status & body
$this->assertResponseStatus(200, $response);
$this->assertJsonResponse([], $response);
$this->assertJsonStructure([
'success',
'message',
'group',
], $response);
$this->assertJsonResponse(true, $response, 'success');
$this->assertJsonResponse('Details updated for group <strong>The Foo</strong>', $response, 'message');
$this->assertJsonResponse('The Foo', $response, 'group.name');

// Test that the user was updated
/** @var Group */
Expand All @@ -125,12 +131,6 @@ public function testPage(): void
$this->assertSame('The Foo', $editedGroup['name']);
$this->assertSame('fas fas-icon', $editedGroup['icon']);
$this->assertSame('Foo description', $editedGroup['description']);

// Test message
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('success', array_reverse($messages)[0]['type']);
}

/**
Expand Down Expand Up @@ -158,7 +158,12 @@ public function testPageWithSameData(): void

// Assert response status & body
$this->assertResponseStatus(200, $response);
$this->assertJsonResponse([], $response);
$this->assertJsonStructure([
'success',
'message',
'group',
], $response);
$this->assertJsonResponse($group->name, $response, 'group.name');
}

public function testPostForFailedValidation(): void
Expand Down
99 changes: 0 additions & 99 deletions app/tests/Controller/Group/GroupEditModalTest.php

This file was deleted.

Loading

0 comments on commit f57dd32

Please sign in to comment.