aboutsummaryrefslogtreecommitdiff
path: root/server/src/Thermoprint/Server/Database/Instances.hs
blob: f5c22fdfa20fcd81fce0c8d1a9f3035d55981282 (plain)
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
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeSynonymInstances       #-}
{-# LANGUAGE FlexibleInstances          #-}

module Thermoprint.Server.Database.Instances where

import Thermoprint.API (Printout, JobStatus, PrintingError)

import Database.Persist (PersistField(..))
import Database.Persist.Sql (PersistFieldSql(..))
import Database.Persist.TH

import Control.Monad ((<=<))
import Data.Bifunctor

import qualified Data.Aeson as JSON (encode, eitherDecodeStrict')

import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS (toStrict)
import qualified Data.Text as T (pack)

import Data.Proxy

-- | Instead of deriving an instance using 'derivePersistField', which would use 'show' and 'read', we write our own by hand in order to use json
instance PersistField Printout where
  toPersistValue   = toPersistValue . LBS.toStrict . JSON.encode
  fromPersistValue = first T.pack . JSON.eitherDecodeStrict' <=< fromPersistValue

instance PersistFieldSql Printout where
  sqlType _ = sqlType (Proxy :: Proxy ByteString)

derivePersistField "PrintingError"
derivePersistField "JobStatus"