Skip to content

Commit

Permalink
fix close db
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Dec 27, 2024
1 parent a404145 commit ce81d90
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 27 deletions.
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ build/
coverage/

.vscode

.env
2 changes: 1 addition & 1 deletion backend/lib/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:stormberry/stormberry.dart';
part 'user.schema.dart';

@Model()
abstract class User {
abstract class User {
@PrimaryKey()
String get id;

Expand Down
32 changes: 19 additions & 13 deletions backend/routes/api/auth/login/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ Future<Response> _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<Response>((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);
}
3 changes: 2 additions & 1 deletion backend/routes/api/auth/register/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ Future<Response> _onRegisterPostRequest(RequestContext context) async {
.then<Response>((_) => CreatedResponse())
.onError(
(e, st) => InternalServerErrorResponse(ErrorMessageCode.unknownError),
);
)
.whenComplete(db.close);
}
4 changes: 4 additions & 0 deletions backend/routes/api/blogs/[id]/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Future<Response> _onBlogsPatchRequest(RequestContext context, String id) async {
return BadRequestResponse(e.message);
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}

Expand All @@ -103,5 +105,7 @@ Future<Response> _onBlogsDeleteRequest(
return OkResponse();
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}
4 changes: 4 additions & 0 deletions backend/routes/api/blogs/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Future<Response> _onBlogsGetRequest(RequestContext context) async {
return OkResponse(blogs.map((e) => e.toJson()).toList());
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}

Expand Down Expand Up @@ -70,5 +72,7 @@ Future<Response> _onBlogsPostRequest(RequestContext context) async {
return BadRequestResponse(e.message);
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}
13 changes: 8 additions & 5 deletions backend/routes/api/favorites/index.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:dart_frog/dart_frog.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:stormberry/stormberry.dart';
Expand All @@ -21,15 +20,15 @@ Future<Response> onRequest(RequestContext context) {

Future<Response> _onFavoritesGetRequest(RequestContext context) {
final userView = context.read<UserView>();
return context
.read<Database>()
.favoriteBlogsUserses
final db = context.read<Database>();
return db.favoriteBlogsUserses
.queryFavoriteBlogsUserses(
QueryParams(where: 'user_id=@id', values: {'id': userView.id}),
)
.then((r) => r.map(GetUserFavoriteBlogResponse.fromView).toList())
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => InternalServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()))
.whenComplete(db.close);
}

Future<Response> _onFavoritesPostRequest(RequestContext context) async {
Expand Down Expand Up @@ -84,5 +83,9 @@ Future<Response> _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();
}
}
2 changes: 2 additions & 0 deletions backend/routes/api/followings/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ Future<Response> _onFollowingPost(RequestContext context) async {
return BadRequestResponse(e.message);
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}
3 changes: 2 additions & 1 deletion backend/routes/api/users/[id]/blogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Future<Response> _onUsersByIdBlogsGet(RequestContext context, String id) {
.queryBlogs(QueryParams(where: 'creator_id=@id', values: {'id': id}))
.then((views) => views.map(GetUserBlogResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => InternalServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()))
.whenComplete(() => context.read<Database>().close());
}
3 changes: 2 additions & 1 deletion backend/routes/api/users/[id]/followers/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ Future<Response> _onFollowersByIdGetRequest(
)
.then((followers) => followers.map(GetUserFollowerResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => InternalServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()))
.whenComplete(database.close);
}
3 changes: 2 additions & 1 deletion backend/routes/api/users/[id]/followings/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ Future<Response> _onFollowingsByIdGetRequest(
)
.then((result) => result.map(GetUserFollowingResponse.fromView))
.then<Response>((res) => OkResponse(res.map((e) => e.toJson()).toList()))
.onError((e, _) => InternalServerErrorResponse(e.toString()));
.onError((e, _) => InternalServerErrorResponse(e.toString()))
.whenComplete(database.close);
}
10 changes: 6 additions & 4 deletions backend/routes/api/users/[id]/profiles/index.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'package:dart_frog/dart_frog.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:stormberry/stormberry.dart';
Expand Down Expand Up @@ -60,11 +59,10 @@ Future<Response> _onUserByIdPatchRequest(
if (body.isEmpty) {
return BadRequestResponse();
}
final db = context.read<Database>();
try {
final request = EditUserProfileRequest.fromJson(body.asJson());
return context
.read<Database>()
.users
return db.users
.updateOne(
UserUpdateRequest(
id: userView.id,
Expand All @@ -76,5 +74,9 @@ Future<Response> _onUserByIdPatchRequest(
.onError((e, _) => InternalServerErrorResponse(e.toString()));
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
} catch (e) {
return InternalServerErrorResponse(e.toString());
} finally {
await db.close();
}
}

0 comments on commit ce81d90

Please sign in to comment.