package com.airlock.iam.base.infrastructure.db;

import com.airlock.iam.base.api.domain.model.exception.FailureException;

public final class DbUtils {
   public static void assertExactlyOneRecordAffected(int affectedRecords) {
      if (affectedRecords != 1) {
         throw new FailureException("Error while updating / deleting - expected one record to be affected but were " + affectedRecords);
      }
   }

   public static void assertExactlyOneRecordAffectedOrElseHandleExceptions(int affectedRecords, RuntimeException notFoundException) {
      if (affectedRecords == 0) {
         throw notFoundException;
      }

      assertExactlyOneRecordAffected(affectedRecords);
   }

   private DbUtils() {
      throw new AssertionError();
   }
}
