콘텐츠로 이동

2023 12 22

2023-12-22

Slick filterIf, filterOpt

  • filterIf - filterIf(condition: Boolean)(f: R => Rep[Boolean]) - condition을 만족하면 filter를 추가하는 용도 - condition true => f 적용됨 - condition false => f 무시됨
    def findTopBanner(partnerId: Option[Long] = None): Future[Option[Bulletin]] = db.execute {
        Bulletins
            .join(BulletinServiceTypeMappings)
            .on(_.id === _.bulletinId)
            .filter(_._1.status === ACTIVE)
            .filter(_._2.serviceType === JOEL)
            .filterIf(partnerId.isDefined)(_._2.partnerId.getOrElse(-1L) === partnerId.get)
            .sortBy(_._1.displayStartAt.desc)
            .map(_._1)
            .result
            .headOption
    }
    
    select 
        x2.`id`, x2.`category_id`, x2.`title`, x2.`content`, x2.`status`, x2.`pin`, x2.`popup`, x2.`popup_start_at`, 
        x2.`popup_end_at`, x2.`display_start_at`, x2.`created_at`, x2.`created_by`, x2.`modified_at`, x2.`modified_by` 
    from `bulletin` x2
         join `bulletin_service_type_mapping` x3
              on x2.id = x3.bulletin_id
    where (((x2.`status` = 'A') and (x3.`service_type` = 'JOEL')) and (ifnull(x3.`partnerId`,-1) = 20334))
    order by x2.`display_start_at` desc;
    
  • filterOpt - filterOpt(option: Option[T])(f: ((E, T)) => Rep[Option[Boolean]]) - option이 Some 이라면 filter를 적용하는 용도 - option == Some => f 적용됨 - optoin == None => f 무시됨
    def findTopBanner(partnerId: Option[Long] = None): Future[Option[Bulletin]] = db.execute {
        Bulletins
            .join(BulletinServiceTypeMappings)
            .on(_.id === _.bulletinId)
            .filter(_._1.status === ACTIVE)
            .filter(_._2.serviceType === JOEL)
            .filterOpt(partnerId)(_._2.serviceGroupId === _)
            .sortBy(_._1.displayStartAt.desc)
            .map(_._1)
            .result
            .headOption
    }
    
    select 
        x2.`id`, x2.`category_id`, x2.`title`, x2.`content`, x2.`status`, x2.`pin`, x2.`popup`, x2.`popup_start_at`, 
        x2.`popup_end_at`, x2.`display_start_at`, x2.`created_at`, x2.`created_by`, x2.`modified_at`, x2.`modified_by` 
    from `bulletin` x2 
        join `bulletin_service_type_mapping` x3 
            on x2.id = x3.bulletin_id
    where (((x2.`status` = 'A') and (x3.`service_type` = 'JOEL')) and (x3.`partnerId` = 20334))
    order by x2.`display_start_at` desc;