Selaa lähdekoodia

Organisation form controller ::new

Andrew Grant 6 kuukautta sitten
vanhempi
commit
f4c5919bb6

+ 28 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationFormController.java

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+import jakarta.validation.Valid;
+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 scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.constants.MappingConstants;
+
+public interface OrganisationFormController {
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_NEW)
+    String getNewOrganisation(Model model);
+
+    String saveOrUpdate(@Valid @ModelAttribute OrganisationCommand organisationCommand, BindingResult bindingResult, Model model);
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_SHOW)
+    String showById(@PathVariable String id, Model model);
+}

+ 94 - 0
src/main/java/scot/carricksoftware/grants/controllers/places/organisations/OrganisationFormControllerImpl.java

@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grants.controllers.places.organisations;
+
+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.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommandImpl;
+import scot.carricksoftware.grants.constants.AttributeConstants;
+import scot.carricksoftware.grants.constants.MappingConstants;
+import scot.carricksoftware.grants.constants.ViewConstants;
+import scot.carricksoftware.grants.converters.Capitalisation;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverter;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverter;
+import scot.carricksoftware.grants.services.places.organisations.OrganisationService;
+import scot.carricksoftware.grants.validators.places.OrganisationCommandValidator;
+
+@Controller
+public class OrganisationFormControllerImpl implements OrganisationFormController {
+
+    private static final Logger logger = LogManager.getLogger(OrganisationFormControllerImpl.class);
+
+    private final OrganisationService organisationService;
+    @SuppressWarnings({"unused", "FieldCanBeLocal"})
+    private final OrganisationCommandConverter organisationCommandConverter;
+    private final OrganisationConverter organisationConverter;
+    private final Capitalisation capitalisation;
+    private final OrganisationCommandValidator organisationCommandValidator;
+
+    public OrganisationFormControllerImpl(OrganisationService organisationService,
+                                          OrganisationCommandConverter organisationCommandConverter,
+                                          OrganisationConverter organisationConverter,
+                                          Capitalisation capitalisation,
+                                          OrganisationCommandValidator organisationCommandValidator) {
+        this.organisationService = organisationService;
+        this.organisationCommandConverter = organisationCommandConverter;
+        this.organisationConverter = organisationConverter;
+        this.capitalisation = capitalisation;
+        this.organisationCommandValidator = organisationCommandValidator;
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_NEW)
+    @Override
+    public final String getNewOrganisation(final Model model) {
+        logger.debug("OrganisationFormControllerImpl::getNewOrganisation");
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, new OrganisationCommandImpl());
+        return ViewConstants.ORGANISATION_FORM;
+    }
+
+    @PostMapping(MappingConstants.ORGANISATION)
+    @Override
+    public String saveOrUpdate(@Valid @ModelAttribute OrganisationCommand organisationCommand, BindingResult bindingResult, Model model) {
+        logger.debug("OrganisationFormControllerImpl::saveOrUpdate");
+
+        organisationCommandValidator.validate(organisationCommand, bindingResult);
+
+        if (bindingResult.hasErrors()) {
+            bindingResult.getAllErrors().forEach(error -> logger.debug(error.getDefaultMessage()));
+            return ViewConstants.ORGANISATION_FORM;
+        }
+
+        cleanUp(organisationCommand);
+        OrganisationCommand savedCommand = organisationService.saveOrganisationCommand(organisationCommand);
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, savedCommand);
+        return MappingConstants.REDIRECT + MappingConstants.ORGANISATION_SHOW.replace("{id}", "" + savedCommand.getId());
+    }
+
+    private void cleanUp(OrganisationCommand organisationCommand) {
+        organisationCommand.setName(capitalisation.getCapitalisation(organisationCommand.getName()));
+    }
+
+    @SuppressWarnings("SameReturnValue")
+    @GetMapping(MappingConstants.ORGANISATION_SHOW)
+    @Override
+    public String showById(@PathVariable String id, Model model) {
+        logger.debug("OrganisationFormControllerImpl::showById");
+        OrganisationCommand savedCommand = organisationConverter.convert(organisationService.findById(Long.valueOf(id)));
+        model.addAttribute(AttributeConstants.ORGANISATION_COMMAND, savedCommand);
+        return ViewConstants.ORGANISATION_FORM;
+    }
+
+}

+ 1 - 0
src/main/java/scot/carricksoftware/grants/services/places/organisations/OrganisationService.java

@@ -26,5 +26,6 @@ public interface OrganisationService {
 
     Organisation findById(Long id);
 
+    @SuppressWarnings("UnusedReturnValue")
     Organisation save(Organisation organisation);
 }

+ 31 - 0
src/main/java/scot/carricksoftware/grants/validators/places/OrganisationCommandValidator.java

@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 19/03/2025, 09:51. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.validators.places;
+
+import org.springframework.stereotype.Component;
+import org.springframework.validation.BindingResult;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.constants.ValidationConstants;
+
+@Component
+public class OrganisationCommandValidator {
+
+    public void validate(OrganisationCommand organisationCommand, BindingResult bindingResult) {
+        if (organisationCommand.getName().length() < ApplicationConstants.MINIMUM_NAME_LENGTH) {
+            bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                    null,
+                    ValidationConstants.NAME_IS_TOO_SHORT);
+        } else {
+            if (organisationCommand.getName().length() > ApplicationConstants.MAXIMUM_NAME_LENGTH) {
+                bindingResult.rejectValue("name", ApplicationConstants.EMPTY_STRING,
+                        null,
+                        ValidationConstants.NAME_IS_TOO_LONG);
+            }
+        }
+    }
+}
+

+ 3 - 3
src/main/resources/templates/organisation/form.html

@@ -18,12 +18,12 @@
 
 </head>
 <body>
-<!--/*@thymesVar id="countryCommand" type="scot.carricksoftware.grants.commands.places.countries.CountryCommand"*/-->
+
 <div th:insert="~{fragments/layout::banner}"></div>
 
 <div class="container border border-info
 rounded-3 text-center p-4">
-    <form th:object="${countryCommand}" th:action="@{/country}" method="post">
+    <form th:object="${organisationCommand}" th:action="@{/organisation}" method="post">
         <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
             <p>Please Correct The Errors Below</p>
         </div>
@@ -45,7 +45,7 @@ rounded-3 text-center p-4">
             </div>
         </div>
         <button type="submit" class="btn btn-primary">Commit</button>
-        <a class="btn btn-secondary" th:href="@{/countries}" th:text="${'List all'}">List all</a>
+        <a class="btn btn-secondary" th:href="@{/organisations}" 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>