Skip to content

Commit

Permalink
feat: search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
oltimaloku committed May 10, 2024
1 parent 3d3b9dc commit 64585b2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
18 changes: 18 additions & 0 deletions client/lib/features/friends/services/user_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@ class UserService {
throw Exception('Failed to reject friend request: $e');
}
}

static Future<List<OtherUser>> searchUsers(String query) async {
try {
http.Response response = await http.get(
Uri.parse(
'${EnvVariables.uri}/users/search?query=${Uri.encodeComponent(query)}'),
);
List<OtherUser> users = [];
log('Searching users: ${response.body}');
for (var user in json.decode(response.body)) {
users.add(OtherUser.fromMap(user));
}
return users;
} catch (e) {
log(e.toString());
throw Exception('Failed to search users: $e');
}
}
}
6 changes: 5 additions & 1 deletion client/lib/features/friends/view/friends_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class FriendsScreen extends StatelessWidget {
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
child: TextField(
controller:
Provider.of<FriendViewModel>(context, listen: false)
.searchController,
decoration: InputDecoration(
hintText: 'Search by username',
hintStyle: TextStyle(
Expand Down Expand Up @@ -52,7 +55,8 @@ class FriendsScreen extends StatelessWidget {
padding: const EdgeInsets.only(right: 20),
child: TextButton(
onPressed: () {
// Cancel button action
Provider.of<FriendViewModel>(context, listen: false)
.searchUsers();
},
style: TextButton.styleFrom(
backgroundColor:
Expand Down
11 changes: 11 additions & 0 deletions client/lib/features/friends/view_model/friend_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:georeal/models/other_user.dart';
class FriendViewModel extends ChangeNotifier {
List<OtherUser> _searchedUsers = [];
OtherUser? _selectedUser;
TextEditingController searchController = TextEditingController();

List<OtherUser> get searchedUsers => _searchedUsers;
OtherUser? get selectedUser => _selectedUser;
Expand Down Expand Up @@ -37,4 +38,14 @@ class FriendViewModel extends ChangeNotifier {
log(e.toString());
}
}

Future<void> searchUsers() async {
try {
_searchedUsers = await UserService.searchUsers(searchController.text);
log(_searchedUsers.toString(), name: 'Searched users');
notifyListeners();
} catch (e) {
log(e.toString());
}
}
}
22 changes: 22 additions & 0 deletions georeal/routes/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,25 @@ def reject_friend_request(request_id):
db.session.commit()

return jsonify({'message': 'Friend request rejected'}), 200


@users.route('/users/search', methods=['GET'])
def search_users():
search_query = request.args.get('query')
if not search_query:
return jsonify({'error': 'Missing query parameter'}), 400

users = User.query.filter(User.username.ilike(f'%{search_query}%')).all()
users_list = []
for user in users:
user_data = {
'user_id': user.id,
'username': user.username,
'num_places': user.num_places,
'num_posts': user.num_posts,
'num_friends': user.num_friends,
'is_friend': False,
}
users_list.append(user_data)

return jsonify(users_list), 200

0 comments on commit 64585b2

Please sign in to comment.