-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added db operations for
PackageGroup
and refactored relevant …
…migrations
- Loading branch information
Showing
6 changed files
with
99 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CREATE TABLE IF NOT EXISTS package_groups ( | ||
id uuid PRIMARY KEY | ||
package_group_id uuid PRIMARY KEY | ||
, group_name varchar(255) NOT NULL | ||
) |
14 changes: 10 additions & 4 deletions
14
migrations/20240927142245_create_package_group_packages.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
CREATE TABLE IF NOT EXISTS package_group_packages ( | ||
package_group_id uuid NOT NULL REFERENCES package_groups (id) | ||
, package_id uuid NOT NULL REFERENCES packages (package_id) | ||
, PRIMARY KEY (package_group_id, package_id) | ||
) | ||
package_group_package_id uuid PRIMARY KEY | ||
, package_group_id uuid NOT NULL REFERENCES package_groups | ||
, package_id uuid NOT NULL REFERENCES packages | ||
); | ||
|
||
CREATE INDEX package_group_packages_package_id_fkey | ||
ON package_group_packages (package_id); | ||
|
||
CREATE INDEX package_group_packages_package_group_id_fkey | ||
ON package_group_packages (package_group_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module Flora.Model.PackageGroup.Query where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Flora.Model.PackageGroup.Types where | ||
|
||
import Flora.Model.Package.Types | ||
|
||
import GHC.Generics | ||
|
||
import Control.DeepSeq (NFData) | ||
import Data.Aeson | ||
import Data.Text (Text) | ||
import Data.Text.Display | ||
import Data.UUID | ||
import Database.PostgreSQL.Entity | ||
import Database.PostgreSQL.Entity.Types (GenericEntity, TableName) | ||
import Database.PostgreSQL.Simple (FromRow) | ||
import Database.PostgreSQL.Simple.FromField (FromField (..)) | ||
import Database.PostgreSQL.Simple.ToField (ToField (..)) | ||
import Database.PostgreSQL.Simple.ToRow (ToRow) | ||
|
||
newtype PackageGroupId = PackageGroupId {getPackageGroupId :: UUID} | ||
deriving | ||
(Eq, Ord, Show, FromField, ToField, FromJSON, ToJSON, NFData) | ||
via UUID | ||
deriving | ||
(Display) | ||
via ShowInstance UUID | ||
|
||
newtype PackageGroupPackageId = PackageGroupPackageId {getPackageGroupPackageId :: UUID} | ||
deriving | ||
(Eq, Ord, Show, FromField, ToField, FromJSON, ToJSON, NFData) | ||
via UUID | ||
deriving | ||
(Display) | ||
via ShowInstance UUID | ||
|
||
data PackageGroup | ||
= PackageGroup | ||
{ packageGroupId :: PackageGroupId | ||
, groupName :: Text | ||
} | ||
deriving stock | ||
(Eq, Ord, Show, Generic) | ||
deriving anyclass | ||
(FromRow, ToRow, FromJSON, ToJSON, NFData) | ||
deriving | ||
(Entity) | ||
via (GenericEntity '[TableName "package_groups"] PackageGroup) | ||
|
||
data PackageGroupPackage = PackageGroupPackage | ||
{ packageGroupPackageId :: PackageGroupPackageId | ||
, packageId :: PackageId | ||
, packageGroupId :: PackageGroupId | ||
} | ||
deriving stock | ||
(Eq, Ord, Show, Generic) | ||
deriving anyclass | ||
(FromRow, ToRow, FromJSON, ToJSON, NFData) | ||
deriving | ||
(Entity) | ||
via (GenericEntity '[TableName "package_group_packages"] PackageGroupPackage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Flora.Model.PackageGroup.Update | ||
( insertPackageGroup | ||
, addPackageToPackageGroup | ||
, removePackageFromPackageGroup | ||
) where | ||
|
||
import Control.Monad (void) | ||
import Database.PostgreSQL.Entity (delete, insert) | ||
import Effectful | ||
import Effectful.PostgreSQL.Transact.Effect (DB, dbtToEff) | ||
import Flora.Model.PackageGroup.Types | ||
|
||
insertPackageGroup :: DB :> es => PackageGroup -> Eff es () | ||
insertPackageGroup packageGroup = do | ||
void $ dbtToEff $ insert @PackageGroup packageGroup | ||
|
||
addPackageToPackageGroup :: DB :> es => PackageGroupPackage -> Eff es () | ||
addPackageToPackageGroup packageGroupPackage = | ||
void $ dbtToEff $ insert @PackageGroupPackage packageGroupPackage | ||
|
||
removePackageFromPackageGroup :: DB :> es => PackageGroupPackage -> Eff es () | ||
removePackageFromPackageGroup packageGroupPackage = | ||
void $ dbtToEff $ delete @PackageGroupPackage packageGroupPackage |