Viačeslav Pozdniakov @poznia, Adform, VU
Viačeslav Pozdniakov @poznia, Adform, VU
@Entity
public class A {
@JsonProperty("banner_id")
@Id
public int bannerId;
}
public class S {
@JsonProperty("banner_id")
public int bannerId;
}
public class A {
public int bannerId;
}
@Entity
public class B {
@Id
public int bannerId;
}
data BannerSetup = BannerSetup {
bannerId :: BannerId
, clientDivId :: ClientDivId
, eshopId :: EshopId
, templateId :: TemplateId
}
data BannerId = BannerId Integer
data ClientDivId = ClientDivId Integer
data EshopId = EshopId Integer
data TemplateId = TemplateId Integer
routes :: ScottyM ()
routes = do
post "/" $ readBannerSetup >>= save >> raw "Ok"
readBannerSetup :: ActionM BannerSetup
readBannerSetup = jsonData
API:
jsonData :: FromJSON a => ActionM a
instance FromJSON BannerSetup where
parseJSON (Object v) = do
b <- v .: "banner_id"
c <- v .: "client_division_id"
e <- v .: "eshop_id"
t <- v .: "template_id"
return $ BannerSetup (BannerId b) (ClientDivId c) (EshopId e) (TemplateId t)
parseJSON _ = mzero
routes :: ScottyM ()
routes = do
post "/" $ readBannerSetup >>= save >> raw "Ok"
readBannerSetup :: ActionM BannerSetup
readBannerSetup = jsonData
API:
jsonData :: FromJSON a => ActionM a
save :: ToRow a => a -> ActionM ()
save d = liftIO $ bracket getConnection close $ \conn -> void $
execute conn "insert into banners " ++
"(banner_id, client_div_id, eshop_id, template_id)" ++
"values (?, ?, ?, ?)" d
instance ToRow BannerSetup where
toRow (BannerSetup (BannerId b) (ClientDivId c) (EshopId e) (TemplateId t)) =
toRow [toField b, toField c, toField e, toField t]
routes :: ScottyM ()
routes = do
post "/" $ readBannerSetup >>= save >> raw "Ok"
get "/" $ loadAllBanners >>= renderJson
loadAllBanners :: FromRow a => ActionM [a]
loadAllBanners = liftIO $ bracket getConnection close $ \conn ->
query_ conn "select * from banners"
instance FromRow BannerSetup where
fromRow = do
b <- field
c <- field
e <- field
t <- field
return $ BannerSetup (BannerId b) (ClientDivId c) (EshopId e) (TemplateId t)
renderJson :: [BannerSetup] -> ActionM ()
renderJson = json
instance ToJSON BannerSetup where
toJSON (BannerSetup (BannerId b) (ClientDivId c) (EshopId e) (TemplateId t)) =
object [
"banner_id" .= b, "client_division_id" .= c,
"eshop_id" .= e, "template_id" .= t
]
ToJSON Bool
ToJSON Char
ToJSON Double
ToJSON Float
ToJSON Int
-- skip some
ToJSON Value
ToJSON DotNetTime
ToJSON [Char]
ToJSON a => ToJSON [a]
ToJSON (Ratio Integer)
ToJSON a => ToJSON (Maybe a)
-- skip many
Yes, you can. Use:
case class BannerSetup(bannerId: Int, clientDivId: Int, eshopId: Int, templateId: Int)
To "implement"
trait ToRow[T] {
def toRows(value: T): List[AnyVal]
}
def save[T: ToRow](t: T) = {
val rows = implicitly[ToRow[T]].toRows(t)
for (row <- rows) println(row.toString)
}
We need such a glue:
implicit object BannerSetupToRow extends ToRow[BannerSetup] {
def toRows(b: BannerSetup) = List(b.bannerId, b.clientDivId, b.eshopId, b.templateId)
}
➜ dco-bannerz git:(master) cloc src 4 text files. 4 unique files. 0 files ignored. http://cloc.sourceforge.net v 1.60 T=0.02 s (259.6 files/s, 6814.6 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- Haskell 4 21 0 84 ------------------------------------------------------------------------------- SUM: 4 21 0 84 -------------------------------------------------------------------------------