Skip to content

Commit

Permalink
Merge pull request AmityCo#7 from AmityCo/feature-post-live-object
Browse files Browse the repository at this point in the history
Feature post live object
  • Loading branch information
trustratch authored Mar 7, 2022
2 parents d505b7a + 9835a5f commit 142d461
Show file tree
Hide file tree
Showing 42 changed files with 758 additions and 125 deletions.
8 changes: 7 additions & 1 deletion .dart_tool/package_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@
"packageUri": "lib/",
"languageVersion": "2.14"
},
{
"name": "rxdart",
"rootUri": "file:///Users/saurabhkumar/sorbh/flutter/.pub-cache/hosted/pub.dartlang.org/rxdart-0.27.3",
"packageUri": "lib/",
"languageVersion": "2.12"
},
{
"name": "shelf",
"rootUri": "file:///Users/saurabhkumar/sorbh/flutter/.pub-cache/hosted/pub.dartlang.org/shelf-1.2.0",
Expand Down Expand Up @@ -530,7 +536,7 @@
"languageVersion": "2.15"
}
],
"generated": "2022-02-27T12:21:18.149516Z",
"generated": "2022-03-02T14:43:54.851642Z",
"generator": "pub",
"generatorVersion": "2.16.0"
}
1 change: 1 addition & 0 deletions lib/core/constant/global_constant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const POST_V4 = 'api/v4/posts';
const POST_V3 = 'api/v3/posts';

const COMMENT_V3 = 'api/v3/comments';
const FILE_V3 = 'api/v3/files';

const ME_FOLLLOWING = 'api/v4/me/following';
const ME_FOLLLOWERS = 'api/v4/me/followers';
Expand Down
78 changes: 78 additions & 0 deletions lib/core/model/api_request/get_post_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// To parse this JSON data, do
//
// final getPostRequest = getPostRequestFromJson(jsonString);

import 'dart:convert';

GetPostRequest getPostRequestFromJson(String str) =>
GetPostRequest.fromJson(json.decode(str));

String getPostRequestToJson(GetPostRequest data) => json.encode(data.toJson());

class GetPostRequest {
GetPostRequest({
required this.targetId,
required this.targetType,
required this.sortBy,
required this.hasFlag,
required this.isDeleted,
required this.options,
required this.feedType,
required this.dataTypes,
required this.matchingOnlyParentPost,
});

final String targetId;
final String targetType;
final String sortBy;
final dynamic hasFlag;
final dynamic isDeleted;
final Options options;
final String feedType;
final List<String> dataTypes;
final bool matchingOnlyParentPost;

factory GetPostRequest.fromJson(Map<String, dynamic> json) => GetPostRequest(
targetId: json["targetId"],
targetType: json["targetType"],
sortBy: json["sortBy"],
hasFlag: json["hasFlag"],
isDeleted: json["isDeleted"],
options: Options.fromJson(json["options"]),
feedType: json["feedType"],
dataTypes: List<String>.from(json["dataTypes"].map((x) => x)),
matchingOnlyParentPost: json["matchingOnlyParentPost"],
);

Map<String, dynamic> toJson() => {
"targetId": targetId,
"targetType": targetType,
"sortBy": sortBy,
"hasFlag": hasFlag,
"isDeleted": isDeleted,
"options": options.toJson(),
"feedType": feedType,
"dataTypes": List<dynamic>.from(dataTypes.map((x) => x)),
"matchingOnlyParentPost": matchingOnlyParentPost,
};
}

