Skip to content

Commit

Permalink
Update parsing of Tags
Browse files Browse the repository at this point in the history
to show user which tag has incorrect format
  • Loading branch information
lennoxtr committed Mar 18, 2023
1 parent 58fab50 commit 281655d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
42 changes: 22 additions & 20 deletions src/main/java/seedu/address/logic/commands/TagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.lecture.Lecture;
Expand All @@ -26,14 +27,9 @@
public class TagCommand extends Command {
public static final String COMMAND_WORD = "tag";

//TODO: MODIFY THIS
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Tag a specified video, module, or lecture ";
//TODO: MODIFY THIS
public static final String MESSAGE_SUCCESS = "Item tagged";
public static final String MESSAGE_MODULE_NOT_FOUND = "Module doesn't exist in Le Tracker";
public static final String MESSAGE_LECTURE_NOT_FOUND = "Lecture doesn't exist in this module";
public static final String MESSAGE_VIDEO_NOT_FOUND = "Video doesn't exist in this lecture";

public static final String MESSAGE_SUCCESS = "%1$s tagged";

private final Set<Tag> tags;

Expand Down Expand Up @@ -95,18 +91,17 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (this.isTaggingMod) {
tagModule(model);
return tagModule(model);
} else if (this.isTaggingLec) {
tagLecture(model);
} else if (this.isTaggingVid) {
tagVideo(model);
return tagLecture(model);
} else {
return tagVideo(model);
}
return new CommandResult(MESSAGE_SUCCESS);
}

private void tagModule(Model model) throws CommandException {
private CommandResult tagModule(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}

ReadOnlyModule taggingModule = model.getModule(this.moduleCode);
Expand All @@ -122,15 +117,17 @@ private void tagModule(Model model) throws CommandException {
Module taggedModule = new Module(taggingModule.getCode(),
taggingModule.getName(), newTags, currentLectureList);
model.setModule(taggingModule, taggedModule);
return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode));
}

private void tagLecture(Model model) throws CommandException {
private CommandResult tagLecture(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}

if (!model.hasLecture(this.moduleCode, this.lectureName)) {
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName,
moduleCode));
}

ReadOnlyModule targetModule = model.getModule(this.moduleCode);
Expand All @@ -143,22 +140,26 @@ private void tagLecture(Model model) throws CommandException {

Lecture taggedLecture = new Lecture(taggingLecture.getName(), newTags, taggingLecture.getVideoList());
model.setLecture(targetModule, taggingLecture, taggedLecture);
return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName));
}

private void tagVideo(Model model) throws CommandException {
private CommandResult tagVideo(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}

if (!model.hasLecture(this.moduleCode, this.lectureName)) {
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName,
moduleCode));
}

ReadOnlyModule targetModule = model.getModule(this.moduleCode);
ReadOnlyLecture targetLecture = targetModule.getLecture(this.lectureName);

if (!model.hasVideo(targetLecture, this.videoName)) {
throw new CommandException(MESSAGE_VIDEO_NOT_FOUND);
throw new CommandException(String.format(Messages.MESSAGE_VIDEO_DOES_NOT_EXIST, this.videoName,
this.lectureName,
this.moduleCode));
}

Video taggingVideo = targetLecture.getVideo(this.videoName);
Expand All @@ -170,5 +171,6 @@ private void tagVideo(Model model) throws CommandException {

Video taggedVideo = new Video(taggingVideo.getName(), taggingVideo.hasWatched(), newTags);
model.setVideo(targetLecture, taggingVideo, taggedVideo);
return new CommandResult(String.format(MESSAGE_SUCCESS, videoName));
}
}
20 changes: 11 additions & 9 deletions src/main/java/seedu/address/logic/commands/UntagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UntagCommand extends Command {
//TODO: MODIFY THIS
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Untag a specified video, module, or lecture ";
//TODO: MODIFY THIS
public static final String MESSAGE_SUCCESS = "Item untagged";
public static final String MESSAGE_SUCCESS = "%1$s untagged";
private final Tag tag;
private final VideoName videoName;
private final LectureName lectureName;
Expand Down Expand Up @@ -93,16 +93,15 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (this.isUntaggingMod) {
untagModule(model);
return untagModule(model);
} else if (this.isUntaggingLec) {
untagLecture(model);
} else if (this.isUntaggingLec) {
untagVideo(model);
return untagLecture(model);
} else {
return untagVideo(model);
}
return new CommandResult(MESSAGE_SUCCESS);
}

private void untagModule(Model model) throws CommandException {
private CommandResult untagModule(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}
Expand All @@ -126,9 +125,10 @@ private void untagModule(Model model) throws CommandException {
Module untaggedModule = new Module(untaggingModule.getCode(),
untaggingModule.getName(), newTags, currentLectureList);
model.setModule(untaggingModule, untaggedModule);
return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode));
}

private void untagLecture(Model model) throws CommandException {
private CommandResult untagLecture(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}
Expand All @@ -155,9 +155,10 @@ private void untagLecture(Model model) throws CommandException {

Lecture untaggedLecture = new Lecture(untaggingLecture.getName(), newTags, untaggingLecture.getVideoList());
model.setLecture(targetModule, untaggingLecture, untaggedLecture);
return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName));
}

private void untagVideo(Model model) throws CommandException {
private CommandResult untagVideo(Model model) throws CommandException {
if (!model.hasModule(moduleCode)) {
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
}
Expand Down Expand Up @@ -191,5 +192,6 @@ private void untagVideo(Model model) throws CommandException {

Video taggedVideo = new Video(untaggingVideo.getName(), untaggingVideo.hasWatched(), newTags);
model.setVideo(targetLecture, untaggingVideo, taggedVideo);
return new CommandResult(String.format(MESSAGE_SUCCESS, videoName));
}
}
15 changes: 9 additions & 6 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static Tag parseSingleTag(String tag) throws ParseException {
requireNonNull(tag);
String trimmedTag = tag.trim();
if (!Tag.isValidTagName(trimmedTag)) {
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS, trimmedTag));
}
return new Tag(trimmedTag);
}
Expand All @@ -135,11 +135,14 @@ public static Set<Tag> parseMultiTags(String tags) throws ParseException {

String[] arrayOfTags = tags.split(",");

for (String tagName : arrayOfTags) {
tagName = tagName.trim();
if (!Tag.isValidTagName(tagName)) {
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
}
List<String> listOfUnvalidTagName = Arrays.stream(arrayOfTags)
.map(tag -> tag.trim())
.filter(trimmedTag -> !Tag.isValidTagName(trimmedTag))
.collect(Collectors.toList());

if (listOfUnvalidTagName.size() > 0) {
throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS,
String.join(", ", listOfUnvalidTagName)));
}

List<Tag> listOfTags = Arrays.stream(arrayOfTags)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Tag {

public static final String MESSAGE_CONSTRAINTS = "Tags names should be alphanumeric";
public static final String MESSAGE_CONSTRAINTS = "Tag %1$s should be alphanumeric";
public static final String VALIDATION_REGEX = "\\p{Alnum}+";

public final String tagName;
Expand Down

0 comments on commit 281655d

Please sign in to comment.