leumas95

TenantEntity

Nov 17th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.38 KB | None | 0 0
  1. package windall.console.account.api.model
  2.  
  3. import javax.persistence.*
  4. import java.math.BigDecimal
  5. import java.time.LocalDate
  6. import windall.console.account.api.dto.TenantDto
  7. import windall.console.account.api.dto.ReceiptDto
  8. import windall.console.account.api.persistence.ReceiptEntity
  9.  
  10. @Entity
  11. @Table(name="tenant")
  12. internal data class TenantEntity (
  13.        
  14.         @Id
  15.         @GeneratedValue(strategy = GenerationType.IDENTITY)
  16.         val id: Long? = null,
  17.        
  18.         val name: String,
  19.        
  20.         val weeklyRent: BigDecimal,
  21.        
  22.         val paidTill: LocalDate = LocalDate.now(),
  23.        
  24.         val rentCredit: BigDecimal = BigDecimal(0),
  25.        
  26.         @OneToMany(mappedBy = "tenant", cascade = arrayOf(CascadeType.ALL), fetch = FetchType.EAGER)
  27.         val receipts: List<ReceiptEntity> = emptyList()
  28.  
  29. ) {
  30.    
  31.     @Suppress("unused")
  32.     private constructor() : this(name = "", weeklyRent = BigDecimal.ZERO)
  33.    
  34.     fun toDto(): TenantDto = TenantDto (   
  35.             id = this.id!!,
  36.             name = this.name,
  37.             weeklyRent = this.weeklyRent,
  38.             paidTill = this.paidTill,
  39.             rentCredit = this.rentCredit,
  40.             receipts = this.receipts.map { it.toDto() }.toMutableList()
  41.     )
  42.    
  43.     companion object {
  44.  
  45.         fun fromDto(dto: TenantDto) = TenantEntity (
  46.                 id = dto.id,
  47.                 name = dto.name,
  48.                 weeklyRent = dto.weeklyRent,
  49.                 paidTill = dto.paidTill,
  50.                 rentCredit = dto.rentCredit,
  51.                 receipts = dto.receipts.map { ReceiptEntity.fromDto(it) }
  52.         )
  53.  
  54.     }
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment