From ce81d908220780878ed0b1a99687d09ccb19ea24 Mon Sep 17 00:00:00 2001 From: dungngminh <63831488+dungngminh@users.noreply.github.com> Date: Fri, 27 Dec 2024 18:49:59 +0700 Subject: [PATCH] fix close db --- backend/.gitignore | 2 ++ backend/lib/models/user.dart | 2 +- backend/routes/api/auth/login/index.dart | 32 +++++++++++-------- backend/routes/api/auth/register/index.dart | 3 +- backend/routes/api/blogs/[id]/index.dart | 4 +++ backend/routes/api/blogs/index.dart | 4 +++ backend/routes/api/favorites/index.dart | 13 +++++--- backend/routes/api/followings/index.dart | 2 ++ backend/routes/api/users/[id]/blogs.dart | 3 +- .../api/users/[id]/followers/index.dart | 3 +- .../api/users/[id]/followings/index.dart | 3 +- .../routes/api/users/[id]/profiles/index.dart | 10 +++--- 12 files changed, 54 insertions(+), 27 deletions(-) diff --git a/backend/.gitignore b/backend/.gitignore index df87ee1..d440c4e 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -13,3 +13,5 @@ build/ coverage/ .vscode + +.env \ No newline at end of file diff --git a/backend/lib/models/user.dart b/backend/lib/models/user.dart index 7247a73..20beba2 100644 --- a/backend/lib/models/user.dart +++ b/backend/lib/models/user.dart @@ -3,7 +3,7 @@ import 'package:stormberry/stormberry.dart'; part 'user.schema.dart'; @Model() -abstract class User { +abstract class User { @PrimaryKey() String get id; diff --git a/backend/routes/api/auth/login/index.dart b/backend/routes/api/auth/login/index.dart index f4570d3..b89126b 100644 --- a/backend/routes/api/auth/login/index.dart +++ b/backend/routes/api/auth/login/index.dart @@ -29,18 +29,24 @@ Future _onLoginPostRequest(RequestContext context) async { return db.users .queryUsers( - QueryParams( - where: 'email=@email AND password=@password', - values: {'email': request.email, 'password': request.password.hashValue}, - ), - ) + QueryParams( + where: 'email=@email AND password=@password', + values: { + 'email': request.email, + 'password': request.password.hashValue, + }, + ), + ) .then((users) { - final user = users.firstOrNull; - return user == null - ? BadRequestResponse(ErrorMessageCode.notRegisterYet) - : OkResponse( - LoginResponse(id: user.id, token: createJwt(user.id)).toJson(), - ); - }).onError( - (e, _) => InternalServerErrorResponse(ErrorMessageCode.unknownError)); + final user = users.firstOrNull; + return user == null + ? BadRequestResponse(ErrorMessageCode.notRegisterYet) + : OkResponse( + LoginResponse(id: user.id, token: createJwt(user.id)).toJson(), + ); + }) + .onError( + (e, _) => InternalServerErrorResponse(ErrorMessageCode.unknownError), + ) + .whenComplete(db.close); } diff --git a/backend/routes/api/auth/register/index.dart b/backend/routes/api/auth/register/index.dart index 6900d54..1452122 100644 --- a/backend/routes/api/auth/register/index.dart +++ b/backend/routes/api/auth/register/index.dart @@ -53,5 +53,6 @@ Future _onRegisterPostRequest(RequestContext context) async { .then((_) => CreatedResponse()) .onError( (e, st) => InternalServerErrorResponse(ErrorMessageCode.unknownError), - ); + ) + .whenComplete(db.close); } diff --git a/backend/routes/api/blogs/[id]/index.dart b/backend/routes/api/blogs/[id]/index.dart index 34146b0..a160b57 100644 --- a/backend/routes/api/blogs/[id]/index.dart +++ b/backend/routes/api/blogs/[id]/index.dart @@ -84,6 +84,8 @@ Future _onBlogsPatchRequest(RequestContext context, String id) async { return BadRequestResponse(e.message); } catch (e) { return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } @@ -103,5 +105,7 @@ Future _onBlogsDeleteRequest( return OkResponse(); } catch (e) { return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } diff --git a/backend/routes/api/blogs/index.dart b/backend/routes/api/blogs/index.dart index 76dcd55..01717a5 100644 --- a/backend/routes/api/blogs/index.dart +++ b/backend/routes/api/blogs/index.dart @@ -39,6 +39,8 @@ Future _onBlogsGetRequest(RequestContext context) async { return OkResponse(blogs.map((e) => e.toJson()).toList()); } catch (e) { return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } @@ -70,5 +72,7 @@ Future _onBlogsPostRequest(RequestContext context) async { return BadRequestResponse(e.message); } catch (e) { return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } diff --git a/backend/routes/api/favorites/index.dart b/backend/routes/api/favorites/index.dart index e15df16..a012078 100644 --- a/backend/routes/api/favorites/index.dart +++ b/backend/routes/api/favorites/index.dart @@ -1,4 +1,3 @@ - import 'package:dart_frog/dart_frog.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:stormberry/stormberry.dart'; @@ -21,15 +20,15 @@ Future onRequest(RequestContext context) { Future _onFavoritesGetRequest(RequestContext context) { final userView = context.read(); - return context - .read() - .favoriteBlogsUserses + final db = context.read(); + return db.favoriteBlogsUserses .queryFavoriteBlogsUserses( QueryParams(where: 'user_id=@id', values: {'id': userView.id}), ) .then((r) => r.map(GetUserFavoriteBlogResponse.fromView).toList()) .then((res) => OkResponse(res.map((e) => e.toJson()).toList())) - .onError((e, _) => InternalServerErrorResponse(e.toString())); + .onError((e, _) => InternalServerErrorResponse(e.toString())) + .whenComplete(db.close); } Future _onFavoritesPostRequest(RequestContext context) async { @@ -84,5 +83,9 @@ Future _onFavoritesPostRequest(RequestContext context) async { .onError((e, _) => InternalServerErrorResponse(e.toString())); } on CheckedFromJsonException catch (e) { return BadRequestResponse(e.message); + } catch (e) { + return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } diff --git a/backend/routes/api/followings/index.dart b/backend/routes/api/followings/index.dart index cb98b4b..92c7715 100644 --- a/backend/routes/api/followings/index.dart +++ b/backend/routes/api/followings/index.dart @@ -63,5 +63,7 @@ Future _onFollowingPost(RequestContext context) async { return BadRequestResponse(e.message); } catch (e) { return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } } diff --git a/backend/routes/api/users/[id]/blogs.dart b/backend/routes/api/users/[id]/blogs.dart index 80d9442..24b6a84 100644 --- a/backend/routes/api/users/[id]/blogs.dart +++ b/backend/routes/api/users/[id]/blogs.dart @@ -19,5 +19,6 @@ Future _onUsersByIdBlogsGet(RequestContext context, String id) { .queryBlogs(QueryParams(where: 'creator_id=@id', values: {'id': id})) .then((views) => views.map(GetUserBlogResponse.fromView)) .then((res) => OkResponse(res.map((e) => e.toJson()).toList())) - .onError((e, _) => InternalServerErrorResponse(e.toString())); + .onError((e, _) => InternalServerErrorResponse(e.toString())) + .whenComplete(() => context.read().close()); } diff --git a/backend/routes/api/users/[id]/followers/index.dart b/backend/routes/api/users/[id]/followers/index.dart index 7fadc5b..cf0f56e 100644 --- a/backend/routes/api/users/[id]/followers/index.dart +++ b/backend/routes/api/users/[id]/followers/index.dart @@ -39,5 +39,6 @@ Future _onFollowersByIdGetRequest( ) .then((followers) => followers.map(GetUserFollowerResponse.fromView)) .then((res) => OkResponse(res.map((e) => e.toJson()).toList())) - .onError((e, _) => InternalServerErrorResponse(e.toString())); + .onError((e, _) => InternalServerErrorResponse(e.toString())) + .whenComplete(database.close); } diff --git a/backend/routes/api/users/[id]/followings/index.dart b/backend/routes/api/users/[id]/followings/index.dart index 3843a6f..b335954 100644 --- a/backend/routes/api/users/[id]/followings/index.dart +++ b/backend/routes/api/users/[id]/followings/index.dart @@ -39,5 +39,6 @@ Future _onFollowingsByIdGetRequest( ) .then((result) => result.map(GetUserFollowingResponse.fromView)) .then((res) => OkResponse(res.map((e) => e.toJson()).toList())) - .onError((e, _) => InternalServerErrorResponse(e.toString())); + .onError((e, _) => InternalServerErrorResponse(e.toString())) + .whenComplete(database.close); } diff --git a/backend/routes/api/users/[id]/profiles/index.dart b/backend/routes/api/users/[id]/profiles/index.dart index f147f1d..d39ac7b 100644 --- a/backend/routes/api/users/[id]/profiles/index.dart +++ b/backend/routes/api/users/[id]/profiles/index.dart @@ -1,4 +1,3 @@ - import 'package:dart_frog/dart_frog.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:stormberry/stormberry.dart'; @@ -60,11 +59,10 @@ Future _onUserByIdPatchRequest( if (body.isEmpty) { return BadRequestResponse(); } + final db = context.read(); try { final request = EditUserProfileRequest.fromJson(body.asJson()); - return context - .read() - .users + return db.users .updateOne( UserUpdateRequest( id: userView.id, @@ -76,5 +74,9 @@ Future _onUserByIdPatchRequest( .onError((e, _) => InternalServerErrorResponse(e.toString())); } on CheckedFromJsonException catch (e) { return BadRequestResponse(e.message); + } catch (e) { + return InternalServerErrorResponse(e.toString()); + } finally { + await db.close(); } }