forked from nus-cs2103-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-edit-command
- Loading branch information
Showing
16 changed files
with
692 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
src/main/java/seedu/address/logic/commands/TagCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.lecture.Lecture; | ||
import seedu.address.model.lecture.LectureName; | ||
import seedu.address.model.lecture.ReadOnlyLecture; | ||
import seedu.address.model.module.Module; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.ReadOnlyModule; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.video.Video; | ||
import seedu.address.model.video.VideoName; | ||
|
||
/** | ||
* Tag a video, a lecture, or a module. | ||
*/ | ||
|
||
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"; | ||
|
||
|
||
private final Set<Tag> tags; | ||
|
||
private final VideoName videoName; | ||
private final LectureName lectureName; | ||
private final ModuleCode moduleCode; | ||
private final boolean isTaggingMod; | ||
private final boolean isTaggingLec; | ||
private final boolean isTaggingVid; | ||
|
||
/** | ||
* Creates a TagCommand to tag the specified {@code Module} | ||
*/ | ||
|
||
public TagCommand(Set<Tag> tags, ModuleCode moduleCode) { | ||
requireAllNonNull(tags, moduleCode); | ||
|
||
this.tags = tags; | ||
this.videoName = new VideoName("dummy"); | ||
this.lectureName = new LectureName("dummy"); | ||
this.moduleCode = moduleCode; | ||
this.isTaggingMod = true; | ||
this.isTaggingLec = false; | ||
this.isTaggingVid = false; | ||
} | ||
|
||
/** | ||
* Creates a TagCommand to tag the specified {@code Lecture} | ||
*/ | ||
public TagCommand(Set<Tag> tags, ModuleCode moduleCode, LectureName lectureName) { | ||
requireAllNonNull(tags, moduleCode, lectureName); | ||
|
||
this.tags = tags; | ||
this.videoName = new VideoName("dummy"); | ||
this.lectureName = lectureName; | ||
this.moduleCode = moduleCode; | ||
this.isTaggingMod = false; | ||
this.isTaggingLec = true; | ||
this.isTaggingVid = false; | ||
} | ||
|
||
/** | ||
* Creates a TagCommand to tag the specified {@code Video} | ||
*/ | ||
public TagCommand(Set<Tag> tags, ModuleCode moduleCode, LectureName lectureName, VideoName videoName) { | ||
requireAllNonNull(tags, moduleCode, lectureName, videoName); | ||
|
||
this.tags = tags; | ||
this.videoName = videoName; | ||
this.lectureName = lectureName; | ||
this.moduleCode = moduleCode; | ||
this.isTaggingMod = false; | ||
this.isTaggingLec = false; | ||
this.isTaggingVid = true; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (this.isTaggingMod) { | ||
tagModule(model); | ||
} else if (this.isTaggingLec) { | ||
tagLecture(model); | ||
} else if (this.isTaggingVid) { | ||
tagVideo(model); | ||
} | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
private void tagModule(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(MESSAGE_MODULE_NOT_FOUND); | ||
} | ||
|
||
ReadOnlyModule taggingModule = model.getModule(this.moduleCode); | ||
|
||
Set<Tag> currentTags = taggingModule.getTags(); | ||
|
||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(this.tags); | ||
newTags.addAll(currentTags); | ||
|
||
List<Lecture> currentLectureList = (List<Lecture>) taggingModule.getLectureList(); | ||
|
||
Module taggedModule = new Module(taggingModule.getCode(), | ||
taggingModule.getName(), newTags, currentLectureList); | ||
model.setModule(taggingModule, taggedModule); | ||
} | ||
|
||
private void tagLecture(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(MESSAGE_MODULE_NOT_FOUND); | ||
} | ||
|
||
if (!model.hasLecture(this.moduleCode, this.lectureName)) { | ||
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND); | ||
} | ||
|
||
ReadOnlyModule targetModule = model.getModule(this.moduleCode); | ||
ReadOnlyLecture taggingLecture = targetModule.getLecture(this.lectureName); | ||
|
||
Set<Tag> currentTags = taggingLecture.getTags(); | ||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(this.tags); | ||
newTags.addAll(currentTags); | ||
|
||
Lecture taggedLecture = new Lecture(taggingLecture.getName(), newTags, taggingLecture.getVideoList()); | ||
model.setLecture(targetModule, taggingLecture, taggedLecture); | ||
} | ||
|
||
private void tagVideo(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(MESSAGE_MODULE_NOT_FOUND); | ||
} | ||
|
||
if (!model.hasLecture(this.moduleCode, this.lectureName)) { | ||
throw new CommandException(MESSAGE_LECTURE_NOT_FOUND); | ||
} | ||
|
||
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); | ||
} | ||
|
||
Video taggingVideo = targetLecture.getVideo(this.videoName); | ||
|
||
Set<Tag> currentTags = taggingVideo.getTags(); | ||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(this.tags); | ||
newTags.addAll(currentTags); | ||
|
||
Video taggedVideo = new Video(taggingVideo.getName(), taggingVideo.hasWatched(), newTags); | ||
model.setVideo(targetLecture, taggingVideo, taggedVideo); | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
src/main/java/seedu/address/logic/commands/UntagCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.lecture.Lecture; | ||
import seedu.address.model.lecture.LectureName; | ||
import seedu.address.model.lecture.ReadOnlyLecture; | ||
import seedu.address.model.module.Module; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.ReadOnlyModule; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.video.Video; | ||
import seedu.address.model.video.VideoName; | ||
|
||
/** | ||
* Tag a video, a lecture, or a module. | ||
*/ | ||
|
||
public class UntagCommand extends Command { | ||
public static final String COMMAND_WORD = "untag"; | ||
|
||
//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"; | ||
private final Tag tag; | ||
private final VideoName videoName; | ||
private final LectureName lectureName; | ||
private final ModuleCode moduleCode; | ||
private final boolean isUntaggingMod; | ||
private final boolean isUntaggingLec; | ||
private final boolean isUntaggingVid; | ||
|
||
/** | ||
* Creates an UntagCommand to untag the specified {@code Module} | ||
*/ | ||
|
||
|
||
public UntagCommand(Tag tag, ModuleCode moduleCode) { | ||
requireAllNonNull(tag, moduleCode); | ||
|
||
this.tag = tag; | ||
this.videoName = new VideoName("dummy"); | ||
this.lectureName = new LectureName("dummy"); | ||
this.moduleCode = moduleCode; | ||
this.isUntaggingMod = true; | ||
this.isUntaggingLec = false; | ||
this.isUntaggingVid = false; | ||
} | ||
|
||
/** | ||
* Creates an UntagCommand to untag the specified {@code Lecture} | ||
*/ | ||
|
||
public UntagCommand(Tag tag, ModuleCode moduleCode, LectureName lectureName) { | ||
requireAllNonNull(tag, moduleCode, lectureName); | ||
|
||
this.tag = tag; | ||
this.videoName = new VideoName("dummy"); | ||
this.lectureName = lectureName; | ||
this.moduleCode = moduleCode; | ||
this.isUntaggingMod = false; | ||
this.isUntaggingLec = true; | ||
this.isUntaggingVid = false; | ||
} | ||
|
||
/** | ||
* Creates an UntagCommand to untag the specified {@code Video} | ||
*/ | ||
public UntagCommand(Tag tag, ModuleCode moduleCode, LectureName lectureName, VideoName videoName) { | ||
requireAllNonNull(tag, moduleCode, lectureName, videoName); | ||
|
||
this.tag = tag; | ||
this.videoName = videoName; | ||
this.lectureName = lectureName; | ||
this.moduleCode = moduleCode; | ||
this.isUntaggingMod = false; | ||
this.isUntaggingLec = false; | ||
this.isUntaggingVid = true; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (this.isUntaggingMod) { | ||
untagModule(model); | ||
} else if (this.isUntaggingLec) { | ||
untagLecture(model); | ||
} else if (this.isUntaggingLec) { | ||
untagVideo(model); | ||
} | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
|
||
private void untagModule(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); | ||
} | ||
|
||
ReadOnlyModule untaggingModule = model.getModule(this.moduleCode); | ||
|
||
Set<Tag> currentTags = untaggingModule.getTags(); | ||
|
||
if (!currentTags.contains(this.tag)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_MODULE_TAG_DOES_NOT_EXIST, this.tag, moduleCode)); | ||
} | ||
|
||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(currentTags); | ||
newTags = newTags.stream().filter(tag -> !tag.equals(this.tag)) | ||
.collect(Collectors.toSet()); | ||
|
||
|
||
List<Lecture> currentLectureList = (List<Lecture>) untaggingModule.getLectureList(); | ||
|
||
Module untaggedModule = new Module(untaggingModule.getCode(), | ||
untaggingModule.getName(), newTags, currentLectureList); | ||
model.setModule(untaggingModule, untaggedModule); | ||
} | ||
|
||
private void untagLecture(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); | ||
} | ||
|
||
if (!model.hasLecture(this.moduleCode, this.lectureName)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName, | ||
moduleCode)); | ||
} | ||
|
||
ReadOnlyModule targetModule = model.getModule(this.moduleCode); | ||
ReadOnlyLecture untaggingLecture = targetModule.getLecture(this.lectureName); | ||
|
||
Set<Tag> currentTags = untaggingLecture.getTags(); | ||
|
||
if (!currentTags.contains(this.tag)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_TAG_DOES_NOT_EXIST, this.tag, | ||
this.lectureName, this.moduleCode)); | ||
} | ||
|
||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(currentTags); | ||
newTags = newTags.stream().filter(tag -> !tag.equals(this.tag)) | ||
.collect(Collectors.toSet()); | ||
|
||
Lecture untaggedLecture = new Lecture(untaggingLecture.getName(), newTags, untaggingLecture.getVideoList()); | ||
model.setLecture(targetModule, untaggingLecture, untaggedLecture); | ||
} | ||
|
||
private void untagVideo(Model model) throws CommandException { | ||
if (!model.hasModule(moduleCode)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); | ||
} | ||
|
||
if (!model.hasLecture(this.moduleCode, this.lectureName)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName, | ||
this.moduleCode)); | ||
} | ||
|
||
ReadOnlyModule targetModule = model.getModule(this.moduleCode); | ||
ReadOnlyLecture targetLecture = targetModule.getLecture(this.lectureName); | ||
|
||
if (!model.hasVideo(targetLecture, this.videoName)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_VIDEO_DOES_NOT_EXIST, this.videoName, | ||
this.lectureName, | ||
this.moduleCode)); | ||
} | ||
|
||
Video untaggingVideo = targetLecture.getVideo(this.videoName); | ||
|
||
Set<Tag> currentTags = untaggingVideo.getTags(); | ||
if (!currentTags.contains(this.tag)) { | ||
throw new CommandException(String.format(Messages.MESSAGE_VIDEO_TAG_DOES_NOT_EXIST, this.tag, | ||
this.videoName, this.lectureName, this.moduleCode)); | ||
} | ||
|
||
Set<Tag> newTags = new HashSet<>(); | ||
newTags.addAll(currentTags); | ||
newTags = newTags.stream().filter(tag -> !tag.equals(this.tag)) | ||
.collect(Collectors.toSet()); | ||
|
||
Video taggedVideo = new Video(untaggingVideo.getName(), untaggingVideo.hasWatched(), newTags); | ||
model.setVideo(targetLecture, untaggingVideo, taggedVideo); | ||
} | ||
} |
Oops, something went wrong.