Nantes Université

Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 0fb47bfb rédigé par Féry Mathieu (Mathius)'s avatar Féry Mathieu (Mathius)
Parcourir les fichiers

lintlike): Rename Vars

parent 7ec8e07a
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -4,17 +4,17 @@ import org.apache.commons.lang3.NotImplementedException;
public class Like implements WithIdentifier {
public final User likedBy;
public final Post likes;
public final User userRelated;
public final Post postRelated;
public Like(User likedBy, Post likes) {
this.likedBy = likedBy;
this.likes = likes;
public Like(User userRelated, Post postRelated) {
this.userRelated = userRelated;
this.postRelated = postRelated;
}
@Override
public String getId() {
return String.format("%s~%s", likedBy.getId(), likes.getId());
return String.format("%s~%s", userRelated.getId(), postRelated.getId());
}
@Override
......
package fr.univnantes.webandcloud.api.services.db;
import fr.univnantes.webandcloud.api.core.Following;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.Like;
import fr.univnantes.webandcloud.api.core.Post;
import fr.univnantes.webandcloud.api.core.User;
import fr.univnantes.webandcloud.api.services.DBService;
import fr.univnantes.webandcloud.api.services.db.index.like.IndexLikedBy;
import fr.univnantes.webandcloud.api.services.db.index.like.IndexLikes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;
import fr.univnantes.webandcloud.api.services.db.index.like.IndexPostRelated;
import fr.univnantes.webandcloud.api.services.db.index.like.IndexUserRelated;
@Service
public class DBLike extends DBService<Like> {
@Autowired
private IndexLikedBy indexLikedBy;
private IndexUserRelated indexLikedBy;
@Autowired
private IndexLikes indexLikes;
private IndexPostRelated indexLikes;
private User makeUser(String id) {
return new User(id, null, null, null, null, null, null);
}
private Post makePost(String id) {
return new Post(id, null, null, null, null, null, null);
}
protected Set<String> getLikesIdFromId(Set<String> ids) {
Set<String> likedByIds = new HashSet<>();
protected Set<String> getUserIdFromId(Set<String> ids) {
Set<String> usersId = new HashSet<>();
for (String id : ids) {
likedByIds.add(getLikedByIdFromId(id));
usersId.add(getUserIdFromId(id));
}
return likedByIds;
return usersId;
}
protected String getLikedByIdFromId(String id) {
protected String getUserIdFromId(String id) {
return id.split("~")[0];
}
protected Set<String> getLikesFromId(Set<String> ids) {
protected Set<String> getPostIdFromId(Set<String> ids) {
Set<String> likesIds = new HashSet<>();
for (String id : ids) {
likesIds.add(getLikesIdFromId(id));
likesIds.add(getPostIdFromId(id));
}
return likesIds;
}
protected String getLikesIdFromId(String id) {
protected String getPostIdFromId(String id) {
return id.split("~")[1];
}
public Set<String> searchLikedByIdWithLikes(Post likes) {
return getLikesIdFromId(searchIdWith(new Like(null, likes), indexLikes));
public Set<String> searchUserByIdWith(Post postRelated) {
return getUserIdFromId(searchIdWith(new Like(null, postRelated), indexLikes));
}
public Set<String> searchUserByIdWith(String postId) {
return searchUserByIdWith(makePost(postId));
}
public Set<String> searchLikedByIdWithLikes(String likesId) {
return searchLikedByIdWithLikes(makePost(likesId));
public Set<String> searchPostByIdWith(User userRelated) {
return getPostIdFromId(searchIdWith(new Like(userRelated, null), indexLikedBy));
}
public Set<String> searchLikesIdWithLikedBy(User likedBy) {
return getLikesIdFromId(searchIdWith(new Like(likedBy, null), indexLikedBy));
public Set<String> searchPostByIdWith(String likedById) {
return searchPostByIdWith(makeUser(likedById));
}
public Set<String> searchLikesIdWithLikedby(String likedById) {
return searchLikesIdWithLikedBy(makeUser(likedById));
public void put(Post postRelated, String userId) {
super.put(new Like(makeUser(userId), postRelated));
}
public void put(Post likes, String likedById) {
super.put(new Like(makeUser(likedById), likes));
public void put(User userRelated, String postId) {
super.put(new Like(userRelated, makePost(postId)));
}
}
......@@ -2,20 +2,23 @@ package fr.univnantes.webandcloud.api.services.db.index.like;
import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.Like;
import fr.univnantes.webandcloud.api.services.IndexedFieldOf;
import org.springframework.stereotype.Service;
@Service
public class IndexLikedBy extends IndexedFieldOf<Like> {
public class IndexPostRelated extends IndexedFieldOf<Like> {
@Override
public String getFieldName() {
return "likedBy";
return "postRelated";
}
@Override
public Value<?> getValue(Like entityUsed) {
return new StringValue(entityUsed.likedBy.getId());
return new StringValue(entityUsed.postRelated.getId());
}
}
......@@ -2,21 +2,22 @@ package fr.univnantes.webandcloud.api.services.db.index.like;
import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.Like;
import fr.univnantes.webandcloud.api.services.IndexedFieldOf;
import org.springframework.stereotype.Service;
@Service
public class IndexLikes extends IndexedFieldOf<Like> {
public class IndexUserRelated extends IndexedFieldOf<Like> {
@Override
public String getFieldName() {
return "likes";
return "userRelated";
}
@Override
public Value<?> getValue(Like entityUsed) {
return new StringValue(entityUsed.likes.getId());
return new StringValue(entityUsed.userRelated.getId());
}
}
......@@ -4,14 +4,14 @@ import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Entity.Builder;
import fr.univnantes.webandcloud.api.core.Following;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import fr.univnantes.webandcloud.api.core.Like;
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.DBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LikeUtils extends BaseUtils<Like>{
......@@ -24,8 +24,8 @@ public class LikeUtils extends BaseUtils<Like>{
@Override
public Builder setEntityInBuilder(Builder builder, Like instance) {
builder.set("likedBy", instance.likedBy.getId());
builder.set("likes", instance.likes.getId());
builder.set("userRelated", instance.userRelated.getId());
builder.set("postRelated", instance.postRelated.getId());
return builder;
}
......@@ -37,10 +37,10 @@ public class LikeUtils extends BaseUtils<Like>{
@Override
public Like createFromEntity(Entity entity) {
try {
User likedBy = userDB.get(entity.getString("likedBy"));
User userRelated = userDB.get(entity.getString("userRelated"));
try {
Post likes = postDB.get(entity.getString("likes"));
return new Like(likedBy, likes);
Post postRelated = postDB.get(entity.getString("postRelated"));
return new Like(userRelated, postRelated);
} catch (ResponseError ignored) {
throw new RuntimeException(String.format("Integrity error of like %s (post missing)",
entity.getKey().getName()));
......@@ -54,10 +54,10 @@ public class LikeUtils extends BaseUtils<Like>{
@Override
public Like createFromEntityWithoutLinks(Entity entity) {
try {
User likedBy = new User(entity.getString("likedBy"), null, null, null);
User userRelated = new User(entity.getString("userRelated"), null, null, null);
try {
Post likes = new Post(entity.getString("likes"), null, null, null, null, null, null);
return new Like(likedBy, likes);
Post postRelated = new Post(entity.getString("postRelated"), null, null, null, null, null, null);
return new Like(userRelated, postRelated);
} catch (DatastoreException ignored) {
throw new RuntimeException(String.format("Integrity error of like %s (post missing)",
entity.getKey().getName()));
......
......@@ -8,10 +8,7 @@ import java.util.Set;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Entity.Builder;
import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.Value;
import fr.univnantes.webandcloud.api.services.db.DBLike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -21,6 +18,7 @@ import fr.univnantes.webandcloud.api.responses.ResponseError;
import fr.univnantes.webandcloud.api.services.DBService;
import fr.univnantes.webandcloud.api.services.WithIdentifierService;
import fr.univnantes.webandcloud.api.services.db.DBFollowing;
import fr.univnantes.webandcloud.api.services.db.DBLike;
@Service
public class UserUtils extends BaseUtils<User> {
......@@ -54,17 +52,6 @@ public class UserUtils extends BaseUtils<User> {
try {
String email = entity.getString("email");
List<Post> likedPosts = new ArrayList<>();
try {
for (Value<?> postId : entity.getList("likedPostsId")) {
try {
likedPosts.add(postDb.getWithoutLinks((String) postId.get()));
} catch (ResponseError ignored) {
// likedPost may be disappear if deleted
}
}
} catch (DatastoreException ignored) {
// likedPosts may be empty, exception is ignored
}
List<User> follows = new ArrayList<>();
for (String followId : followingDB.searchFollowedIdWithFollowing(entity.getKey().getName())) {
try {
......@@ -98,11 +85,8 @@ public class UserUtils extends BaseUtils<User> {
if (instance.getUri() != null)
builder.set("uri", instance.getUri());
builder.set("email", instance.getEmail());
List<StringValue> likedPostsId = new ArrayList<>();
for (Post likedPost : instance.getLikedPosts())
likedPostsId.add(new StringValue(likedPost.getId()));
builder.set("likedPostsId", likedPostsId);
updateFollows(instance);
updateLikes(instance);
return builder;
}
......@@ -149,18 +133,17 @@ public class UserUtils extends BaseUtils<User> {
}
}
protected void updateLikes(Post instance) {
Set<String> likes = withIdentifierService.getIdOf(instance.getLikes());
Set<String> keys = likeDB.searchLikedByIdWithLikes(instance);
protected void updateLikes(User instance) {
Set<String> posts = withIdentifierService.getIdOf(instance.getLikedPosts());
Set<String> keys = likeDB.searchPostByIdWith(instance);
Set<String> initialsKeys = new HashSet<>();
initialsKeys.addAll(keys);
for (String liked : likes)
for (String liked : posts)
if (!keys.contains(liked))
keys.add(liked);
if (keys.size() < likes.size())
if (keys.size() < posts.size())
for (String key : keys)
if (!likes.contains(key))
if (!posts.contains(key))
keys.remove(key);
for (String key : keys)
if (!initialsKeys.contains(key))
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter