Skip to content

Commit

Permalink
Merge pull request #208
Browse files Browse the repository at this point in the history
Tag categories must be saved properly in json
  • Loading branch information
KrashKart authored Nov 4, 2024
2 parents a783838 + 5876e89 commit 34da714
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Adds the specified person's tag.

Format: `addtag INDEX t/KEYWORD [t/MORE_TAGS]`

* Deletes the tag with the specified name `KEYWORD` of the person at the specified `INDEX`.
* Adds the tags with the specified name `KEYWORD` of the person at the specified `INDEX`.
* The index refers to the index number shown in the displayed person list.
* The index **must be a positive integer** 1, 2, 3, …​
* Multiple tags can be added at a time.
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/tag/TagCategory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model.tag;

import seedu.address.commons.exceptions.IllegalValueException;

/**
* Provides categories to sort Tags into, as well as colour codes for display in the UI.
*/
Expand All @@ -10,12 +12,39 @@ public enum TagCategory {
NETWORKING("#32CD32"), // Lime Green
MENTORSHIP("#FF69B4"); // Hot Pink

private static final String INVALID_CATEGORY = "Error! %s is not a valid category!";
private final String colorCode;

TagCategory(String colorCode) {
this.colorCode = colorCode;
}

/**
* Returns TagCategory from a String.
* @return TagCategory represented by the String.
*/
public static TagCategory fromString(String category) throws IllegalValueException {
switch (category) {
case "ACADEMICS":
return ACADEMICS;

case "ACTIVITIES":
return ACTIVITIES;

case "NETWORKING":
return NETWORKING;

case "MENTORSHIP":
return MENTORSHIP;

case "GENERAL":
return GENERAL;

default:
throw new IllegalValueException(String.format(INVALID_CATEGORY, category));
}
}

/**
* Returns the colour code of the {@code TagCategory}.
* @return String containing the hex code for the colour.
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package seedu.address.model.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

//import seedu.address.model.CampusConnect;
//import seedu.address.model.ReadOnlyCampusConnect;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagCategory;

/**
* Contains utility methods for populating {@code CampusConnect} with sample data.
Expand Down Expand Up @@ -44,11 +46,33 @@ public static ReadOnlyCampusConnect getSampleCampusConnect() {
}*/

/**
* Returns a tag set containing the list of strings given.
* Returns a tag set containing the list of name strings given.
* @param names Varargs array containing the names of the tags.
* @return Set containing the tags.
*/
public static Set<Tag> getTagSet(String... strings) {
return Arrays.stream(strings)
public static Set<Tag> getTagSet(String... names) {
return Arrays.stream(names)
.map(Tag::new)
.collect(Collectors.toSet());
}

/**
* Returns a tag set containing tags with the names and categories specified.
* @param names Array of names for the tags.
* @param categories Array of categories to be attached to the respective names.
* @return Set containing the tags.
* @throws IllegalValueException thrown when a tag category is not valid.
*/
public static Set<Tag> getTagSet(String[] names, String[] categories) throws IllegalValueException {
List<Tag> resultTags = new ArrayList<>();
TagCategory tagCat;

// assume both arrays same length, since this is a helper for test functions
// so no need to test for inconsistent length
for (int i = 0; i < names.length; i++) {
tagCat = TagCategory.fromString(categories[i]);
resultTags.add(new Tag(names[i], tagCat));
}
return resultTags.stream().collect(Collectors.toSet());
}
}
27 changes: 18 additions & 9 deletions src/main/java/seedu/address/storage/JsonAdaptedTag.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package seedu.address.storage;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagCategory;

/**
* Jackson-friendly version of {@link Tag}.
*/
class JsonAdaptedTag {

private final String tagName;
private final String tagCategory;

/**
* Constructs a {@code JsonAdaptedTag} with the given {@code tagName}.
* Constructs a {@code JsonAdaptedTag} with the given {@code tagName} and {@code tagCategory}.
*/
@JsonCreator
public JsonAdaptedTag(String tagName) {
public JsonAdaptedTag(@JsonProperty("tagName") String tagName,
@JsonProperty("tagCategory") String tagCategory) {
this.tagName = tagName;
this.tagCategory = tagCategory;
}

/**
* Overloaded constructor for default tag creation.
*/
@JsonCreator
public JsonAdaptedTag(String tagName) {
this(tagName, "GENERAL");
}

/**
* Converts a given {@code Tag} into this class for Jackson use.
*/
public JsonAdaptedTag(Tag source) {
tagName = source.tagName;
}

@JsonValue
public String getTagName() {
return tagName;
tagCategory = source.getTagCategory().toString();
}

/**
Expand All @@ -42,7 +50,8 @@ public Tag toModelType() throws IllegalValueException {
if (!Tag.isValidTagName(tagName)) {
throw new IllegalValueException(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(tagName);
TagCategory tagCat = TagCategory.fromString(tagCategory); // convert to TagCategory enum
return new Tag(tagName, tagCat);
}

}
33 changes: 33 additions & 0 deletions src/test/java/seedu/address/model/tag/TagCategoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.model.tag;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static seedu.address.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;

import seedu.address.commons.exceptions.IllegalValueException;

public class TagCategoryTest {

@Test
public void fromString_validCategories_returnsSuccess() throws IllegalValueException {
// all valid inputs
assertEquals(TagCategory.GENERAL, TagCategory.fromString("GENERAL"));
assertEquals(TagCategory.ACADEMICS, TagCategory.fromString("ACADEMICS"));
assertEquals(TagCategory.NETWORKING, TagCategory.fromString("NETWORKING"));
assertEquals(TagCategory.MENTORSHIP, TagCategory.fromString("MENTORSHIP"));
assertEquals(TagCategory.ACTIVITIES, TagCategory.fromString("ACTIVITIES"));
}

@Test
public void fromString_invalidCategories_throwsIllegalValueException() {
// completely invalid inputs
assertThrows(IllegalValueException.class, () -> TagCategory.fromString("HEHE"));
assertThrows(IllegalValueException.class, () -> TagCategory.fromString("ACACACACAC"));

// wrong case
assertThrows(IllegalValueException.class, () -> TagCategory.fromString("Academics"));
assertThrows(IllegalValueException.class, () -> TagCategory.fromString("GeNeRaL"));
assertThrows(IllegalValueException.class, () -> TagCategory.fromString("aCtIViTieS"));
}
}
18 changes: 15 additions & 3 deletions src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public class JsonAdaptedPersonTest {
private static final String INVALID_NAME = "R@chel";
private static final String INVALID_PHONE = "+651234";
private static final String INVALID_EMAIL = "example.com";
private static final String INVALID_TAG = "#friend";
private static final String INVALID_TAG_NAME = "#friend";
private static final String INVALID_TAG_CATEGORY = "hehe";

private static final String VALID_NAME = BENSON.getName().toString();
private static final String VALID_PHONE = BENSON.getPhone().toString();
private static final String VALID_EMAIL = BENSON.getEmail().toString();
private static final String VALID_TAG_NAME = "colleague";
private static final String VALID_TAG_CATEGORY = "acads";
private static final List<JsonAdaptedTag> VALID_TAGS = BENSON.getTags().stream()
.map(JsonAdaptedTag::new)
.collect(Collectors.toList());
Expand Down Expand Up @@ -81,9 +84,18 @@ public void toModelType_nullEmail_throwsIllegalValueException() {
}

@Test
public void toModelType_invalidTags_throwsIllegalValueException() {
public void toModelType_invalidTagName_throwsIllegalValueException() {
List<JsonAdaptedTag> invalidTags = new ArrayList<>(VALID_TAGS);
invalidTags.add(new JsonAdaptedTag(INVALID_TAG));
invalidTags.add(new JsonAdaptedTag(INVALID_TAG_NAME, VALID_TAG_CATEGORY));
JsonAdaptedPerson person =
new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, invalidTags);
assertThrows(IllegalValueException.class, person::toModelType);
}

@Test
public void toModelType_invalidTagCategory_throwsIllegalValueException() {
List<JsonAdaptedTag> invalidTags = new ArrayList<>(VALID_TAGS);
invalidTags.add(new JsonAdaptedTag(VALID_TAG_NAME, INVALID_TAG_CATEGORY));
JsonAdaptedPerson person =
new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, invalidTags);
assertThrows(IllegalValueException.class, person::toModelType);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/seedu/address/testutil/PersonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.Set;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -60,6 +61,16 @@ public PersonBuilder withTags(String ... tags) {
return this;
}

/**
* Parses the {@code tagNames} and the respective {@tagCategories} into a {@code Set<Tag>} and
* set it to the {@code Person} that we are building.
* @throws IllegalValueException thrown when a tag category is invalid
*/
public PersonBuilder withTags(String[] tags, String[] categories) throws IllegalValueException {
this.tags = SampleDataUtil.getTagSet(tags, categories);
return this;
}

/**
* Sets the {@code Phone} of the {@code Person} that we are building.
*/
Expand Down

0 comments on commit 34da714

Please sign in to comment.