aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuicommon/db/repositories/RepositoryLogging.scala
blob: d1ec1dad35895d218d1086578b3718b1dc65f8a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package xyz.driver.pdsuicommon.db.repositories

import xyz.driver.pdsuicommon.logging._

trait RepositoryLogging extends PhiLogging {

  protected def logCreatedOne[T](x: T)(implicit toPhiString: T => PhiString): T = {
    logger.info(phi"An entity was created: $x")
    x
  }

  protected def logCreatedMultiple[T <: Iterable[_]](xs: T)(implicit toPhiString: T => PhiString): T = {
    if (xs.nonEmpty) {
      logger.info(phi"Entities were created: $xs")
    }
    xs
  }

  protected def logUpdatedOne(rowsAffected: Long): Long = {
    rowsAffected match {
      case 0 => logger.trace(phi"The entity is up to date")
      case 1 => logger.info(phi"The entity was updated")
      case x => logger.warn(phi"The ${Unsafe(x)} entities were updated")
    }
    rowsAffected
  }

  protected def logUpdatedOneUnimportant(rowsAffected: Long): Long = {
    rowsAffected match {
      case 0 => logger.trace(phi"The entity is up to date")
      case 1 => logger.trace(phi"The entity was updated")
      case x => logger.warn(phi"The ${Unsafe(x)} entities were updated")
    }
    rowsAffected
  }

  protected def logUpdatedMultiple(rowsAffected: Long): Long = {
    rowsAffected match {
      case 0 => logger.trace(phi"All entities are up to date")
      case x => logger.info(phi"The ${Unsafe(x)} entities were updated")
    }
    rowsAffected
  }

  protected def logDeletedOne(rowsAffected: Long): Long = {
    rowsAffected match {
      case 0 => logger.trace(phi"The entity does not exist")
      case 1 => logger.info(phi"The entity was deleted")
      case x => logger.warn(phi"Deleted ${Unsafe(x)} entities, expected one")
    }
    rowsAffected
  }

  protected def logDeletedMultiple(rowsAffected: Long): Long = {
    rowsAffected match {
      case 0 => logger.trace(phi"Entities do not exist")
      case x => logger.info(phi"Deleted ${Unsafe(x)} entities")
    }
    rowsAffected
  }

}