Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
Sean
fsharp-sql-provider
Commits
ff42d21e
Commit
ff42d21e
authored
Feb 07, 2020
by
Sean
🎨
Browse files
Added simple CLI program to manage bare minimum cats and breeds
parent
d6e0fbb8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Cli.fs
0 → 100644
View file @
ff42d21e
module
Cli
open
Domain
open
DbAccess
let
private
getInput
()
=
System
.
Console
.
ReadLine
()
let
commandList
=
[
(
"help"
,
"?"
)
(
"count"
,
"c"
)
(
"add"
,
"a"
)
(
"quit"
,
"q"
)
]
|>
List
.
map
(
fun
(
cmd
,
abbrev
)
->
sprintf
"%s (%s)"
cmd
abbrev
)
|>
String
.
concat
"
\n\t
"
let
private
handleAdd
()
=
printf
"What do you want to add? (cat(c) | breed(b))
\n
> "
let
addType
=
getInput
()
match
addType
.
ToLowerInvariant
()
with
|
"c"
|
"cat"
->
printf
" > What's the kitty's name? > "
let
name
=
getInput
()
printf
" > What's the kitty's breed? > "
let
breed
=
getInput
()
let
newCat
=
mkCat
name
breed
|>
insertCat
catToStr
newCat
|>
printfn
" > There's a new cat on the block: %s"
|
"b"
|
"breed"
->
printf
" > What's the breed? > "
let
breed
=
getInput
()
match
findBreedIdByName
breed
with
|
Some
id
->
printfn
" > That breed already exists with id %d"
id
|
None
->
insertBreed
breed
|>
printfn
" > Inserted breed, it has id %d"
|
_
->
printfn
"Wat? Idk what that has to do with kittehs."
true
/// Handles input - matches on lowered string and returns to keep going or not
let
private
handleInput
(
str
:
string
)
=
let
lowered
=
str
.
ToLowerInvariant
()
match
lowered
with
|
"?"
|
"help"
->
printfn
"Available Commands are:
\n\t
%s"
commandList
true
|
"c"
|
"count"
->
let
latestCatNumber
=
countNumOfCats
()
printfn
"Currently tracking %d cats"
latestCatNumber
true
|
"a"
|
"add"
->
handleAdd
()
|
"q"
|
"quit"
->
false
|
_
->
true
let
private
prompt
()
=
printf
" > "
let
start
()
=
let
latestCatNumber
=
countNumOfCats
()
printfn
"Cat Manager CLI 😺: Currently tracking %d cats"
latestCatNumber
let
mutable
keepGoing
=
true
while
keepGoing
do
keepGoing
<-
()
|>
prompt
|>
getInput
|>
handleInput
printfn
"You're purrfect! 😽"
0
DbAccess.fs
View file @
ff42d21e
...
...
@@ -50,13 +50,14 @@ let private createBreedAttr = BreedAttrs.``Create(attributeid, breedid)``
let
private
createOwnerCat
=
Owners
.
``Create(age, name)``
/// Query db for a breedId, by Breed Name
let
private
findBreedIdByName
breedName
=
let
findBreedIdByName
breedName
=
query
{
for
b
in
Breeds
do
where
(
b
.
Name
=
breedName
)
take
1
select
b
.
Id
}
|>
Seq
.
tryHead
/// Insert a breed into the db by name
let
insertBreed
breedName
=
...
...
@@ -71,10 +72,7 @@ let private defInsertBreed: string -> unit -> int = thunkOne insertBreed
/// Insert a cat into the db, will insert the breed if necessary
let
insertCat
cat
=
let
breedId
=
findBreedIdByName
cat
.
breed
.
name
|>
Seq
.
tryHead
|>
Option
.
defaultWith
(
defInsertBreed
cat
.
breed
.
name
)
let
breedId
=
findBreedIdByName
cat
.
breed
.
name
|>
Option
.
defaultWith
(
defInsertBreed
cat
.
breed
.
name
)
let
catEntity
=
createCat
(
breedId
,
cat
.
name
)
printfn
" > Makin' kitteh %s"
cat
.
name
...
...
@@ -84,3 +82,10 @@ let insertCat cat =
{
cat
with
id
=
Some
catEntity
.
Id
breed
=
{
cat
.
breed
with
id
=
Some
breedId
}
}
let
countNumOfCats
()
=
query
{
for
_
in
Cats
do
select
true
count
}
Program.fs
View file @
ff42d21e
module
Program
[<
EntryPoint
>]
let
main
argv
=
printfn
"Loading up some cats!"
|>
Loader
.
load
let
main
argv
=
Cli
.
start
()
test-sql-provider.fsproj
View file @
ff42d21e
...
...
@@ -15,6 +15,7 @@
<Compile Include="Domain.fs" />
<Compile Include="DbAccess.fs" />
<Compile Include="Loader.fs" />
<Compile Include="Cli.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment