-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from SandroMaglione/database
Supabase database [5]
- Loading branch information
Showing
15 changed files
with
263 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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,27 @@ | ||
/// Convert database model for `user` table to | ||
/// internal dart `class`: | ||
/// - Use `fromJson` method to convert supabase response to [UserModel] | ||
/// - Use `toJson` method to convert [UserModel] for update request | ||
class UserModel { | ||
final String id; | ||
final String? firstName; | ||
final String? lastName; | ||
|
||
const UserModel({ | ||
required this.id, | ||
this.firstName, | ||
this.lastName, | ||
}); | ||
|
||
static UserModel fromJson(Map<String, dynamic> json) => UserModel( | ||
id: json['id'] as String, | ||
firstName: json['first_name'] as String?, | ||
lastName: json['last_name'] as String?, | ||
); | ||
|
||
Map<String, dynamic> toJson() => <String, dynamic>{ | ||
'id': id, | ||
'first_name': firstName, | ||
'last_name': lastName, | ||
}; | ||
} |
This file contains 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 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 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,6 @@ | ||
import 'package:flutter_supabase_complete/app/models/user_model.dart'; | ||
|
||
abstract class UserDatabaseRepository { | ||
Future<UserModel> getUserInformation(String userId); | ||
Future<UserModel> updateUserInformation(UserModel userModel); | ||
} |
This file contains 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 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,32 @@ | ||
import 'package:flutter_supabase_complete/app/models/user_model.dart'; | ||
import 'package:flutter_supabase_complete/app/repository/user_database_repository.dart'; | ||
import 'package:flutter_supabase_complete/core/config/supabase_table.dart'; | ||
import 'package:injectable/injectable.dart'; | ||
import 'package:supabase_flutter/supabase_flutter.dart'; | ||
|
||
@Injectable(as: UserDatabaseRepository) | ||
class SupabaseDatabaseRepository implements UserDatabaseRepository { | ||
final Supabase _supabase; | ||
final UserSupabaseTable _userSupabaseTable; | ||
const SupabaseDatabaseRepository(this._supabase, this._userSupabaseTable); | ||
|
||
@override | ||
Future<UserModel> getUserInformation(String userId) async { | ||
final response = await _supabase.client | ||
.from(_userSupabaseTable.tableName) | ||
.select() | ||
.eq(_userSupabaseTable.idColumn, userId) | ||
.single(); | ||
|
||
final userModel = UserModel.fromJson(response as Map<String, dynamic>); | ||
return userModel; | ||
} | ||
|
||
@override | ||
Future<UserModel> updateUserInformation(UserModel userModel) async { | ||
await _supabase.client | ||
.from(_userSupabaseTable.tableName) | ||
.update(userModel.toJson()); | ||
return userModel; | ||
} | ||
} |
This file contains 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,58 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_supabase_complete/app/models/user_model.dart'; | ||
import 'package:flutter_supabase_complete/app/repository/user_database_repository.dart'; | ||
import 'package:flutter_supabase_complete/injectable.dart'; | ||
|
||
class UpdateUserForm extends StatefulWidget { | ||
final String userId; | ||
const UpdateUserForm({ | ||
required this.userId, | ||
Key? key, | ||
}) : super(key: key); | ||
|
||
@override | ||
State<UpdateUserForm> createState() => _UpdateUserFormState(); | ||
} | ||
|
||
class _UpdateUserFormState extends State<UpdateUserForm> { | ||
String firstName = ""; | ||
String lastName = ""; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
children: [ | ||
TextField( | ||
onChanged: (value) => setState(() { | ||
firstName = value; | ||
}), | ||
), | ||
TextField( | ||
onChanged: (value) => setState(() { | ||
lastName = value; | ||
}), | ||
), | ||
ElevatedButton( | ||
onPressed: _onClickUpdateUser, | ||
child: const Text("Update"), | ||
), | ||
], | ||
); | ||
} | ||
|
||
Future<void> _onClickUpdateUser() async { | ||
try { | ||
await getIt<UserDatabaseRepository>().updateUserInformation( | ||
UserModel( | ||
id: widget.userId, | ||
firstName: firstName, | ||
lastName: lastName, | ||
), | ||
); | ||
} catch (e) { | ||
// TODO: Show proper error to users | ||
print("Error when updating user information"); | ||
print(e); | ||
} | ||
} | ||
} |
This file contains 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,33 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_supabase_complete/app/models/user_model.dart'; | ||
import 'package:flutter_supabase_complete/app/repository/user_database_repository.dart'; | ||
import 'package:flutter_supabase_complete/injectable.dart'; | ||
|
||
class UserInformationText extends StatelessWidget { | ||
final String userId; | ||
const UserInformationText({ | ||
required this.userId, | ||
Key? key, | ||
}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FutureBuilder<UserModel>( | ||
future: getIt<UserDatabaseRepository>().getUserInformation(userId), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return const CircularProgressIndicator(); | ||
} else if (snapshot.connectionState == ConnectionState.done) { | ||
final data = snapshot.data; | ||
if (data != null) { | ||
return Text(data.firstName ?? "No name"); | ||
} | ||
|
||
return const Text("No found"); | ||
} | ||
|
||
return const Text("Error"); | ||
}, | ||
); | ||
} | ||
} |
This file contains 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 @@ | ||
import 'package:injectable/injectable.dart'; | ||
|
||
/// Set of all the database tables in Supabase. | ||
/// | ||
/// Used to reference valid tables when making database requests. | ||
abstract class SupabaseTable { | ||
const SupabaseTable(); | ||
String get tableName; | ||
} | ||
|
||
@lazySingleton | ||
class UserSupabaseTable implements SupabaseTable { | ||
const UserSupabaseTable(); | ||
|
||
@override | ||
String get tableName => "user"; | ||
|
||
String get idColumn => "id"; | ||
String get idFirstName => "first_name"; | ||
String get idLastName => "last_name"; | ||
} |
This file contains 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 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
Oops, something went wrong.