Skip to content

Commit 2d3bbf9

Browse files
authored
Merge pull request nus-cs2103-AY2223S2#119 from lennoxtr/master
Update parsing of Tags
2 parents 58fab50 + 281655d commit 2d3bbf9

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

src/main/java/seedu/address/logic/commands/TagCommand.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88
import java.util.Set;
99

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

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

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

3834
private final Set<Tag> tags;
3935

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

9793
if (this.isTaggingMod) {
98-
tagModule(model);
94+
return tagModule(model);
9995
} else if (this.isTaggingLec) {
100-
tagLecture(model);
101-
} else if (this.isTaggingVid) {
102-
tagVideo(model);
96+
return tagLecture(model);
97+
} else {
98+
return tagVideo(model);
10399
}
104-
return new CommandResult(MESSAGE_SUCCESS);
105100
}
106101

107-
private void tagModule(Model model) throws CommandException {
102+
private CommandResult tagModule(Model model) throws CommandException {
108103
if (!model.hasModule(moduleCode)) {
109-
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
104+
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
110105
}
111106

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

127-
private void tagLecture(Model model) throws CommandException {
123+
private CommandResult tagLecture(Model model) throws CommandException {
128124
if (!model.hasModule(moduleCode)) {
129-
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
125+
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
130126
}
131127

132128
if (!model.hasLecture(this.moduleCode, this.lectureName)) {
133-
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND);
129+
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName,
130+
moduleCode));
134131
}
135132

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

144141
Lecture taggedLecture = new Lecture(taggingLecture.getName(), newTags, taggingLecture.getVideoList());
145142
model.setLecture(targetModule, taggingLecture, taggedLecture);
143+
return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName));
146144
}
147145

148-
private void tagVideo(Model model) throws CommandException {
146+
private CommandResult tagVideo(Model model) throws CommandException {
149147
if (!model.hasModule(moduleCode)) {
150-
throw new CommandException(MESSAGE_MODULE_NOT_FOUND);
148+
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
151149
}
152150

153151
if (!model.hasLecture(this.moduleCode, this.lectureName)) {
154-
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND);
152+
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName,
153+
moduleCode));
155154
}
156155

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

160159
if (!model.hasVideo(targetLecture, this.videoName)) {
161-
throw new CommandException(MESSAGE_VIDEO_NOT_FOUND);
160+
throw new CommandException(String.format(Messages.MESSAGE_VIDEO_DOES_NOT_EXIST, this.videoName,
161+
this.lectureName,
162+
this.moduleCode));
162163
}
163164

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

171172
Video taggedVideo = new Video(taggingVideo.getName(), taggingVideo.hasWatched(), newTags);
172173
model.setVideo(targetLecture, taggingVideo, taggedVideo);
174+
return new CommandResult(String.format(MESSAGE_SUCCESS, videoName));
173175
}
174176
}

src/main/java/seedu/address/logic/commands/UntagCommand.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class UntagCommand extends Command {
3131
//TODO: MODIFY THIS
3232
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Untag a specified video, module, or lecture ";
3333
//TODO: MODIFY THIS
34-
public static final String MESSAGE_SUCCESS = "Item untagged";
34+
public static final String MESSAGE_SUCCESS = "%1$s untagged";
3535
private final Tag tag;
3636
private final VideoName videoName;
3737
private final LectureName lectureName;
@@ -93,16 +93,15 @@ public CommandResult execute(Model model) throws CommandException {
9393
requireNonNull(model);
9494

9595
if (this.isUntaggingMod) {
96-
untagModule(model);
96+
return untagModule(model);
9797
} else if (this.isUntaggingLec) {
98-
untagLecture(model);
99-
} else if (this.isUntaggingLec) {
100-
untagVideo(model);
98+
return untagLecture(model);
99+
} else {
100+
return untagVideo(model);
101101
}
102-
return new CommandResult(MESSAGE_SUCCESS);
103102
}
104103

105-
private void untagModule(Model model) throws CommandException {
104+
private CommandResult untagModule(Model model) throws CommandException {
106105
if (!model.hasModule(moduleCode)) {
107106
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
108107
}
@@ -126,9 +125,10 @@ private void untagModule(Model model) throws CommandException {
126125
Module untaggedModule = new Module(untaggingModule.getCode(),
127126
untaggingModule.getName(), newTags, currentLectureList);
128127
model.setModule(untaggingModule, untaggedModule);
128+
return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode));
129129
}
130130

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

156156
Lecture untaggedLecture = new Lecture(untaggingLecture.getName(), newTags, untaggingLecture.getVideoList());
157157
model.setLecture(targetModule, untaggingLecture, untaggedLecture);
158+
return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName));
158159
}
159160

160-
private void untagVideo(Model model) throws CommandException {
161+
private CommandResult untagVideo(Model model) throws CommandException {
161162
if (!model.hasModule(moduleCode)) {
162163
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode));
163164
}
@@ -191,5 +192,6 @@ private void untagVideo(Model model) throws CommandException {
191192

192193
Video taggedVideo = new Video(untaggingVideo.getName(), untaggingVideo.hasWatched(), newTags);
193194
model.setVideo(targetLecture, untaggingVideo, taggedVideo);
195+
return new CommandResult(String.format(MESSAGE_SUCCESS, videoName));
194196
}
195197
}

src/main/java/seedu/address/logic/parser/ParserUtil.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static Tag parseSingleTag(String tag) throws ParseException {
115115
requireNonNull(tag);
116116
String trimmedTag = tag.trim();
117117
if (!Tag.isValidTagName(trimmedTag)) {
118-
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
118+
throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS, trimmedTag));
119119
}
120120
return new Tag(trimmedTag);
121121
}
@@ -135,11 +135,14 @@ public static Set<Tag> parseMultiTags(String tags) throws ParseException {
135135

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

138-
for (String tagName : arrayOfTags) {
139-
tagName = tagName.trim();
140-
if (!Tag.isValidTagName(tagName)) {
141-
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
142-
}
138+
List<String> listOfUnvalidTagName = Arrays.stream(arrayOfTags)
139+
.map(tag -> tag.trim())
140+
.filter(trimmedTag -> !Tag.isValidTagName(trimmedTag))
141+
.collect(Collectors.toList());
142+
143+
if (listOfUnvalidTagName.size() > 0) {
144+
throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS,
145+
String.join(", ", listOfUnvalidTagName)));
143146
}
144147

145148
List<Tag> listOfTags = Arrays.stream(arrayOfTags)

src/main/java/seedu/address/model/tag/Tag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class Tag {
1111

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

1515
public final String tagName;

0 commit comments

Comments
 (0)