Skip to content

Commit

Permalink
Start writing functions to read user info from database
Browse files Browse the repository at this point in the history
  • Loading branch information
arnavb committed Jul 16, 2024
1 parent 3520f33 commit 3a9c60c
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions JimBroBot/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module JimBroBot.Data

open System

open Npgsql.FSharp

type BotUser = { Id: int; DiscordId: string }

type Exercise =
Expand Down Expand Up @@ -30,3 +32,38 @@ type SpeedExerciseDetail =
DistanceMiles: double }

type TimeExerciseDetail = { Id: uint32; DurationSec: uint32 }

// TODO: Replace temporary connection string with proper environment setup
let connection =
"Host=localhost;Port=5432;Username=jimbrobotdb;\
Password=jimbrobotdbpassword;Database=jimbrobotdb"

let readBotUser (connectionString: string) (discordId: string) =
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM bot_user WHERE discord_id = @discord_id"
|> Sql.parameters [ "discord_id", Sql.string discordId ]
|> Sql.executeRowAsync (fun read ->
{ Id = read.int "id"
DiscordId = read.string "discord_id" })

let readExercisesForBotUser (connectionString: string) (botUserId: int) =
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM exercise_log WHERE bot_user_id = @bot_user_id"
|> Sql.parameters [ "bot_user_id", Sql.int botUserId ]
|> Sql.executeAsync (fun read ->
{ Id = read.int "id"
Name = read.string "name"
Type = read.string "type"
BotUserId = read.int "bot_user_id" })

let readExerciseLogEntriesForBotUser (connectionString: string) (botUserId: int) =
connectionString
|> Sql.connect
|> Sql.query "SELECT * FROM exercise WHERE bot_user_id = @bot_user_id"
|> Sql.parameters [ "bot_user_id", Sql.int botUserId ]
|> Sql.executeAsync (fun read ->
{ Id = read.int "id"
ExerciseId = read.int "exercise_id"
LogDate = read.dateOnly "log_date" })

0 comments on commit 3a9c60c

Please sign in to comment.