--------------------------------------------------------------------------------
-- |
-- Module : Database.EventStore.Internal.Operation.Write.Common
-- Copyright : (C) 2015 Yorick Laupa
-- License : (see the file LICENSE)
--
-- Maintainer : Yorick Laupa <yo.eight@gmail.com>
-- Stability : provisional
-- Portability : non-portable
--
--------------------------------------------------------------------------------
module Database.EventStore.Internal.Operation.Write.Common where

--------------------------------------------------------------------------------
import Data.Int

--------------------------------------------------------------------------------
import Database.EventStore.Internal.Control (freshUUID)
import Database.EventStore.Internal.Prelude
import Database.EventStore.Internal.Types

--------------------------------------------------------------------------------
-- | Returned after writing to a stream.
data WriteResult
    = WriteResult
      { WriteResult -> Int64
writeNextExpectedVersion :: !Int64
        -- ^ Next expected version of the stream.
      , WriteResult -> Position
writePosition :: !Position
        -- ^ 'Position' of the write.
      }
    deriving (WriteResult -> WriteResult -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WriteResult -> WriteResult -> Bool
$c/= :: WriteResult -> WriteResult -> Bool
== :: WriteResult -> WriteResult -> Bool
$c== :: WriteResult -> WriteResult -> Bool
Eq, Int -> WriteResult -> ShowS
[WriteResult] -> ShowS
WriteResult -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WriteResult] -> ShowS
$cshowList :: [WriteResult] -> ShowS
show :: WriteResult -> String
$cshow :: WriteResult -> String
showsPrec :: Int -> WriteResult -> ShowS
$cshowsPrec :: Int -> WriteResult -> ShowS
Show)

--------------------------------------------------------------------------------
-- | Constructs a 'NewEvent' from an 'Event'.
eventToNewEventIO :: Event -> IO NewEvent
eventToNewEventIO :: Event -> IO NewEvent
eventToNewEventIO Event
evt = do
    UUID
uuid <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (m :: * -> *). MonadIO m => m UUID
freshUUID forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe UUID
evt_id
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Text
-> UUID
-> Int32
-> Int32
-> ByteString
-> Maybe ByteString
-> NewEvent
newEvent Text
evt_type
                      UUID
uuid
                      Int32
evt_data_type
                      Int32
evt_metadata_type
                      ByteString
evt_data_bytes
                      Maybe ByteString
evt_metadata_bytes
  where
    evt_type :: Text
evt_type           = EventType -> Text
eventTypeText forall a b. (a -> b) -> a -> b
$ Event -> EventType
eventType Event
evt
    evt_id :: Maybe UUID
evt_id             = Event -> Maybe UUID
eventId Event
evt
    evt_data_bytes :: ByteString
evt_data_bytes     = EventData -> ByteString
eventDataBytes forall a b. (a -> b) -> a -> b
$ Event -> EventData
eventData Event
evt
    evt_data_type :: Int32
evt_data_type      = EventData -> Int32
eventDataType forall a b. (a -> b) -> a -> b
$ Event -> EventData
eventData Event
evt
    evt_metadata_bytes :: Maybe ByteString
evt_metadata_bytes = EventData -> Maybe ByteString
eventMetadataBytes forall a b. (a -> b) -> a -> b
$ Event -> EventData
eventData Event
evt
    evt_metadata_type :: Int32
evt_metadata_type  = EventData -> Int32
eventMetadataType forall a b. (a -> b) -> a -> b
$ Event -> EventData
eventData Event
evt