Class JsonMaterialRepository

java.lang.Object
com.university.bookstore.repository.JsonMaterialRepository
All Implemented Interfaces:
MaterialRepository

public class JsonMaterialRepository extends Object implements MaterialRepository
JSON-based implementation of MaterialRepository (Adapter in hexagonal architecture). Persists materials to a JSON file using Jackson for serialization.

This adapter implements the MaterialRepository port, providing JSON file-based persistence without the domain layer knowing about the storage mechanism.

Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
  • Constructor Details

    • JsonMaterialRepository

      public JsonMaterialRepository(String filePath)
      Creates a new JSON material repository.
      Parameters:
      filePath - the path to the JSON file for persistence
  • Method Details

    • save

      public void save(Material material)
      Description copied from interface: MaterialRepository
      Saves a material to the repository. If a material with the same ID already exists, it will be updated.
      Specified by:
      save in interface MaterialRepository
      Parameters:
      material - the material to save
    • findById

      public Optional<Material> findById(String id)
      Description copied from interface: MaterialRepository
      Finds a material by its unique identifier.
      Specified by:
      findById in interface MaterialRepository
      Parameters:
      id - the material ID
      Returns:
      the material if found, empty Optional otherwise
    • findAll

      public List<Material> findAll()
      Description copied from interface: MaterialRepository
      Retrieves all materials from the repository.
      Specified by:
      findAll in interface MaterialRepository
      Returns:
      list of all materials
    • delete

      public boolean delete(String id)
      Description copied from interface: MaterialRepository
      Deletes a material by its ID.
      Specified by:
      delete in interface MaterialRepository
      Parameters:
      id - the material ID to delete
      Returns:
      true if the material was deleted, false if not found
    • exists

      public boolean exists(String id)
      Description copied from interface: MaterialRepository
      Checks if a material with the given ID exists.
      Specified by:
      exists in interface MaterialRepository
      Parameters:
      id - the material ID
      Returns:
      true if the material exists, false otherwise
    • count

      public long count()
      Description copied from interface: MaterialRepository
      Counts the total number of materials in the repository.
      Specified by:
      count in interface MaterialRepository
      Returns:
      the number of materials
    • deleteAll

      public void deleteAll()
      Description copied from interface: MaterialRepository
      Deletes all materials from the repository.
      Specified by:
      deleteAll in interface MaterialRepository
    • getFilePath

      public String getFilePath()
      Gets the file path used for persistence.
      Returns:
      the file path
    • dataFileExists

      public boolean dataFileExists()
      Checks if the data file exists.
      Returns:
      true if the file exists
    • getDataFileSize

      public long getDataFileSize()
      Gets the size of the data file in bytes.
      Returns:
      file size in bytes