From 5848867423d5a86e4154a650902f8644592d3950 Mon Sep 17 00:00:00 2001 From: xenia Date: Wed, 9 Aug 2023 23:05:56 +0200 Subject: [PATCH] bare bones haskell --- backend/DBConfig.hs | 12 ++++++++++++ backend/Main.hs | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 backend/DBConfig.hs create mode 100644 backend/Main.hs diff --git a/backend/DBConfig.hs b/backend/DBConfig.hs new file mode 100644 index 0000000..c2ddc17 --- /dev/null +++ b/backend/DBConfig.hs @@ -0,0 +1,12 @@ +{-# LANGUAGE OverloadedStrings #-} +module DBConfig where + +import qualified Database.PostgreSQL.Typed as PG +import Network.Socket (SockAddr(SockAddrUnix)) + +myPGDatabase :: PG.PGDatabase +myPGDatabase = PG.defaultPGDatabase + { PG.pgDBAddr = Left ("localhost", "2137") + , PG.pgDBUser = "matabasare" + , PG.pgDBName = "matabas" + } \ No newline at end of file diff --git a/backend/Main.hs b/backend/Main.hs new file mode 100644 index 0000000..1e81ad7 --- /dev/null +++ b/backend/Main.hs @@ -0,0 +1,24 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE TemplateHaskell #-} + +import Control.Exception (bracket) +import Control.Monad (void, unless) +import Data.Int (Int32) +import Data.Maybe (listToMaybe) +import qualified Database.PostgreSQL.Typed as PG + +import DBConfig + +PG.useTPGDatabase myPGDatabase + +data Ingredient = Ingredient Int32 + deriving (Eq) + +createThing :: PG.PGConnection -> Ingredient -> IO () +createThing pg (Ingredient product_id) = + void $ PG.pgExecute pg [PG.pgSQL|INSERT INTO ingredient (product_id) VALUES (${product_id})|] + +main = bracket (PG.pgConnect myPGDatabase) PG.pgDisconnect $ \pg -> do + let ing = Ingredient 10 + createThing pg ing