class Options {
Options({
required this.limit,
required this.token,
});

final int limit;
final String token;

factory Options.fromJson(Map<String, dynamic> json) => Options(
limit: json["limit"],
token: json["token"],
);

Map<String, dynamic> toJson() => {
"limit": limit,
"token": token,
};
}
17 changes: 16 additions & 1 deletion lib/core/service_locator/sdk_service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import 'package:amity_sdk/domain/composer_usecase/user_compose_usecase.dart';
import 'package:amity_sdk/domain/domain.dart';
import 'package:amity_sdk/domain/repo/reaction_repo.dart';
import 'package:amity_sdk/domain/usecase/comment/comment_create_usecase.dart';
import 'package:amity_sdk/domain/usecase/comment/comment_flag_usecase.dart';
import 'package:amity_sdk/domain/usecase/comment/comment_query_usecase.dart';
import 'package:amity_sdk/domain/usecase/comment/comment_unflag_usecase.dart';
import 'package:amity_sdk/domain/usecase/post/post_flag_usecase.dart';
import 'package:amity_sdk/domain/usecase/post/post_unflag_usecase.dart';
import 'package:amity_sdk/domain/usecase/reaction/add_reaction_usecase.dart';
import 'package:amity_sdk/domain/usecase/reaction/remove_reaction_usecase.dart';
import 'package:amity_sdk/public/public.dart';
Expand Down Expand Up @@ -108,7 +112,8 @@ class SdkServiceLocator {
commentDbAdapter: serviceLocator(),
commentApiInterface: serviceLocator(),
fileDbAdapter: serviceLocator(),
userDbAdapter: serviceLocator()));
userDbAdapter: serviceLocator(),
postDbAdapter: serviceLocator()));
serviceLocator.registerLazySingleton<FileRepo>(
() => FileRepoImpl(fileDbAdapter: serviceLocator()));
serviceLocator.registerLazySingleton<ReactionRepo>(
Expand Down Expand Up @@ -186,6 +191,16 @@ class SdkServiceLocator {
serviceLocator.registerLazySingleton<CommentQueryUsecase>(
() => CommentQueryUsecase(commentRepo: serviceLocator()));

serviceLocator.registerLazySingleton<PostFlagUsecase>(
() => PostFlagUsecase(postRepo: serviceLocator()));
serviceLocator.registerLazySingleton<PostUnflagUsecase>(
() => PostUnflagUsecase(postRepo: serviceLocator()));

serviceLocator.registerLazySingleton<CommentFlagUsecase>(
() => CommentFlagUsecase(commentRepo: serviceLocator()));
serviceLocator.registerLazySingleton<CommentUnflagUsecase>(
() => CommentUnflagUsecase(commentRepo: serviceLocator()));

///----------------------------------- Public Layer -----------------------------------///
//-public_repo
serviceLocator.registerLazySingleton(() => PostRepository());
Expand Down
4 changes: 4 additions & 0 deletions lib/data/data_source/local/db_adapter/post_db_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ abstract class PostDbAdapter {
Future savePostEntity(PostHiveEntity data);
Future deletePostEntity(PostHiveEntity data);
PostHiveEntity getPostEntity(String postId);

Stream<PostHiveEntity> listenPostEntity(String postId);

Future updateComment(String postId, String commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CommentDbAdapterImpl extends CommentDbAdapter {
}

@override
CommentHiveEntity getCommentEntity(String id) {
CommentHiveEntity getCommentEntity(String id) {
return box.get(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ class PostDbAdapterImpl extends PostDbAdapter {
PostHiveEntity getPostEntity(String postId) {
return box.get(postId);
}

@override
Stream<PostHiveEntity> listenPostEntity(String postId) {
return box.watch(key: postId).map((event) => event.value);
}

@override
Future updateComment(String postId, String commentId) async {
getPostEntity(postId)
..comments!.add(commentId)
..save();
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ import 'package:amity_sdk/data/response/response.dart';
abstract class CommentApiInterface {
Future<CreatePostResponse> createComment(CreateCommentRequest request);
Future<CreatePostResponse> queryComment(GetCommentRequest request);

Future<CreatePostResponse> getComment(String commentId);
Future<CreatePostResponse> updateComment(
String commentId, CreateCommentRequest request);
Future<bool> deleteComment(String commentId);

Future<bool> flagComment(String commentId);
Future<bool> unflagComment(String commentId);
Future<bool> isCommentFlagByMe(String commentId);
}
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
abstract class FileApiInterface {}
import 'dart:io';

import 'package:amity_sdk/data/response/response.dart';

abstract class FileApiInterface {
Future<CreatePostResponse> uploadFile(File file);
Future<CreatePostResponse> getFile(String fileId);
Future<bool> deleteFile(String fileId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import 'package:amity_sdk/core/model/api_request/create_post_request.dart';
import 'package:amity_sdk/core/model/api_request/get_post_request.dart';
import 'package:amity_sdk/data/data.dart';

abstract class PublicPostApiInterface {
Future<CreatePostResponse> getPostById(String postId);
Future<CreatePostResponse> queryPost(GetPostRequest request);
Future<CreatePostResponse> createPost(CreatePostRequest request);

Future<CreatePostResponse> getPostById(String postId);
Future<CreatePostResponse> updatePostById(
String postId, CreatePostRequest request);
Future<bool> deletePostById(String postId);

Future<bool> flagPost(String postId);
Future<bool> unflagPost(String postId);
Future<bool> isPostFlagByMe(String postId);
}
15 changes: 8 additions & 7 deletions lib/data/data_source/remote/client/http/http_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class HttpApiClient {
return handler.next(options);
},
));

dio.interceptors.add(LogInterceptor(
requestBody: true,
responseBody: true,
logPrint: (logs) {
log(logs as String);
}));
if (amityCoreClientOption.showLogs) {
dio.interceptors.add(LogInterceptor(
requestBody: true,
responseBody: true,
logPrint: (logs) {
log(logs as String);
}));
}
}

late Dio dio;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,64 @@ class CommentApiInterfaceImpl extends CommentApiInterface {
return Future.error(amityError.amityException());
}
}

@override
Future<bool> flagComment(String commentId) async {
try {
final data =
await httpApiClient().post(COMMENT_V3 + '/$commentId' + '/flag');
return true;
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<CreatePostResponse> getComment(String commentId) async {
try {
final data = await httpApiClient().get(COMMENT_V3 + '/$commentId');
return CreatePostResponse.fromJson(data.data);
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<bool> isCommentFlagByMe(String commentId) async {
try {
final data =
await httpApiClient().get(COMMENT_V3 + '/$commentId' + '/isflagbyme');
return true;
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<bool> unflagComment(String commentId) async {
try {
final data =
await httpApiClient().delete(COMMENT_V3 + '/$commentId' + '/unflag');
return true;
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<CreatePostResponse> updateComment(
String commentId, CreateCommentRequest request) async {
try {
final data =
await httpApiClient().put(COMMENT_V3 + '/$commentId', data: request);
return CreatePostResponse.fromJson(data.data);
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import 'dart:io';

import 'package:amity_sdk/core/core.dart';
import 'package:amity_sdk/data/data.dart';
import 'package:amity_sdk/data/data_source/remote/api_interface/file_api_interface.dart';
import 'package:amity_sdk/data/data_source/remote/client/http/http_api_client.dart';
import 'package:dio/dio.dart';

class FileApiInterfaceImpl extends FileApiInterface {
final HttpApiClient httpApiClient;

FileApiInterfaceImpl({required this.httpApiClient});

@override
Future<bool> deleteFile(String fileId) async {
try {
final data = await httpApiClient().delete(FILE_V3 + '/$fileId');
return true;
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<CreatePostResponse> getFile(String fileId) async {
try {
final data = await httpApiClient().get(FILE_V3 + '/$fileId');
return CreatePostResponse.fromJson(data.data);
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}

@override
Future<CreatePostResponse> uploadFile(File file) async {
try {
final data = await httpApiClient().post(FILE_V3 + '/');
return CreatePostResponse.fromJson(data.data);
} on DioError catch (error) {
final amityError = AmityErrorResponse.fromJson(error.response!.data);
return Future.error(amityError.amityException());
}
}
}
Loading

0 comments on commit 142d461

Please sign in to comment.