diff --git a/build.gradle.kts b/build.gradle.kts index 47efc18..d468ce9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -29,6 +29,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-actuator") implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.8") + implementation("org.modelmapper:modelmapper:3.2.3") compileOnly("org.projectlombok:lombok") developmentOnly("org.springframework.boot:spring-boot-devtools") runtimeOnly("org.postgresql:postgresql") diff --git a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java index 6e122f0..2ae1425 100644 --- a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java +++ b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java @@ -226,6 +226,27 @@ public class WebController { return "create_asset"; } + /** + * Shows a view where the user can edit an existing asset. + * + * @param qr The QR code of the asset to edit. + */ + @GetMapping("/duplicate/{qr}") + public String duplicate(Model model, @PathVariable long qr) { + model.addAttribute(TIME, System.currentTimeMillis()); + var asset = assetService.getAssetByQr(qr); + if (asset == null) { + throw new RuntimeException("Asset not found"); + } + String assetType = asset.getAsset().getType(); + model.addAttribute(ACTION, "duplicate"); + model.addAttribute(ASSET, asset); + model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptorTree(assetType)); + model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(assetType)); + model.addAttribute(INPUT_LIST, assetService.getInputList(assetType)); + return "create_asset"; + } + /** * Actually edits an asset based on the form data submitted. * diff --git a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java index 022cf13..bf07927 100644 --- a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java +++ b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java @@ -12,6 +12,8 @@ import be.seeseepuff.pcinv.repositories.GenericAssetRepository; import be.seeseepuff.pcinv.repositories.WorkLogRepository; import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import org.modelmapper.ModelMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -54,6 +56,30 @@ public class AssetService { return getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset); } + /** + * Retrieves an asset by its QR code and duplicates the object. + * + * @param qr the QR code of the asset to retrieve + * @return the Asset associated with the given QR code, or null if no duplicate is found + */ + @SneakyThrows + public Asset getAssetDuplicateByQr(long qr) { + var mapper = new ModelMapper(); + var originalGenericAsset = genericRepository.findByQr(qr); + if (originalGenericAsset == null) { + throw new IllegalArgumentException("No asset found with QR code: " + qr); + } + var originalAsset = getRepositoryFor(originalGenericAsset.getType()).findByAsset(originalGenericAsset); + + var genericAsset = mapper.map(originalGenericAsset, GenericAsset.class); + genericAsset.setId(0); // Reset ID to create a new instance + genericAsset.setQr(0); + var asset = mapper.map(originalAsset, originalAsset.getClass()); + asset.setAsset(genericAsset); + + return asset; + } + /** * Retrieves all assets of a specific type. * diff --git a/src/main/resources/templates/browse_type.html b/src/main/resources/templates/browse_type.html index 8d2df4f..927ca29 100644 --- a/src/main/resources/templates/browse_type.html +++ b/src/main/resources/templates/browse_type.html @@ -14,6 +14,7 @@