Skip to content

Commit

Permalink
Add housekeeping fields to remaining models
Browse files Browse the repository at this point in the history
  • Loading branch information
sras committed Nov 7, 2016
1 parent f6a52f0 commit 46ba90a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
36 changes: 20 additions & 16 deletions SpockOpaleye/src/DataTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ newtype TenantId = TenantId Int
data TenantStatus = TenantStatusActive | TenantStatusInActive | TenantStatusNew
deriving (Show, Generic)

data TenantPoly key created_at updated_at name fname lname email phone status owner_id b_domain = Tenant
{ tenant_id :: key
data TenantPoly key created_at updated_at name fname lname email phone status owner_id b_domain = Tenant {
tenant_id :: key
, tenant_createdat :: created_at
, tenant_updatedat :: updated_at
, tenant_name :: name
Expand All @@ -31,7 +31,7 @@ data TenantPoly key created_at updated_at name fname lname email phone status ow
, tenant_status :: status
, tenant_ownerid :: owner_id
, tenant_backofficedomain :: b_domain
} deriving (Show, Generic)
} deriving (Show, Generic)

type Tenant = TenantPoly TenantId UTCTime UTCTime Text Text Text Text Text TenantStatus (Maybe UserId) Text

Expand All @@ -43,29 +43,33 @@ data UserStatus = UserStatusActive | UserStatusInActive | UserStatusBlocked
newtype UserId = UserId Int
deriving (Show, Generic)

data UserPoly key tenant_id username password firstname lastname status = User {
user_id :: key,
user_tenantid :: tenant_id,
user_username :: username,
user_password :: password,
user_firstname :: firstname,
user_lastname :: lastname,
user_status :: status
data UserPoly key created_at updated_at tenant_id username password firstname lastname status = User {
user_id :: key
, user_createdat :: created_at
, user_updatedat :: updated_at
, user_tenantid :: tenant_id
, user_username :: username
, user_password :: password
, user_firstname :: firstname
, user_lastname :: lastname
, user_status :: status
}

type User = UserPoly UserId TenantId Text BcryptPassword (Maybe Text) (Maybe Text) UserStatus
type User = UserPoly UserId UTCTime UTCTime TenantId Text BcryptPassword (Maybe Text) (Maybe Text) UserStatus

data Permission = Read | Create | Update | Delete
deriving (Show)

newtype RoleId = RoleId Int
deriving (Show)

data RolePoly key tenant_id name permission = Role
{ role_id :: key
data RolePoly key created_at updated_at tenant_id name permission = Role {
role_id :: key
, role_createdat :: created_at
, role_updatedat :: updated_at
, role_tenantid :: tenant_id
, role_name :: name
, role_permission :: permission
} deriving (Show)
} deriving (Show)

type Role = RolePoly RoleId TenantId Text (NonEmpty Permission)
type Role = RolePoly RoleId UTCTime UTCTime TenantId Text (NonEmpty Permission)
25 changes: 18 additions & 7 deletions SpockOpaleye/src/OpaleyeDef.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ tenantTable = Table "tenants" (pTenant

type UserTableW = UserPoly
(Maybe (Column PGInt4))
(Maybe (Column PGTimestamptz)) -- createdAt
(Maybe (Column PGTimestamptz)) -- updatedAt
(Column PGInt4)
(Column PGText)
(Column PGBytea)
Expand All @@ -78,6 +80,8 @@ type UserTableW = UserPoly

type UserTableR = UserPoly
(Column PGInt4)
(Column PGTimestamptz) -- createdAt
(Column PGTimestamptz) -- updatedAt
(Column PGInt4)
(Column PGText)
(Column PGBytea)
Expand All @@ -92,22 +96,28 @@ userTable :: Table UserTableW UserTableR
userTable = Table "users" (pUser
User {
user_id = optional "id"
, user_tenantid = required "tenant_id"
, user_username = required "username"
, user_password = required "password"
, user_firstname = optional "first_name"
, user_lastname = optional "last_name"
, user_status = required "status"
, user_createdat = (optional "created_at")
, user_updatedat = (optional "updated_at")
, user_tenantid = required "tenant_id"
, user_username = required "username"
, user_password = required "password"
, user_firstname = optional "first_name"
, user_lastname = optional "last_name"
, user_status = required "status"
})

type RoleTableW = RolePoly
(Maybe (Column PGInt4))
(Maybe (Column PGTimestamptz)) -- createdAt
(Maybe (Column PGTimestamptz)) -- updatedAt
(Column PGInt4)
(Column PGText)
(Column (PGArray PGText))

type RoleTableR = RolePoly
(Column PGInt4)
(Column PGTimestamptz) -- createdAt
(Column PGTimestamptz) -- updatedAt
(Column PGInt4)
(Column PGText)
(Column (PGArray PGText))
Expand All @@ -118,6 +128,8 @@ $(makeLensesWith abbreviatedFields ''RolePoly)
roleTable :: Table RoleTableW RoleTableR
roleTable = Table "roles" (pRole Role {
role_id = optional "id",
role_createdat = optional "created_at",
role_updatedat = optional "updated_at",
role_tenantid = required "tenant_id",
role_name = required "name",
role_permission = required "permissions"})
Expand Down Expand Up @@ -292,7 +304,6 @@ instance D.Default Constant () (Maybe (Column PGTimestamptz)) where
instance D.Default Constant UTCTime (Maybe (Column PGTimestamptz)) where
def = Constant (\time -> Just $ pgUTCTime time)


create_item :: (D.Default Constant haskells columnsW, D.Default QueryRunner returned b)
=> Connection -> Table columnsW returned -> haskells -> IO b
create_item conn table item = fmap Prelude.head $ runInsertManyReturning conn table [constant item] id

0 comments on commit 46ba90a

Please sign in to comment.