Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
feat: update response
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Feb 7, 2024
1 parent b236965 commit 4afc93a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion e2e/auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void main() {
);

final loginResponse =
LoginResponse.fromJson(baseResponse.data as Map<String, dynamic>);
LoginResponse.fromJson(baseResponse.result as Map<String, dynamic>);

expect(response.statusCode, HttpStatus.ok);
expect(
Expand Down
10 changes: 5 additions & 5 deletions lib/dtos/response/base_response_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ part 'base_response_data.g.dart';
class BaseResponseData {
const BaseResponseData({
required this.success,
this.data,
this.result,
this.message = kSuccessResponseMessage,
});

factory BaseResponseData.fromJson(Map<String, dynamic> json) =>
_$BaseResponseDataFromJson(json);

factory BaseResponseData.data(dynamic data) {
factory BaseResponseData.data(dynamic result) {
return BaseResponseData(
success: true,
data: data,
result: result,
);
}

Expand All @@ -43,7 +43,7 @@ class BaseResponseData {

final bool success;
final String message;
final dynamic data;
final dynamic result;

Map<String, dynamic> toJson() => _$BaseResponseDataToJson(this);

Expand All @@ -55,7 +55,7 @@ class BaseResponseData {
return BaseResponseData(
success: success ?? this.success,
message: message ?? this.message,
data: data ?? this.data,
result: data ?? result,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/dtos/response/base_response_data.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions routes/api/blogs/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:stormberry/stormberry.dart';
import 'package:uuid/uuid.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';
import 'package:very_good_blog_app_backend/dtos/request/blogs/create_blog_request.dart';
import 'package:very_good_blog_app_backend/dtos/response/base_pagination_response.dart';
import 'package:very_good_blog_app_backend/dtos/response/base_response_data.dart';
import 'package:very_good_blog_app_backend/dtos/response/blogs/get_blog_response.dart';
import 'package:very_good_blog_app_backend/models/blog.dart';
Expand All @@ -26,26 +25,21 @@ Future<Response> _onBlogsGetRequest(RequestContext context) async {
final queryParams = context.request.uri.queryParameters;
final limit = int.tryParse(queryParams['limit'] ?? '') ?? 20;
final currentPage = int.tryParse(queryParams['page'] ?? '') ?? 1;
final searchValue = queryParams['search'];

// fuzzy search


try {
final results = await db.blogs.queryBlogs(
QueryParams(
limit: limit,
offset: (currentPage - 1) * limit,
),
where:
,),
);
final blogs = results.map(GetBlogResponse.fromView);
final pagination = BasePaginationResponse(
currentPage: currentPage,
limit: limit,
totalCount: blogs.length,
);
return OkResponse(
{
'blogs': blogs.map((e) => e.toJson()).toList(),
'pagination': pagination.toJson(),
},
);
return OkResponse(blogs.map((e) => e.toJson()).toList());
} catch (e) {
return ServerErrorResponse(e.toString());
}
Expand Down

0 comments on commit 4afc93a

Please sign in to comment.