Skip to content

Commit

Permalink
Merge pull request #86 from Grupo13-PES-Mascotas/feature/feature_Icon…
Browse files Browse the repository at this point in the history
…Medal

Feature/feature icon medal
  • Loading branch information
santidrj authored Jun 5, 2020
2 parents 7d6d018 + e77cc41 commit 5059bf2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ private void changeNameInTags(String currentName, String newName, WriteBatch bat
*/
private void deleteForumFromTag(String tag, String forum, WriteBatch batch) {
documentAdapter
.updateDocumentFields(Path.ofDocument(Collections.tags, tag), FORUMS_FIELD, FieldValue.arrayRemove(forum),
batch);
.updateDocumentFields(batch, Path.ofDocument(Collections.tags, tag), FORUMS_FIELD,
FieldValue.arrayRemove(forum));
}

/**
Expand All @@ -392,8 +392,8 @@ private void deleteForumFromTag(String tag, String forum, WriteBatch batch) {
*/
private void addNewTags(String groupId, String forumId, String forumName, List<String> newTags, WriteBatch batch)
throws DatabaseAccessException {
documentAdapter.updateDocumentFields(Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayUnion(newTags.toArray()), batch);
documentAdapter.updateDocumentFields(batch, Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayUnion(newTags.toArray()));
for (String tag : newTags) {
addForumToTag(tag, forumName, batch);
}
Expand All @@ -410,8 +410,8 @@ private void addNewTags(String groupId, String forumId, String forumName, List<S
*/
private void removeDeletedTags(String groupId, String forumId, String forumName, List<String> deletedTags,
WriteBatch batch) {
documentAdapter.updateDocumentFields(Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayRemove(deletedTags.toArray()), batch);
documentAdapter.updateDocumentFields(batch, Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayRemove(deletedTags.toArray()));
for (String tag : deletedTags) {
deleteForumFromTag(tag, forumName, batch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public List<UserMedalEntity> getAllUserMedalsData(String owner) throws DatabaseA
@Override
public void updateField(String owner, String name, String field, Object value)
throws DatabaseAccessException, DocumentException {
checkField(field);
initializeWithDocumentPath(owner, name);
dbDoc.updateDocumentFields(batch, path, field, value);
batch.commit();
Expand Down Expand Up @@ -128,4 +129,14 @@ private String getUserId(String username) throws DatabaseAccessException, Docume
return dbDoc.getStringFromDocument(usernamePath, "user");
}

/**
* This method check that the field are allowed to update.
* @param field The value of the field.
*/
private void checkField(String field) {
if (!("progress".equals(field) || "currentLevel".equals(field) || "completedLevelsDate".equals(field))) {
throw new IllegalArgumentException("Field not allowed to update.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class UserDaoImpl implements UserDao {
private static final String UPDATE_FAILED_CODE = "update-failed";
private static final String FIELD_LIKED_BY = "likedBy";
private static final String FCM = "FCM";
private static final String NOTIFICATIONS_FIELD = "notification-tokens";
private static final String WRITE_FAILED_CODE = "write-failed";
private FirebaseAuth myAuth;
private CollectionReference users;
Expand Down Expand Up @@ -493,9 +494,14 @@ private void updateTokenInSubscribedGroups(String token, String currentToken, Wr
ApiFuture<QuerySnapshot> subscribedGroups = collectionAdapter
.getDocumentsWhereArrayContains(Path.ofCollection(Collections.groups), "notification-tokens", currentToken);
try {
List<String> tokens;
for (QueryDocumentSnapshot group : subscribedGroups.get().getDocuments()) {
batch.update(group.getReference(), FCM, FieldValue.arrayRemove(currentToken), FCM,
FieldValue.arrayUnion(token));
tokens = (List<String>) group.get(NOTIFICATIONS_FIELD);
if (tokens != null) {
tokens.remove(currentToken);
tokens.add(token);
batch.update(group.getReference(), NOTIFICATIONS_FIELD, tokens);
}
}
} catch (InterruptedException e) {
throw new DatabaseAccessException(WRITE_FAILED_CODE, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public static void checkFieldAndValues(String field, Object newValue) {
&& !(newValue instanceof String)) {
throw new IllegalArgumentException("New value must be a String");
} else if ((field.equals(PROGRESS) || field.equals(CURRENT_LEVEL) || field.equals(LEVELS))
&& !(newValue instanceof Double)) {
throw new IllegalArgumentException("New value must be a Double");
&& !(newValue instanceof Double || newValue instanceof Integer)) {
throw new IllegalArgumentException("New value must be a Double or Integer");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author Oriol Catalán
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,19 @@ public void createForum() throws DatabaseAccessException, DocumentException {
public void updateTags() throws DatabaseAccessException, DocumentException {
mockGetGroupAndForumIds();
willDoNothing().given(documentAdapter)
.updateDocumentFields(anyString(), anyString(), any(FieldValue.class), any(WriteBatch.class));
.updateDocumentFields(any(WriteBatch.class), anyString(), anyString(), any(FieldValue.class));
mockAddForumToTag();

dao.updateTags(groupName, forumName, tags, tags);
verify(documentAdapter)
.updateDocumentFields(eq(forumPath), eq("tags"), eq(FieldValue.arrayRemove(tags.toArray())),
same(batch));
.updateDocumentFields(same(batch), eq(forumPath), eq("tags"),
eq(FieldValue.arrayRemove(tags.toArray())));
verify(documentAdapter)
.updateDocumentFields(eq(tagPath), eq("forums"), eq(FieldValue.arrayRemove(forumName)),
same(batch));
.updateDocumentFields(same(batch), eq(tagPath), eq("forums"),
eq(FieldValue.arrayRemove(forumName)));
verify(documentAdapter)
.updateDocumentFields(eq(forumPath), eq("tags"), eq(FieldValue.arrayUnion(tags.toArray())),
same(batch));
.updateDocumentFields(same(batch), eq(forumPath), eq("tags"),
eq(FieldValue.arrayUnion(tags.toArray())));
verifyAddForumToTag(forumName);
}

Expand Down Expand Up @@ -371,7 +371,7 @@ public void updateName() throws DatabaseAccessException, DocumentException {
.updateDocumentFields(anyString(), anyString(), anyString(), any(WriteBatch.class));
willDoNothing().given(documentAdapter).deleteDocument(anyString(), any(WriteBatch.class));
willDoNothing().given(documentAdapter)
.updateDocumentFields(anyString(), anyString(), any(FieldValue.class), any(WriteBatch.class));
.updateDocumentFields(any(WriteBatch.class), anyString(), anyString(), any(FieldValue.class));
mockAddForumToTag();
given(
documentAdapter.createDocumentWithId(anyString(), anyString(), anyMap(), any(WriteBatch.class)))
Expand Down

0 comments on commit 5059bf2

Please sign in to comment.