Class PointsRepository

java.lang.Object
no.ntnu.principes.repository.AbstractRepository<Points,Long>
no.ntnu.principes.repository.PointsRepository
All Implemented Interfaces:
BaseRepository<Points,Long>

public class PointsRepository extends AbstractRepository<Points,Long>
Repository for managing household member points. Stores and retrieves points earned by members for completing tasks, with methods for calculating totals, generating leaderboards, and filtering by time periods.
  • Constructor Details

    • PointsRepository

      public PointsRepository(DataSource dataSource)
      Creates a new PointsRepository with the specified data source.
      Parameters:
      dataSource - The JDBC data source for database connections
  • Method Details

    • findById

      public Optional<Points> findById(Long id)
      Finds points by their ID.
      Parameters:
      id - The points ID
      Returns:
      An Optional containing the found points or empty if not found
    • findAll

      public List<Points> findAll()
      Retrieves all points records.
      Returns:
      A list of all points records
    • save

      public Points save(Points points)
      Saves points, creating a new record if it doesn't exist. Update functionality is not implemented as points are typically immutable once created.
      Parameters:
      points - The points to save
      Returns:
      The saved points with ID populated
    • deleteById

      public void deleteById(Long id)
      Deletes points by their ID.
      Parameters:
      id - The ID of the points to delete
    • mapRow

      protected Points mapRow(ResultSet rs) throws SQLException
      Maps a database result set row to a Points object.
      Specified by:
      mapRow in class AbstractRepository<Points,Long>
      Parameters:
      rs - The result set positioned at the row to map
      Returns:
      The mapped Points object
      Throws:
      SQLException - If a database error occurs during mapping
    • getTotalPointsForMember

      public int getTotalPointsForMember(Long memberId)
      Calculates the total points earned by a specific member. Returns 0 if the member has no points or if an error occurs.
      Parameters:
      memberId - The ID of the member to total points for
      Returns:
      The sum of all points earned by the member
    • findByMemberId

      public List<Points> findByMemberId(Long memberId)
      Finds all points earned by a specific member, ordered by most recent first.
      Parameters:
      memberId - The ID of the member to find points for
      Returns:
      A list of points earned by the member
    • createIfNotExist

      public void createIfNotExist(Points awardedPoints)
      Creates a points record if one doesn't exist for the member-task combination, or adds to the existing value if it does exist. Uses the database's ON CONFLICT clause for atomic upsert.
      Parameters:
      awardedPoints - The points to create or update
    • deleteByMemberId

      public int deleteByMemberId(Long memberId)
      Deletes all points associated with a specific member.
      Parameters:
      memberId - The ID of the member whose points should be deleted
      Returns:
      The number of points records deleted
    • deleteByTaskId

      public int deleteByTaskId(Long taskId)
      Deletes all points associated with a specific task's assignments.
      Parameters:
      taskId - The ID of the task whose points should be deleted
      Returns:
      The number of points records deleted