Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 7 course enrolment debugging #83

Merged
merged 7 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/entities/TaskMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class TaskMap implements Serializable {
private static HashMap<String, Task> taskMap;
Expand All @@ -26,6 +27,10 @@ public static void addTask(String id, Task task) {
taskMap.put(id, task);
}

public static void addTasks(Map newTasks) {
taskMap.putAll(newTasks);
}

/**
* Remove a Task from the TaskMap.txt
* @param id - the ID of the Task being removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import use_cases.login_registration.user_register_usecase.UserRegGateway;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -84,39 +85,44 @@ public CourseEnrolmentResponseModel enrol(CourseEnrolmentRequestModel requestMod
courseTaskId.add(taskTitleToId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

above on line 46/47, I think it should be if (!courseEnrolmentDsGateway.existsByCourseID(requestModel.getCourseID())) { (i.e. if course does NOT exist)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Thanks for catching it

}

// get task id : task object pairs from taskmap, save to old task id map
// find + get taskId-taskValue pairs from TaskMap, save to (temporary) oldTaskIdMap
HashMap<String, Task> oldTaskIdMap = new HashMap<>();
for (String oldTaskId : courseTaskId) {
Task taskValue = TaskMap.findTask(oldTaskId);
oldTaskIdMap.put(oldTaskId, taskValue);
Task taskValue = TaskMap.findTask(oldTaskId); // gets value from key
oldTaskIdMap.put(oldTaskId, taskValue); // add key-value pair to new map
}

// for each task id : Task pair, change the key name to courseTasks_student_course, add key-value pair to arraylist
// create new arraylist
HashMap<String, Task> newTaskIdMap = new HashMap<>();
// iterate over original arraylist and put key-value pair into new arraylist with modified key
for (HashMap.Entry<String, Task> entry : oldTaskIdMap.entrySet()) {
// key has old format title_instructor_course
// want to title, so split by _ which gives 'title', 'instructor', 'course'
// take first index which is just 'title'
// concatenating everything gives title_student_course
newTaskIdMap.put(entry.getKey().split("_")[0] + "_" + requestModel.getStudentID() + "_" + courseName,
(Task) oldTaskIdMap.entrySet());
HashMap<String, Task> newTaskIdMap = new HashMap<>(); // initialize new TaskIdMap

// create 2 parallel arraylists, one for keys, one for values
ArrayList<String> oldKeys = courseTaskId; // could also do (ArrayList) oldTaskIdMap.keySet()
ArrayList<Task> values = (ArrayList) oldTaskIdMap.entrySet();

ArrayList<String> newKeys = new ArrayList<>();
// change key name from title_instructor_course to title_student_course
for (String taskId :oldKeys) {
// change key name from title_inst_course to title_student_course
String newKey = taskId.replace(requestModel.getCourseInstructor(), requestModel.getStudentID());
newKeys.add(newKey);
}

// add map with new task ids to TaskMap
// TODO: read file, make edits, then save changes
for (Map.Entry<String, Task> entry : newTaskIdMap.entrySet()) {
TaskMap.addTask(entry.getKey(), entry.getValue());
// newKeys and values should be of equal length with each index referring to a pair
// ie. newKey[0], values[0] should be the old Task object but with the new course task id name!

// iterate through one of the arraylists (let's do keys)
for (int i = 0; i <= newKeys.size(); i++) {
// add key-value pairs to newTaskIdMap
newTaskIdMap.put(newKeys.get(i), values.get(i));
}

// take the keys of the key-value pair and add to student's to do list
// get the set of all keys of the new task id map and add to arraylist of strings
ArrayList<String> newTaskIds = new ArrayList<>(newTaskIdMap.keySet());
// add all newTaskIdMap key-value pairs to TaskMap
// TODO: read file, make edits, then save changes
TaskMap.addTasks(newTaskIdMap);

// add new task ids to the student's task list
// add new task ids to the student's to-do list (of task ids)
try {
tasksToTodolistDsGateway.addSaveTasksToTodolist(requestModel.getStudentID(), newTaskIds);
tasksToTodolistDsGateway.addSaveTasksToTodolist(requestModel.getStudentID(), newKeys);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down