Skip to content

Commit

Permalink
feat/hashtagsandusertags/posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Medcell1 committed Aug 20, 2024
1 parent b999484 commit e61ffef
Show file tree
Hide file tree
Showing 93 changed files with 433 additions and 188 deletions.
392 changes: 204 additions & 188 deletions .idea/libraries/Dart_Packages.xml

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

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

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
88 changes: 88 additions & 0 deletions .idea/sonarlint/issuestore/index.pb

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

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
88 changes: 88 additions & 0 deletions .idea/sonarlint/securityhotspotstore/index.pb

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

24 changes: 24 additions & 0 deletions lib/common/models/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ class Post {
final String caption;
final List<String> mediaUrls;
final DateTime postTimestamp;
final List<String> tags;
final List<String> hashtags;
final List<String> userTags;

Post({
required this.id,
required this.userId,
required this.caption,
this.mediaUrls = const [],
required this.postTimestamp,
this.tags = const [],
this.hashtags = const [],
this.userTags = const [],
});

factory Post.fromJson(Map<String, dynamic> json) {
Expand All @@ -26,6 +32,15 @@ class Post {
postTimestamp: json['postTimestamp'] != null
? DateTime.parse(json['postTimestamp'].toString())
: DateTime.now(),
tags: (json['tags'] == null)
? <String>[]
: (json['tags'] as List).map((e) => e as String).toList(),
hashtags: (json['hashtags'] == null)
? <String>[]
: (json['hashtags'] as List).map((e) => e as String).toList(),
userTags: (json['userTags'] == null)
? <String>[]
: (json['userTags'] as List).map((e) => e as String).toList(),
);
}

Expand All @@ -36,6 +51,9 @@ class Post {
'caption': caption,
'mediaUrls': mediaUrls,
'postTimestamp': postTimestamp.toString(),
'tags': tags,
'hashtags': hashtags,
'userTags': userTags,
};
}

Expand All @@ -45,13 +63,19 @@ class Post {
String? caption,
List<String>? mediaUrls,
DateTime? postTimestamp,
List<String>? tags,
List<String>? hashtags,
List<String>? userTags,
}) {
return Post(
id: id ?? this.id,
userId: userId ?? this.userId,
caption: caption ?? this.caption,
mediaUrls: mediaUrls ?? this.mediaUrls,
postTimestamp: postTimestamp ?? this.postTimestamp,
tags: tags ?? this.tags,
hashtags: hashtags ?? this.hashtags,
userTags: userTags ?? this.userTags,
);
}
}
4 changes: 4 additions & 0 deletions lib/data/repositories/post/post_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ class PostRepository extends PostRepositoryImpl {
required ObjectId userId,
required String caption,
List<String>? mediaUrls,
List<String>? hashTags,
List<String>? userTags,
}) async {
final post = Post(
id: ObjectId(),
userId: userId,
caption: caption,
mediaUrls: mediaUrls ?? [],
postTimestamp: DateTime.now(),
hashtags: hashTags ?? [],
userTags: userTags ?? [],
);

await _postsCollection.insert(post.toJson());
Expand Down
9 changes: 9 additions & 0 deletions lib/data/request_handlers/post.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:cats_backend/common/common.dart';
import 'package:cats_backend/helpers/extract_tags.dart';
import 'package:dart_frog/dart_frog.dart';
import 'package:mongo_dart/mongo_dart.dart';

Expand All @@ -11,9 +12,11 @@ abstract class PostRequestHandler {
required User saint,
required FormData formData,
});

Future<Response> handleGetPostById({
required ObjectId postId,
});

Future<Response> handleGetPostsByUserId({
required ObjectId userId,
});
Expand Down Expand Up @@ -42,6 +45,10 @@ class PostRequestHandlerImpl implements PostRequestHandler {
statusCode: 400,
);
}
final hashTags = extractHashtags(caption);
final userTags = extractUserTags(caption);
printGreen('===>HashTags: $hashTags');
printGreen('===>userTags: $userTags');

/// Check if there is image in the form data
Expand Down Expand Up @@ -81,6 +88,8 @@ class PostRequestHandlerImpl implements PostRequestHandler {
userId: saint.$_id,
caption: caption,
mediaUrls: mediaUrls,
hashTags: hashTags,
userTags: userTags,
);

return Response.json(
Expand Down
14 changes: 14 additions & 0 deletions lib/helpers/extract_tags.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
List<String> extractHashtags(String caption) {
final hashtagRegExp = RegExp(r'\B#\w\w+');
return hashtagRegExp
.allMatches(caption)
.map((match) => match.group(0)!.substring(1))
.toList();
}
List<String> extractUserTags(String caption) {
final userTagRegExp = RegExp(r'\B@\w+');
return userTagRegExp
.allMatches(caption)
.map((match) => match.group(0)!.substring(1))
.toList();
}

0 comments on commit e61ffef

Please sign in to comment.