Skip to content

Commit

Permalink
[#45] Add function for inserting a list of issues into the DB
Browse files Browse the repository at this point in the history
Resolves #45
  • Loading branch information
rashadg1030 committed Jun 25, 2019
1 parent 8fa601c commit 63a1b9d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/IW/Db/Issue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import IW.App (WithError)
import IW.Core.Issue (Issue (..))
import IW.Core.Repo (Repo)
import IW.Core.Id (Id (..))
import IW.Db.Functions (WithDb, asSingleRow, execute, query, queryRaw)
import IW.Db.Functions (WithDb, asSingleRow, execute, executeMany, query, queryRaw)


-- | Get a list of all issues from database
Expand Down Expand Up @@ -55,4 +55,7 @@ insertIssue issue = execute [sql|

-- | Insert a list of issues into the database
insertIssues :: (WithDb env m) => [Issue] -> m ()
insertIssues = undefined
insertIssues issues = executeMany [sql|
INSERT INTO issues (number, title, body, url, repo_id)
VALUES (?, ?, ?, ?, ?);
|] $ issueToRow <$> issues
49 changes: 44 additions & 5 deletions src/TestDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,57 @@ configT = Config
<$> Toml.byteString "dbCredentials" .= cDbCredentials
<*> Toml.read "log.severity" .= cLogSeverity

testIssue :: Issue
testIssue = Issue { issueId = Id 57, issueNumber = 45, issueTitle = "Cool issue", issueBody = "Idk??", issueUrl = "hello.com", issueRepoId = Id 2}

testIssue1 :: Issue
testIssue1 = Issue { issueId = Id 57
, issueNumber = 45
, issueTitle = "Cool issue"
, issueBody = "Great issue for beginner"
, issueUrl = "hello.com"
, issueRepoId = Id 2
}

testIssue2 :: Issue
testIssue2 = Issue { issueId = Id 12
, issueNumber = 23
, issueTitle = "Hard issue"
, issueBody = "Don't know how to fix this issue!"
, issueUrl = "bye.com"
, issueRepoId = Id 1
}

testIssues :: [Issue]
testIssues = [testIssue1, testIssue2]


setupDb :: IO AppEnv
setupDb = loadConfig >>= mkAppEnv


runDbExecute :: (ToRow a) => Sql.Query -> a -> AppEnv -> IO Int64
runDbExecute q arg Env{..} = do
Pool.withResource envDbPool (\conn -> Sql.execute conn q arg)

runDbExecuteMany :: (ToRow a) => Sql.Query -> [a] -> AppEnv -> IO Int64
runDbExecuteMany q args Env{..} = do
Pool.withResource envDbPool (\conn -> Sql.executeMany conn q args)


testExecute :: (ToRow a) => Sql.Query -> a -> IO Int64
testExecute q arg = loadConfig >>= mkAppEnv >>= runDbExecute q arg
testExecute q arg = setupDb >>= runDbExecute q arg

testExecuteMany :: (ToRow a) => Sql.Query -> [a] -> IO Int64
testExecuteMany q args = setupDb >>= runDbExecuteMany q args


insertIssueTest :: Issue -> IO Int64
insertIssueTest Issue{..} = testExecute [sql|
insertIssueTest issue = testExecute [sql|
INSERT INTO issues (number, title, body, url, repo_id)
VALUES (?, ?, ?, ?, ?)
|] . issueToRow $ issue

insertIssuesTest :: [Issue] -> IO Int64
insertIssuesTest issues = testExecuteMany [sql|
INSERT INTO issues (number, title, body, url, repo_id)
VALUES (?, ?, ?, ?, ?)
|] (issueNumber, issueTitle, issueBody, issueUrl, issueRepoId)
|] $ issueToRow <$> issues

0 comments on commit 63a1b9d

Please sign in to comment.