Browse Source

Create a new document text

Andrew Grant 6 tháng trước cách đây
mục cha
commit
7cea69aa8b

+ 6 - 6
src/main/java/scot/carricksoftware/grants/constants/TextAttributeConstants.java

@@ -15,19 +15,19 @@ public class TextAttributeConstants {
     }
 
     @SuppressWarnings({"unused"})
-    public static final String DOCUMENT_TEXTS = "document_texts";
+    public static final String DOCUMENT_TEXTS = "documentTexts";
     @SuppressWarnings({"unused"})
-    public static final String DOCUMENT_TEXT_COMMAND = "document_text_Command";
+    public static final String DOCUMENT_TEXT_COMMAND = "documentTextCommand";
 
     @SuppressWarnings({"unused"})
-    public static final String PERSON_TEXTS = "document_texts";
+    public static final String PERSON_TEXTS = "personTexts";
     @SuppressWarnings({"unused"})
-    public static final String PERSON_TEXT_COMMAND = "document_text_Command";
+    public static final String PERSON_TEXT_COMMAND = "documentTextCommand";
 
     @SuppressWarnings({"unused"})
-    public static final String PLACE_TEXTS = "place_texts";
+    public static final String PLACE_TEXTS = "placeTexts";
     @SuppressWarnings({"unused"})
-    public static final String PLACE_TEXT_COMMAND = "place_text_Command";
+    public static final String PLACE_TEXT_COMMAND = "placeTextCommand";
 
     @SuppressWarnings("unused")
     public static final String PERSON_IMAGES = "personImages";

+ 97 - 0
src/main/java/scot/carricksoftware/grants/controllers/text/documenttext/DocumentTextFormControllerImpl.java

@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:08. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.text.documenttext;
+
+import jakarta.validation.Valid;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommand;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommandImpl;
+import scot.carricksoftware.grants.constants.*;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextCommandConverterImpl;
+import scot.carricksoftware.grants.converters.text.documenttext.DocumentTextConverterImpl;
+import scot.carricksoftware.grants.services.text.documenttext.DocumentTextService;
+import scot.carricksoftware.grants.validators.text.DocumentTextCommandValidator;
+
+@SuppressWarnings("LoggingSimilarMessage")
+@Controller
+public class DocumentTextFormControllerImpl implements DocumentTextFormController {
+
+    private static final Logger logger = LogManager.getLogger(DocumentTextFormControllerImpl.class);
+    private final DocumentTextService documentTextService;
+    @SuppressWarnings({"FieldCanBeLocal", "unused"})
+    private final DocumentTextCommandConverterImpl documentTextCommandConverter;
+    private final DocumentTextConverterImpl documentTextConverter;
+    private final DocumentTextCommandValidator documentTextCommandValidator;
+
+
+    public DocumentTextFormControllerImpl(DocumentTextService documentTextService,
+                                          DocumentTextCommandConverterImpl documentTextCommandConverter,
+                                          DocumentTextConverterImpl documentTextConverter,
+                                          DocumentTextCommandValidator documentTextCommandValidator) {
+        this.documentTextService = documentTextService;
+        this.documentTextCommandConverter = documentTextCommandConverter;
+
+
+        this.documentTextConverter = documentTextConverter;
+        this.documentTextCommandValidator = documentTextCommandValidator;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(TextMappingConstants.DOCUMENT_TEXT_NEW)
+    public final String getNewDocumentText(final Model model) {
+        logger.debug("DocumentTextFormControllerImpl::getNewDocumentText");
+        model.addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, new DocumentTextCommandImpl());
+        return ViewConstants.DOCUMENT_TEXT_FORM;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(TextMappingConstants.DOCUMENT_TEXT_EDIT)
+    public final String documentTextEdit(@Valid @PathVariable final String id, Model model) {
+        logger.debug("DocumentTextFormControllerImpl::documentTextEdit");
+        model.addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, documentTextService.findById(Long.valueOf(id)));
+        return ViewConstants.DOCUMENT_TEXT_FORM;
+    }
+
+
+    @Override
+    @PostMapping(TextMappingConstants.DOCUMENT_TEXT)
+    public String saveOrUpdate(@Valid @ModelAttribute DocumentTextCommand documentTextCommand, BindingResult bindingResult, Model model) {
+        logger.debug("DocumentTextFormControllerImpl::saveOrUpdate");
+
+        documentTextCommandValidator.validate(documentTextCommand, bindingResult);
+
+
+        if (bindingResult.hasErrors()) {
+            bindingResult.getAllErrors().forEach(error -> logger.debug(error.getDefaultMessage()));
+            return ViewConstants.DOCUMENT_TEXT_FORM;
+        }
+
+        DocumentTextCommand savedCommand = documentTextService.saveDocumentTextCommand(documentTextCommand);
+        model.addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, savedCommand);
+        return MappingConstants.REDIRECT + TextMappingConstants.DOCUMENT_TEXT_SHOW.replace("{id}", "" + savedCommand.getId());
+    }
+
+
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(TextMappingConstants.DOCUMENT_TEXT_SHOW)
+    public String showById(@PathVariable String id, Model model) {
+        logger.debug("DocumentTextFormControllerImpl::saveOrUpdate");
+        DocumentTextCommand savedCommand = documentTextConverter.convert(documentTextService.findById(Long.valueOf(id)));
+        model.addAttribute(TextAttributeConstants.DOCUMENT_TEXT_COMMAND, savedCommand);
+        return ViewConstants.DOCUMENT_TEXT_FORM;
+    }
+
+
+}

