Adjust API for profile fetching

- require tokenid -> required userid
This commit is contained in:
Linloir 2022-10-13 20:37:30 +08:00
parent 9369192482
commit 834e810663
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
2 changed files with 15 additions and 18 deletions

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-06 16:15:01 * @Date : 2022-10-06 16:15:01
* @LastEditTime : 2022-10-12 09:35:07 * @LastEditTime : 2022-10-13 20:36:34
* @Description : * @Description :
*/ */
@ -526,30 +526,27 @@ class DataBaseHelper {
return queryResult[0]['dir'] as String; return queryResult[0]['dir'] as String;
} }
Future<UserInfo> fetchUserInfoViaToken({ Future<UserInfo> fetchUserInfoViaID({
required int? tokenID required int userid
}) async { }) async {
if(tokenID == null) {
throw Exception('Invalid device token');
}
//Find current binded userID //Find current binded userID
var currentUserQueryResult = (await _database.query( var userQueryResult = (await _database.query(
'bindings natural join users', 'users',
where: 'bindings.tokenid = ?', where: 'userid = ?',
whereArgs: [ whereArgs: [
tokenID userid
] ]
)); ));
if(currentUserQueryResult.isEmpty) {
throw Exception('User not logged in'); if(userQueryResult.isEmpty) {
throw Exception('User not found');
} }
var currentUser = currentUserQueryResult[0];
return UserInfo( return UserInfo(
userID: currentUser['userid'] as int, userID: userQueryResult[0]['userid'] as int,
userName: currentUser['username'] as String, userName: userQueryResult[0]['username'] as String,
userAvatar: currentUser['avatar'] as String? userAvatar: userQueryResult[0]['avatar'] as String?
); );
} }

View File

@ -1,7 +1,7 @@
/* /*
* @Author : Linloir * @Author : Linloir
* @Date : 2022-10-08 20:52:48 * @Date : 2022-10-08 20:52:48
* @LastEditTime : 2022-10-12 09:09:16 * @LastEditTime : 2022-10-13 20:30:53
* @Description : * @Description :
*/ */
@ -90,7 +90,7 @@ Future<TCPResponse> onLogout(TCPRequest request, Socket socket) async {
Future<TCPResponse> onFetchProfile(TCPRequest request, Socket socket) async { Future<TCPResponse> onFetchProfile(TCPRequest request, Socket socket) async {
try { try {
var userInfo = await DataBaseHelper().fetchUserInfoViaToken(tokenID: request.tokenID); var userInfo = await DataBaseHelper().fetchUserInfoViaID(userid: request.body['userid'] as int);
return TCPResponse( return TCPResponse(
type: ResponseType.fromRequestType(request.requestType), type: ResponseType.fromRequestType(request.requestType),
status: ResponseStatus.ok, status: ResponseStatus.ok,