-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13d128d
commit b999484
Showing
15 changed files
with
420 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:mongo_dart/mongo_dart.dart'; | ||
|
||
class Post { | ||
final ObjectId id; | ||
final ObjectId userId; | ||
final String caption; | ||
final List<String> mediaUrls; | ||
final DateTime postTimestamp; | ||
|
||
Post({ | ||
required this.id, | ||
required this.userId, | ||
required this.caption, | ||
this.mediaUrls = const [], | ||
required this.postTimestamp, | ||
}); | ||
|
||
factory Post.fromJson(Map<String, dynamic> json) { | ||
return Post( | ||
id: json['_id'] as ObjectId, | ||
userId: json['userId'] as ObjectId, | ||
caption: json['caption'] as String, | ||
mediaUrls: (json['mediaUrls'] == null) | ||
? <String>[] | ||
: (json['mediaUrls'] as List).map((e) => e as String).toList(), | ||
postTimestamp: json['postTimestamp'] != null | ||
? DateTime.parse(json['postTimestamp'].toString()) | ||
: DateTime.now(), | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'_id': id, | ||
'userId': userId, | ||
'caption': caption, | ||
'mediaUrls': mediaUrls, | ||
'postTimestamp': postTimestamp.toString(), | ||
}; | ||
} | ||
|
||
Post copyWith({ | ||
ObjectId? id, | ||
ObjectId? userId, | ||
String? caption, | ||
List<String>? mediaUrls, | ||
DateTime? postTimestamp, | ||
}) { | ||
return Post( | ||
id: id ?? this.id, | ||
userId: userId ?? this.userId, | ||
caption: caption ?? this.caption, | ||
mediaUrls: mediaUrls ?? this.mediaUrls, | ||
postTimestamp: postTimestamp ?? this.postTimestamp, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import 'package:cats_backend/common/common.dart'; | ||
import 'package:mongo_dart/mongo_dart.dart'; | ||
|
||
abstract class PostRepositoryImpl { | ||
Future<Post?> createPost({ | ||
required ObjectId userId, | ||
required String caption, | ||
List<String>? mediaUrls, | ||
}); | ||
|
||
Future<Post?> getPostById({required ObjectId postId}); | ||
|
||
Future<List<Post?>> getPostsByUserId({required ObjectId userId}); | ||
|
||
// Future<Post?> getPostById({required ObjectId postId}); | ||
|
||
// Future<Post?> updatePost({ | ||
// required ObjectId postId, | ||
// required String caption, | ||
// List<String>? mediaUrls, | ||
// }); | ||
|
||
// Future<bool?> deletePost({required ObjectId postId}); | ||
} | ||
|
||
class PostRepository extends PostRepositoryImpl { | ||
final Db _database; | ||
|
||
PostRepository({ | ||
required Db database, | ||
}) : _database = database; | ||
|
||
DbCollection get _postsCollection => _database.collection('posts'); | ||
|
||
@override | ||
Future<Post?> createPost({ | ||
required ObjectId userId, | ||
required String caption, | ||
List<String>? mediaUrls, | ||
}) async { | ||
final post = Post( | ||
id: ObjectId(), | ||
userId: userId, | ||
caption: caption, | ||
mediaUrls: mediaUrls ?? [], | ||
postTimestamp: DateTime.now(), | ||
); | ||
|
||
await _postsCollection.insert(post.toJson()); | ||
|
||
return post; | ||
} | ||
|
||
@override | ||
Future<Post?> getPostById({required ObjectId postId}) async { | ||
final post = await _postsCollection.findOne({ | ||
'_id': postId, | ||
}); | ||
|
||
return post == null ? null : Post.fromJson(post); | ||
} | ||
|
||
@override | ||
Future<List<Post?>> getPostsByUserId({required ObjectId userId}) async { | ||
final posts = await _postsCollection.find({ | ||
'userId': userId, | ||
}).toList(); | ||
|
||
return posts.map((e) => Post.fromJson(e)).toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'package:cats_backend/common/common.dart'; | ||
import 'package:dart_frog/dart_frog.dart'; | ||
import 'package:mongo_dart/mongo_dart.dart'; | ||
|
||
import '../../common/constants/storage_directories.dart'; | ||
import '../repositories/file_upload/file_upload.dart'; | ||
import '../repositories/post/post_repository.dart'; | ||
|
||
abstract class PostRequestHandler { | ||
Future<Response> handleCreatePost({ | ||
required User saint, | ||
required FormData formData, | ||
}); | ||
Future<Response> handleGetPostById({ | ||
required ObjectId postId, | ||
}); | ||
Future<Response> handleGetPostsByUserId({ | ||
required ObjectId userId, | ||
}); | ||
} | ||
|
||
class PostRequestHandlerImpl implements PostRequestHandler { | ||
final PostRepository _postRepository; | ||
|
||
const PostRequestHandlerImpl({ | ||
required PostRepository postRepository, | ||
}) : _postRepository = postRepository; | ||
|
||
@override | ||
Future<Response> handleCreatePost({ | ||
required User saint, | ||
required FormData formData, | ||
}) async { | ||
print('===> POST <==> Post:'); | ||
final errors = <String?>[]; | ||
final mediaUrls = <String>[]; | ||
|
||
final caption = formData.fields['caption']; | ||
if (caption == null) { | ||
return Response.json( | ||
body: 'Error: Caption is required.', | ||
statusCode: 400, | ||
); | ||
} | ||
|
||
/// Check if there is image in the form data | ||
final files = formData.files; | ||
if (files.isNotEmpty) { | ||
final files = await FileUpload.getFilesFromFormData(formData); | ||
printYellow('MyFiles: \n${files.map((e) => '${e.name}\n')}'); | ||
printGreen('${files.length} files found in the form data.'); | ||
|
||
final uploadResults = await FileUpload.uploadMultipleFilesAndReturnUrls( | ||
uploadedFiles: files, | ||
storageDir: StorageDirectories.postsByUserId(userId: saint.$_id.oid), | ||
maxFiles: 3, | ||
); | ||
|
||
errors.addAll( | ||
uploadResults.map((e) => e.error).toList().where((e) => e != null), | ||
); | ||
mediaUrls.addAll( | ||
uploadResults.where((e) => e.url != null).map((e) => e.url!).toList(), | ||
); | ||
} else { | ||
printYellow('No files found in the form data.'); | ||
} | ||
|
||
if (errors.isNotEmpty) { | ||
return Response.json( | ||
body: { | ||
'message': 'Failed to upload image', | ||
'errors': errors, | ||
}, | ||
statusCode: 500, | ||
); | ||
} | ||
|
||
final post = await _postRepository.createPost( | ||
userId: saint.$_id, | ||
caption: caption, | ||
mediaUrls: mediaUrls, | ||
); | ||
|
||
return Response.json( | ||
body: post, | ||
statusCode: post != null ? 201 : 400, | ||
); | ||
} | ||
|
||
@override | ||
Future<Response> handleGetPostById({ | ||
required ObjectId postId, | ||
}) async { | ||
final post = await _postRepository.getPostById(postId: postId); | ||
|
||
return Response.json( | ||
body: post, | ||
statusCode: post != null ? 200 : 404, | ||
); | ||
} | ||
|
||
@override | ||
Future<Response> handleGetPostsByUserId({ | ||
required ObjectId userId, | ||
}) async { | ||
final posts = await _postRepository.getPostsByUserId(userId: userId); | ||
return Response.json( | ||
body: posts, | ||
statusCode: posts.isNotEmpty ? 200 : 404, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export 'buss.dart'; | ||
export 'cats.dart'; | ||
export 'chat.dart'; | ||
export 'post.dart'; | ||
export 'profile.dart'; | ||
export 'user.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.