From 0e4b63ca91228f23308b3b54ef866f93af6a82d9 Mon Sep 17 00:00:00 2001
From: Trym Hamer Gudvangen <trym.gudvangen@gmail.com>
Date: Mon, 22 May 2023 13:55:19 +0200
Subject: [PATCH] refactor: use the StoryFileWriter instead of handler

---
 .../paths/controller/NewGameController.java   |  1 +
 .../paths/controller/NewStoryController.java  |  7 +--
 .../model/filehandling/StoryFileWriter.java   | 55 ++++++++++++++++++-
 .../paths/view/views/NewStoryView.java        |  4 --
 .../filehandling/StoryFileWriterImplTest.java |  2 +-
 5 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java
index 6b5741f..f9e49bc 100644
--- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java
+++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewGameController.java
@@ -3,6 +3,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller;
 import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE;
 
 import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileReader;
+import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView;
 import edu.ntnu.idatt2001.group_30.paths.view.views.PlaythroughView;
 import java.io.File;
 import java.io.IOException;
diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java
index 1d1a7ee..80537a9 100644
--- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java
+++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/controller/NewStoryController.java
@@ -2,8 +2,7 @@ package edu.ntnu.idatt2001.group_30.paths.controller;
 
 import edu.ntnu.idatt2001.group_30.paths.model.Passage;
 import edu.ntnu.idatt2001.group_30.paths.model.Story;
-import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler;
-import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog;
+import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileWriter;
 import edu.ntnu.idatt2001.group_30.paths.view.views.NewStoryView;
 import javafx.stage.FileChooser;
 
@@ -36,8 +35,8 @@ public class NewStoryController extends Controller{
         File selectedFile = fileChooser.showSaveDialog(null);
 
         if (selectedFile != null) {
-            StoryFileHandler fileHandler = new StoryFileHandler();
-            fileHandler.createStoryFile(story, selectedFile);
+            StoryFileWriter storyFileWriter = new StoryFileWriter();
+            storyFileWriter.create(story, selectedFile);
         }
     }
 }
diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java
index 40256fd..673d0d5 100644
--- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java
+++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriter.java
@@ -20,7 +20,24 @@ import java.util.*;
 public class StoryFileWriter {
 
     /**
-     * Creates a new story file with the given name and writes the story to it.
+     * This method takes a story and writes its contents to a .paths file. The story information is transcribed
+     * in the given format:
+     * <pre>
+     *  Story title
+     *
+     *  ::Opening Passage Title
+     *  Opening Passage Content
+     *  [Link Text](Link Reference)
+     *
+     *  ::Another Passage Title
+     *  Passage Content
+     *  [Link Text](Link Reference)
+     *  {@code <Action Type>}\Action Value/
+     *  [Link Text](Link Reference)
+     *
+     *  ...
+     *  </pre>
+     *
      * @param story    The story to be written to the file.
      * @param fileName The name of the file to be created.
      * @throws IOException if an I/O error occurs with the writer, or if the file already exists.
@@ -30,14 +47,48 @@ public class StoryFileWriter {
         Objects.requireNonNull(fileName, "File name cannot be null");
 
         File file = FileHandler.createFile(fileName);
+
+        create(story, file);
+    }
+
+    /**
+     * This method takes a story and writes its contents to a .paths file. The story information is transcribed
+     * in the given format:
+     * <pre>
+     *  Story title
+     *
+     *  ::Opening Passage Title
+     *  Opening Passage Content
+     *  [Link Text](Link Reference)
+     *
+     *  ::Another Passage Title
+     *  Passage Content
+     *  [Link Text](Link Reference)
+     *  {@code <Action Type>}\Action Value/
+     *  [Link Text](Link Reference)
+     *
+     *  ...
+     *  </pre>
+     *
+     * @param story         The story to be written to the file.
+     * @param file          The file the story is going to be written to.
+     * @throws IOException  if an I/O error occurs with the writer, or if the file already exists.
+     */
+    public void create(Story story, File file) throws IOException {
+        Objects.requireNonNull(story, "Story cannot be null");
+        Objects.requireNonNull(file, "File cannot be null");
+
         if (FileHandler.fileExists(file)) throw new FileAlreadyExistsException(
-            "You cannot overwrite a pre-existing story file"
+                "You cannot overwrite a pre-existing story file"
         );
 
         /* propagate any errors while writing */
         writeStory(story, file);
     }
 
+
+    //TODO: add test for story files...
+
     /**
      * Writes the story to the given file.
      * @param story The story to be written to the file.
diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java
index f84736f..4332df5 100644
--- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java
+++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java
@@ -8,7 +8,6 @@ import edu.ntnu.idatt2001.group_30.paths.model.Story;
 import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE;
 
 
-import edu.ntnu.idatt2001.group_30.paths.model.filehandling.StoryFileHandler;
 import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog;
 import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.PassagePopUp;
 import edu.ntnu.idatt2001.group_30.paths.view.components.table.PassageTable;
@@ -25,10 +24,7 @@ import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.scene.text.Text;
-import javafx.stage.FileChooser;
 
-import java.io.File;
-import java.io.IOException;
 import java.net.URL;
 import java.util.stream.Collectors;
 
diff --git a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java
index d681549..0adf533 100644
--- a/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java
+++ b/src/test/java/edu/ntnu/idatt2001/group_30/paths/model/filehandling/StoryFileWriterImplTest.java
@@ -220,7 +220,7 @@ public class StoryFileWriterImplTest {
             Assertions.assertThrows(
                 NullPointerException.class,
                 () -> {
-                    storyFileWriter.create(story, null);
+                    storyFileWriter.create(story, (String) null);
                 }
             );
         }
-- 
GitLab