From 3a706130237620c55df011194a9fbdf73b46b436 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 8 Jun 2025 07:02:13 +0200 Subject: [PATCH] Close to the same feature set as the Go interface --- .../pcinv/controllers/WebController.java | 32 ++++++++++++++++++- .../pcinv/meta/AssetDescriptor.java | 6 +++- .../be/seeseepuff/pcinv/meta/AssetInfo.java | 11 +++++-- .../seeseepuff/pcinv/models/GenericAsset.java | 1 + .../be/seeseepuff/pcinv/models/HddAsset.java | 1 + .../be/seeseepuff/pcinv/models/RamAsset.java | 1 + .../pcinv/repositories/AssetRepository.java | 4 +++ .../pcinv/services/AssetService.java | 10 ++++++ src/main/resources/templates/browse.html | 10 ++++++ src/main/resources/templates/browse_type.html | 13 ++++++++ .../resources/templates/create_asset.html | 2 +- src/main/resources/templates/view.html | 2 +- 12 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/templates/browse.html create mode 100644 src/main/resources/templates/browse_type.html diff --git a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java index 616cfd6..2c9aa7b 100644 --- a/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java +++ b/src/main/java/be/seeseepuff/pcinv/controllers/WebController.java @@ -24,7 +24,10 @@ public class WebController { private static final String DESCRIPTORS = "descriptors"; /// The name of the model attribute that holds the asset descriptor for the current view. private static final String DESCRIPTOR = "descriptor"; - private static final String GENERIC_DESCRIPTOR = "generic"; + /// The name of the model attribute that holds the list of assets. + private static final String ASSETS = "assets"; + /// The name of the model attribute that holds a list of all properties of all descriptors. + private static final String PROPERTIES = "properties"; private final AssetService assetService; @@ -38,6 +41,33 @@ public class WebController { return "index"; } + /** + * Shows a view where a user can select the type of asset to browse. + */ + @GetMapping("/browse") + public String browse(Model model) { + model.addAttribute(DESCRIPTORS, assetService.getAssetDescriptors()); + return "browse"; + } + + /** + * Handles the browsing of assets by type. + * Displays the asset descriptor tree and the specific descriptor for the given type. + * + * @param model The model to add attributes to. + * @param type The type of asset to browse. + * @return The view name for browsing assets by type. + */ + @GetMapping("/browse/{type}") + public String browseType(Model model, @PathVariable String type) { + var tree = assetService.getAssetDescriptorTree(type); + model.addAttribute(DESCRIPTOR, assetService.getAssetDescriptor(type)); + model.addAttribute(DESCRIPTORS, tree); + model.addAttribute(PROPERTIES, tree.stream().flatMap(d -> d.getProperties().stream()).toList()); + model.addAttribute(ASSETS, assetService.getAssetsByType(type)); + return "browse_type"; + } + /** * Handles the view of an asset by its QR code. * If the asset does not exist, it redirects to the index page. diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java index e352970..ce59f3f 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetDescriptor.java @@ -19,9 +19,12 @@ public class AssetDescriptor { /// The type of property, e.g.: ram, asset, etc... private final String type; - /// The displayable name of the property, e.g.: "Random Access memory" + /// The displayable name of the property, e.g.: "Random Access Memory" private final String displayName; + /// The plural name of the property, e.g.: "Random Access Memories" + private final String pluralName; + /// Whether the asset is visible in the user interface. private final boolean visible; @@ -44,6 +47,7 @@ public class AssetDescriptor { var builder = AssetDescriptor.builder() .type(assetInfo.type()) .displayName(assetInfo.displayName()) + .pluralName(assetInfo.pluralName()) .visible(assetInfo.isVisible()) .properties(Arrays.stream(assetType.getDeclaredFields()) .map(field -> AssetProperty.loadFrom(field, assetInfo.type())) diff --git a/src/main/java/be/seeseepuff/pcinv/meta/AssetInfo.java b/src/main/java/be/seeseepuff/pcinv/meta/AssetInfo.java index 40dcb49..28ad4a9 100644 --- a/src/main/java/be/seeseepuff/pcinv/meta/AssetInfo.java +++ b/src/main/java/be/seeseepuff/pcinv/meta/AssetInfo.java @@ -16,14 +16,21 @@ public @interface AssetInfo { * * @return the display name of the asset type */ - String displayName() default ""; + String displayName(); + + /** + * The plural name of the asset type, used for display purposes. + * + * @return the plural name of the asset type + */ + String pluralName(); /** * The type of the asset, which can be a string or an integer. * * @return the type of the asset */ - String type() default ""; + String type(); /** * Indicates whether the asset type should be visible in the UI. diff --git a/src/main/java/be/seeseepuff/pcinv/models/GenericAsset.java b/src/main/java/be/seeseepuff/pcinv/models/GenericAsset.java index fe3e1cc..7255c49 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/GenericAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/GenericAsset.java @@ -17,6 +17,7 @@ import lombok.Setter; @Entity @AssetInfo( displayName = "Asset", + pluralName = "Assets", type = "asset", isVisible = false ) diff --git a/src/main/java/be/seeseepuff/pcinv/models/HddAsset.java b/src/main/java/be/seeseepuff/pcinv/models/HddAsset.java index 305a07f..d77cff3 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/HddAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/HddAsset.java @@ -15,6 +15,7 @@ import lombok.Setter; @Entity @AssetInfo( displayName = "Hard Drive", + pluralName = "Hard Drives", type = "HDD" ) @Table(name = "hdd_assets") diff --git a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java index 787306f..527649e 100644 --- a/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java +++ b/src/main/java/be/seeseepuff/pcinv/models/RamAsset.java @@ -15,6 +15,7 @@ import lombok.Setter; @Entity @AssetInfo( displayName = "Random Access Memory", + pluralName = "Random Access Memories", type = "RAM" ) @Table(name = "ram_assets") diff --git a/src/main/java/be/seeseepuff/pcinv/repositories/AssetRepository.java b/src/main/java/be/seeseepuff/pcinv/repositories/AssetRepository.java index ac3ce64..36fb356 100644 --- a/src/main/java/be/seeseepuff/pcinv/repositories/AssetRepository.java +++ b/src/main/java/be/seeseepuff/pcinv/repositories/AssetRepository.java @@ -4,6 +4,8 @@ import be.seeseepuff.pcinv.models.Asset; import be.seeseepuff.pcinv.models.GenericAsset; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AssetRepository { T saveAndFlush(T entity); @@ -19,6 +21,8 @@ public interface AssetRepository { T findByAsset(GenericAsset asset); + List findAll(); + Class getAssetType(); long count(); diff --git a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java index 62e7108..d1c56a0 100644 --- a/src/main/java/be/seeseepuff/pcinv/services/AssetService.java +++ b/src/main/java/be/seeseepuff/pcinv/services/AssetService.java @@ -50,6 +50,16 @@ public class AssetService { return getRepositoryFor(genericAsset.getType()).findByAsset(genericAsset); } + /** + * Retrieves all assets of a specific type. + * + * @param type the type of asset to retrieve + * @return a list of assets of the specified type + */ + public List getAssetsByType(String type) { + return getRepositoryFor(type).findAll(); + } + /** * Retrieves the global asset descriptors for all asset types. * diff --git a/src/main/resources/templates/browse.html b/src/main/resources/templates/browse.html new file mode 100644 index 0000000..5e6f3a5 --- /dev/null +++ b/src/main/resources/templates/browse.html @@ -0,0 +1,10 @@ + +
+ View device details +
    +
  • + +
  • +
+
+ diff --git a/src/main/resources/templates/browse_type.html b/src/main/resources/templates/browse_type.html new file mode 100644 index 0000000..ceee163 --- /dev/null +++ b/src/main/resources/templates/browse_type.html @@ -0,0 +1,13 @@ + +
+ There are in the database. + + + + + + + +
+
+ diff --git a/src/main/resources/templates/create_asset.html b/src/main/resources/templates/create_asset.html index 9688384..713773c 100644 --- a/src/main/resources/templates/create_asset.html +++ b/src/main/resources/templates/create_asset.html @@ -1,4 +1,4 @@ - +
Create a
diff --git a/src/main/resources/templates/view.html b/src/main/resources/templates/view.html index cba2a27..832ab0a 100644 --- a/src/main/resources/templates/view.html +++ b/src/main/resources/templates/view.html @@ -1,4 +1,4 @@ - +
View device details