From ef6be983b25fd582c4f1f735202c2ed8cba2896b Mon Sep 17 00:00:00 2001 From: Jason Dean Lessenich Date: Fri, 3 May 2024 22:25:33 +0200 Subject: [PATCH] Added Restrr#createUser --- CHANGELOG.md | 1 + lib/src/api/restrr.dart | 3 +++ lib/src/api/restrr_builder.dart | 17 ++--------------- lib/src/internal/restrr_impl.dart | 15 +++++++++++++++ test/restrr_entity_test.dart | 2 ++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cd7eed..d53bf6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.10 - Implemented User registration (RestrrBuilder#register) +- Added Restrr#createUser ## 0.9.1 - Fixed use of `EntityId` diff --git a/lib/src/api/restrr.dart b/lib/src/api/restrr.dart index 81307f1..424d987 100644 --- a/lib/src/api/restrr.dart +++ b/lib/src/api/restrr.dart @@ -51,6 +51,9 @@ abstract class Restrr { /// Retrieves the currently authenticated user. Future retrieveSelf({bool forceRetrieve = false}); + /// Creates a new user. + Future createUser({required String username, required String password, String? displayName, String? email}); + /* Sessions */ Future retrieveCurrentSession({bool forceRetrieve = false}); diff --git a/lib/src/api/restrr_builder.dart b/lib/src/api/restrr_builder.dart index 92d7f1a..7df4cf3 100644 --- a/lib/src/api/restrr_builder.dart +++ b/lib/src/api/restrr_builder.dart @@ -36,24 +36,11 @@ class RestrrBuilder { Future register({required String username, required String password, String? displayName, String? email, String? sessionName}) async { return _handleAuthProcess(authFunction: (apiImpl) async { // register user first - final RestResponse userResponse = await apiImpl.requestHandler.apiRequest( - route: UserRoutes.create.compile(), - body: { - 'username': username, - 'password': password, - if (displayName != null) 'display_name': displayName, - if (email != null) 'email': email, - }, - noAuth: true, - mapper: (json) => apiImpl.entityBuilder.buildUser(json) - ); - if (userResponse.hasError) { - throw userResponse.error!; - } + final User user = await apiImpl.createUser(username: username, password: password, email: email); return apiImpl.requestHandler.apiRequest( route: SessionRoutes.create.compile(), body: { - 'username': username, + 'username': user.username, 'password': password, if (sessionName != null) 'session_name': sessionName, }, diff --git a/lib/src/internal/restrr_impl.dart b/lib/src/internal/restrr_impl.dart index 71c7c42..36a4139 100644 --- a/lib/src/internal/restrr_impl.dart +++ b/lib/src/internal/restrr_impl.dart @@ -48,6 +48,21 @@ class RestrrImpl implements Restrr { return UserIdImpl(api: this, value: session.user.id.value).retrieve(forceRetrieve: forceRetrieve); } + @override + Future createUser({required String username, required String password, String? displayName, String? email}) async { + final RestResponse response = await requestHandler + .apiRequest(route: UserRoutes.create.compile(), mapper: (json) => entityBuilder.buildUser(json), noAuth: true, body: { + 'username': username, + 'password': password, + if (displayName != null) 'display_name': displayName, + if (email != null) 'email': email, + }); + if (response.hasError) { + throw response.error!; + } + return response.data!; + } + /* Sessions */ @override diff --git a/test/restrr_entity_test.dart b/test/restrr_entity_test.dart index 5a12a20..56f173f 100644 --- a/test/restrr_entity_test.dart +++ b/test/restrr_entity_test.dart @@ -70,6 +70,8 @@ const String transactionJson = ''' void main() { late RestrrImpl api; + // TODO: mock the api instance instead of actually logging in + setUp(() async { // log in, get api instance api = (await RestrrBuilder(uri: _validUri).login(username: 'admin', password: 'Financrr123')) as RestrrImpl;