<commit_before>{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module Types where

import           Data.Aeson                         (FromJSON, ToJSON, (.:))
import qualified Data.Aeson                         as JSON
import           Data.Set                           as Set
import           Data.Text                          (Text)
import           Data.UUID                          (UUID)
import           Database.PostgreSQL.Simple         (FromRow)
import           Database.PostgreSQL.Simple.FromRow
import           GHC.Generics                       (Generic)
import           Network.WebSockets                 (Connection)


-- CONFIG, STATE & CLIENTS

newtype Config = Config { port :: Int }

newtype State = State { clients :: Set.Set Client }

data Client = Client { clientId :: UUID, conn :: Connection }

instance Eq Client where
  (Client id1 _) == (Client id2 _) = id1 == id2

instance Ord Client where
  compare (Client id1 _) (Client id2 _) = compare id1 id2


-- LISTS & ITEMS

data List =
  List { listId :: UUID
       , title  :: Text
       , items  :: [Item]
       } deriving (Generic, Show)

instance ToJSON List

instance FromRow List where
  fromRow = List <$> field <*> field <*> pure []

data Item =
  Item { itemId    :: UUID
       , text      :: Text
       , completed :: Bool
       , listsId   :: UUID
       } deriving (Generic, Show)

instance ToJSON Item
instance FromJSON Item
instance FromRow Item


-- ACTIONS

data Action = Register
            | GetLists
            | CreateList List
            | UpdateList List
            | DeleteList List
            | CreateItem Item
            | UpdateItem Item
            | DeleteItem UUID
            | SubscribeToList Text
            deriving (Generic, Show)

instance FromJSON Action where
  parseJSON = JSON.withObject "action" $ \obj -> do
    action <- obj .: "action"
    actionType <- action .: "type"
    data' <- action .: "data"

    let list = List <$>
          data' .: "listId" <*>
          data' .: "title" <*>
          pure [] -- For now let's ignore any submitted sub-items

    let item' = Item <$>
          data' .: "itemId" <*>
          data' .: "text" <*>
          data' .: "completed" <*>
          data' .: "listId"

    case actionType of<commit_msg>Fix typo in let binding for item<commit_after>{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module Types where

import           Data.Aeson                         (FromJSON, ToJSON, (.:))
import qualified Data.Aeson                         as JSON
import           Data.Set                           as Set
import           Data.Text                          (Text)
import           Data.UUID                          (UUID)
import           Database.PostgreSQL.Simple         (FromRow)
import           Database.PostgreSQL.Simple.FromRow
import           GHC.Generics                       (Generic)
import           Network.WebSockets                 (Connection)


-- CONFIG, STATE & CLIENTS

newtype Config = Config { port :: Int }

newtype State = State { clients :: Set.Set Client }

data Client = Client { clientId :: UUID, conn :: Connection }

instance Eq Client where
  (Client id1 _) == (Client id2 _) = id1 == id2

instance Ord Client where
  compare (Client id1 _) (Client id2 _) = compare id1 id2


-- LISTS & ITEMS

data List =
  List { listId :: UUID
       , title  :: Text
       , items  :: [Item]
       } deriving (Generic, Show)

instance ToJSON List

instance FromRow List where
  fromRow = List <$> field <*> field <*> pure []

data Item =
  Item { itemId    :: UUID
       , text      :: Text
       , completed :: Bool
       , listsId   :: UUID
       } deriving (Generic, Show)

instance ToJSON Item
instance FromJSON Item
instance FromRow Item


-- ACTIONS

data Action = Register
            | GetLists
            | CreateList List
            | UpdateList List
            | DeleteList List
            | CreateItem Item
            | UpdateItem Item
            | DeleteItem UUID
            | SubscribeToList Text
            deriving (Generic, Show)

instance FromJSON Action where
  parseJSON = JSON.withObject "action" $ \obj -> do
    action <- obj .: "action"
    actionType <- action .: "type"
    data' <- action .: "data"

    let list = List <$>
          data' .: "listId" <*>
          data' .: "title" <*>
          pure [] -- For now let's ignore any submitted sub-items

    let item = Item <$>
          data' .: "itemId" <*>
          data' .: "text" <*>
          data' .: "completed" <*>
          data' .: "listId"

    case actionType of
{'fraction_non_alphanumeric': 0.09987001733102253, 'fraction_numerical': 0.0034662045060658577, 'mean_word_length': 1.6129032258064515, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5244579', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1274, 'n_words': 403}