Andrew Grant 6 місяців тому
батько
коміт
3d9f67d5df

+ 25 - 0
src/main/java/scot/carricksoftware/grants/repositories/places/OrganisationRepository.java

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 19:47. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.repositories.places;
+
+import org.springframework.data.repository.PagingAndSortingRepository;
+import org.springframework.stereotype.Repository;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.domains.places.Organisation;
+
+@Repository
+public interface OrganisationRepository extends PagingAndSortingRepository<Organisation, Long> {
+
+    OrganisationCommand saveOrganisationCommand(OrganisationCommand organisationCommand);
+
+    Iterable<Organisation> findAll();
+
+    void deleteById(Long id);
+
+    long count();
+
+    Organisation save(Organisation organisation);
+}

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

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 20:23. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.services.places.organisations;
+
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.domains.places.Organisation;
+
+import java.util.List;
+
+public interface OrganisationService {
+
+
+    @SuppressWarnings("unused")
+    OrganisationCommand saveOrganisationCommand(OrganisationCommand organisationCommand);
+
+
+    void deleteById(Long id);
+
+    List<Organisation> getPagedOrganisations(int pageNumber);
+
+    long count();
+
+
+    @SuppressWarnings("unused")
+    List<Organisation> findAll();
+}

+ 91 - 0
src/main/java/scot/carricksoftware/grants/services/places/organisations/OrganisationServiceImpl.java

@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) Andrew Grant of Carrick Software 11/03/2025, 20:23. All rights reserved.
+ *
+ */
+
+package scot.carricksoftware.grants.services.places.organisations;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import scot.carricksoftware.grants.commands.places.organisations.OrganisationCommand;
+import scot.carricksoftware.grants.constants.ApplicationConstants;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationCommandConverter;
+import scot.carricksoftware.grants.converters.places.organisations.OrganisationConverter;
+import scot.carricksoftware.grants.domains.places.Organisation;
+import scot.carricksoftware.grants.repositories.places.OrganisationRepository;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class OrganisationServiceImpl implements OrganisationService {
+    private static final Logger logger = LogManager.getLogger(OrganisationServiceImpl.class);
+
+    @SuppressWarnings({"unused"})
+    private final OrganisationRepository organisationRepository;
+    private final OrganisationConverter organisationConverter;
+    private final OrganisationCommandConverter organisationCommandConverter;
+
+    public OrganisationServiceImpl(
+            OrganisationRepository organisationRepository,
+            OrganisationConverter organisationConverter,
+            OrganisationCommandConverter organisationCommandConverter) {
+
+        this.organisationRepository = organisationRepository;
+        this.organisationConverter = organisationConverter;
+        this.organisationCommandConverter = organisationCommandConverter;
+    }
+
+
+    @Override
+    public void deleteById(Long id) {
+        logger.debug("OrganisationServiceImpl::deleteBy");
+        organisationRepository.deleteById(id);
+    }
+
+
+    @Override
+    public List<Organisation> getPagedOrganisations(int pageNumber) {
+        logger.debug("OrganisationServiceImpl::getPagedOrganisation");
+        Pageable paging = PageRequest.of(pageNumber, ApplicationConstants.DEFAULT_PAGE_SIZE, getSort());
+        Page<Organisation> pagedResult = organisationRepository.findAll(paging);
+        return pagedResult.getContent();
+    }
+
+    private Sort getSort() {
+        return Sort.by(Sort.Direction.ASC, "name");
+    }
+
+    @Override
+    public long count() {
+        logger.debug("OrganisationServiceImpl::count");
+        return organisationRepository.count();
+    }
+
+    @Transactional
+    @Override
+    public OrganisationCommand saveOrganisationCommand(OrganisationCommand organisationCommand) {
+        logger.debug("OrganisationServiceImpl::saveOrganisationCommand");
+        Organisation organisation = organisationCommandConverter.convert(organisationCommand);
+        Organisation savedOrganisation = organisationRepository.save(organisation);
+        assert organisation != null;
+        return organisationConverter.convert(savedOrganisation);
+
+    }
+
+    @Override
+    public List<Organisation> findAll() {
+        logger.debug("OrganisationServiceImpl::findAll");
+        List<Organisation> result = new ArrayList<>();
+        Iterable<Organisation> organisationIterable = organisationRepository.findAll();
+        organisationIterable.forEach(result::add);
+        return result;
+    }
+
+}