Commit ff42d21e authored by Sean's avatar Sean 🎨

Added simple CLI program to manage bare minimum cats and breeds

parent d6e0fbb8
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
......@@ -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
}
module Program
[<EntryPoint>]
let main argv = printfn "Loading up some cats!" |> Loader.load
let main argv = Cli.start()
......@@ -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>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment