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

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed Sep 27, 2023
1 parent 181984d commit 6db8c14
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
41 changes: 21 additions & 20 deletions routes/api/blogs/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,26 @@ Future<Response> _onBlogsPostRequest(RequestContext context) async {
final body = await context.request.body();

if (body.isEmpty) return BadRequestResponse();
final request = CreateBlogRequest.fromJson(body.asJson());
try {
final request = CreateBlogRequest.fromJson(body.asJson());

return db.blogs
.insertOne(
BlogInsertRequest(
id: const Uuid().v4(),
title: request.title,
category: request.category,
content: request.content,
imageUrl: request.imageUrl,
createdAt: DateTime.now(),
creatorId: user.id,
updatedAt: DateTime.now(),
isDeleted: false,
),
)
.then<Response>((_) => CreatedResponse('New blog is created'))
.onError<CheckedFromJsonException>(
(e, _) => BadRequestResponse(e.message),
)
.onError((e, _) => ServerErrorResponse(e.toString()));
await db.blogs.insertOne(
BlogInsertRequest(
id: const Uuid().v4(),
title: request.title,
category: request.category,
content: request.content,
imageUrl: request.imageUrl,
createdAt: DateTime.now(),
creatorId: user.id,
updatedAt: DateTime.now(),
isDeleted: false,
),
);
return CreatedResponse('New blog is created');
} on CheckedFromJsonException catch (e) {
return BadRequestResponse(e.message);
} catch (e) {
return ServerErrorResponse(e.toString());
}
}
2 changes: 1 addition & 1 deletion routes/api/favorites/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import 'package:dart_frog/dart_frog.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:stormberry/stormberry.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';
import 'package:very_good_blog_app_backend/dtos/request/favorites/favorite_blog_request.dart';
import 'package:very_good_blog_app_backend/dtos/response/base_response_data.dart';
import 'package:very_good_blog_app_backend/dtos/response/favorites/get_user_favorite_blog_response.dart';
import 'package:very_good_blog_app_backend/models/blog.dart';
import 'package:very_good_blog_app_backend/models/favorite_blogs_users.dart';
import 'package:very_good_blog_app_backend/models/user.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';

/// @Allow(GET, POST)
Future<Response> onRequest(RequestContext context) {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/followings/index.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:dart_frog/dart_frog.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:stormberry/stormberry.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';
import 'package:very_good_blog_app_backend/dtos/request/followings/create_following_request.dart';
import 'package:very_good_blog_app_backend/dtos/response/base_response_data.dart';
import 'package:very_good_blog_app_backend/models/following_follower.dart';
import 'package:very_good_blog_app_backend/models/user.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';

/// @Allow(POST)
Future<Response> onRequest(RequestContext context) {
Expand Down
2 changes: 1 addition & 1 deletion routes/api/users/[id]/profiles/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import 'package:dart_frog/dart_frog.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:stormberry/stormberry.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';
import 'package:very_good_blog_app_backend/dtos/request/users/edit_user_profile_request.dart';
import 'package:very_good_blog_app_backend/dtos/response/base_response_data.dart';
import 'package:very_good_blog_app_backend/dtos/response/users/profiles/get_user_profile_response.dart';
import 'package:very_good_blog_app_backend/models/following_follower.dart';
import 'package:very_good_blog_app_backend/models/user.dart';
import 'package:very_good_blog_app_backend/common/extensions/json_ext.dart';

/// @Allow(GET, PATCH)
Future<Response> onRequest(
Expand Down

0 comments on commit 6db8c14

Please sign in to comment.