+ 2 - 2
src/main/java/scot/carricksoftware/grants/validators/text/DocumentTextCommandValidator.java

@@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
-import scot.carricksoftware.grants.commands.text.PersonTextCommand;
+import scot.carricksoftware.grants.commands.text.DocumentTextCommand;
 
 @SuppressWarnings("unused")
 @Component
@@ -17,7 +17,7 @@ public class DocumentTextCommandValidator {
 
     private static final Logger logger = LogManager.getLogger(DocumentTextCommandValidator.class);
 
-    public void validate(PersonTextCommand personTextCommand, BindingResult bindingResult) {
+    public void validate(DocumentTextCommand documentTextCommand, BindingResult bindingResult) {
       logger.debug("DocumentTextCommandValidator::validate");
 
     }

+ 3 - 3
src/main/resources/templates/text/documentText/form.html

@@ -18,11 +18,11 @@
 
 </head>
 <body>
-<!--/*@thymesVar id="personCommand" type="scot.carricksoftware.grants.commands.people.PersonCommand"*/-->
+<!--/*@thymesVar id="documentTextCommand" type="scot.carricksoftware.grants.commands.text.DocumentTextCommand"*/-->
 <div th:insert="~{fragments/layout::banner}"></div>
 
 <div class="container border border-info rounded-3 text-center p-4">
-    <form th:object="${imageCommand}" th:action="@{/person}" method="post">
+    <form th:object="${documentTextCommand}" th:action="@{/documentText}" method="post">
         <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
             <p>Please Correct The Errors Below</p>
         </div>
@@ -34,7 +34,7 @@
             </div>
         </div>
         <button type="submit" class="btn btn-primary">Commit</button>
-        <a class="btn btn-secondary" th:href="@{/people}" th:text="${'List all'}">List all</a>
+        <a class="btn btn-secondary" th:href="@{/documentTexts}" th:text="${'List all'}">List all</a>
         <a class="btn btn-success" th:href="@{/}" th:text="${'Home'}">Home</a>
         <h6><span style="color: rgb(255,0,0);">*</span><span> Cannot be edited</span></h6>
     </form>

+ 2 - 2
src/main/resources/templates/text/documentText/list.html

@@ -30,7 +30,7 @@
             </thead>
 
             <!--/*@thymesVar id="text" type="scot.carricksoftware.grants.domains.text.DocumentText"*/-->
-            <tr th:each="text : ${document_texts}">
+            <tr th:each="text : ${documentTexts}">
                 <td th:text="${text.id}">123</td>
                 <td></td>
                 <td></td>
@@ -51,7 +51,7 @@
                          <a th:action="back" class="btn btn-secondary btn-sm"
                             th:href="@{/documentTexts/prev}" th:text="'<'"></a>
                          <a th:action="new" class="btn btn-primary btn-sm" th:href="@{/documentText/new}"
-                            th:text="'New Document'"></a>
+                            th:text="'New Document Text'"></a>
                          <a th:action="home" class="btn btn-success btn-sm"
                             th:href="@{/}" th:text="'Home'"></a>
                          <a th:action="forward" class="btn btn-secondary btn-sm"