Unverified Commit 8e1abe01 authored by Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Browse files

feat(user): Add extraction of users posts in user

parent 8682f0cf
......@@ -22,23 +22,27 @@ public class User implements WithIdentifier {
private List<User> follows;
@Schema(description = "List of followers of this user", implementation = User.class)
private List<User> followers;
@Schema(description = "List of posts of this user", implementation = Post.class)
private List<Post> posts;
@Schema(description = "List of liked posts of this user", implementation = Post.class)
private List<Post> likedPosts;
public User(String id, String userName, String email, String uri, String description, List<Post> likedPosts,
List<User> follows, List<User> followers) {
List<Post> posts, List<User> follows, List<User> followers) {
this.id = id;
this.userName = userName;
this.email = email;
this.uri = uri;
this.description = description;
this.likedPosts = likedPosts;
this.posts = posts;
this.follows = follows;
this.followers = followers;
}
public User(String id, String userName, String email, String uri, String description) {
this(id, userName, email, uri, description, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
this(id, userName, email, uri, description, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
new ArrayList<>());
}
public User(UserCreationPayload payload) {
......@@ -92,6 +96,10 @@ public class User implements WithIdentifier {
return likedPosts;
}
public List<Post> getPosts() {
return posts;
}
@Schema(description = "Number of followers of this user", example = "2")
public Integer getNbFollowers() {
return getFollowers() != null ? getFollowers().size() : null;
......
......@@ -25,7 +25,7 @@ public class DBFollowing extends DBService<Following> {
private FollowedValue followedValue;
private User makeUser(String id) {
return new User(id, null, null, null, null, null, null, null);
return new User(id, null, null, null, null, null, null, null, null);
}
protected Set<String> getFollowedIdFromId(Set<String> ids) {
......
......@@ -26,7 +26,7 @@ public class DBLike extends DBService<Like> {
private PostRelatedValue postRelatedValue;
private User makeUser(String id) {
return new User(id, null, null, null, null, null, null, null);
return new User(id, null, null, null, null, null, null, null, null);
}
private Post makePost(String id) {
......
......@@ -23,7 +23,7 @@ public class DBUser extends DBService<User> {
private UserNameStartsWith userNameStartsWith;
public User searchFirstWithEmail(String email) {
return searchFirstWith(new User(null, null, email, null, null, null, null, null), emailValue).data;
return searchFirstWith(new User(null, null, email, null, null, null, null, null, null), emailValue).data;
}
public WithCursor<List<User>> searchWithUserNameStartsWith(String userName) {
......@@ -56,7 +56,7 @@ public class DBUser extends DBService<User> {
public WithCursor<List<User>> searchWithUserNameStartsWith(Integer limit, Cursor lastCursor, OrderBy[] orders,
String userName) {
return searchWith(limit, lastCursor, orders, new User(null, userName, null, null, null, null, null, null),
return searchWith(limit, lastCursor, orders, new User(null, userName, null, null, null, null, null, null, null),
userNameStartsWith);
}
}
......@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.Post;
import fr.univnantes.webandcloud.api.core.User;
import fr.univnantes.webandcloud.api.responses.ResponseError;
import fr.univnantes.webandcloud.api.services.DBAncestorService;
import fr.univnantes.webandcloud.api.services.DBService;
import fr.univnantes.webandcloud.api.services.WithIdentifierService;
import fr.univnantes.webandcloud.api.services.db.DBFollowing;
......@@ -25,7 +26,7 @@ import fr.univnantes.webandcloud.api.services.db.DBLike;
public class UserUtils extends BaseUtils<User> {
@Autowired
private DBService<Post> postDb;
private DBAncestorService<Post, User> postDb;
@Autowired
private DBService<User> userDb;
@Autowired
......@@ -59,9 +60,18 @@ public class UserUtils extends BaseUtils<User> {
try {
String email = entity.getString("email");
List<Post> likedPosts = new ArrayList<>();
for (String postId : likeDB.searchPostIdWithUser(entity.getKey().getName())) {
for (String likedPostId : likeDB.searchPostIdWithUser(entity.getKey().getName())) {
try {
likedPosts.add(postDb.getWithoutLinks(postId));
likedPosts.add(postDb.getWithoutLinks(likedPostId));
} catch (ResponseError ignored) {
// liked post may be disappear if deleted
}
}
List<Post> posts = new ArrayList<>();
for (String postId : postDb.getIdsRelativeOfAncestor(
new User(entity.getKey().getName(), null, null, null, null, null, null, null, null))) {
try {
posts.add(postDb.getWithoutLinks(postId));
} catch (ResponseError ignored) {
// post may be disappear if deleted
}
......@@ -82,8 +92,8 @@ public class UserUtils extends BaseUtils<User> {
// follower may be disappear if deleted
}
}
return new User(entity.getKey().getName(), userName, email, uri, description, likedPosts, follows,
followers);
return new User(entity.getKey().getName(), userName, email, uri, description, likedPosts, posts,
follows, followers);
} catch (DatastoreException ignored) {
throw new RuntimeException(
String.format("Integrity error of user %s (email missing)", entity.getKey().getName()));
......@@ -156,7 +166,7 @@ public class UserUtils extends BaseUtils<User> {
String userName = entity.getString("userName");
try {
String email = entity.getString("email");
return new User(entity.getKey().getName(), userName, email, uri, description, null, null, null);
return new User(entity.getKey().getName(), userName, email, uri, description, null, null, null, null);
} catch (DatastoreException ignored) {
throw new RuntimeException(
String.format("Integrity error of user %s (email missing)", entity.getKey().getName()));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment