Skip to content

Instantly share code, notes, and snippets.

@rflechner
Last active September 17, 2017 14:31
Show Gist options
  • Save rflechner/25cdab363b59697a44fe257c5b56dcf6 to your computer and use it in GitHub Desktop.
Save rflechner/25cdab363b59697a44fe257c5b56dcf6 to your computer and use it in GitHub Desktop.
FSharp TypeProviderKata
#I @"packages/SQLProvider/lib/"
#r "FSharp.Data.SqlProvider.dll"
open FSharp.Data.Sql
let [<Literal>] connectionString = "..."
let [<Literal>] resolutionPath = ""
type sql = SqlDataProvider<Common.DatabaseProviderTypes.MSSQLSERVER,
connectionString,
ResolutionPath = resolutionPath,
CaseSensitivityChange = Common.CaseSensitivityChange.ORIGINAL>
let ctx = sql.GetDataContext connectionString
// liste des noms de produit
query {
for product in ctx.SalesLt.Product do
select product.Name
} |> Seq.toList
// liste des categories de produit
//TODO
// compter le nombre de produits pour chaque catégorie
// SELECT c.[ProductCategoryID], c.[Name], COUNT(*) AS ProductCount
// FROM [SalesLT].[ProductCategory] AS c
// INNER JOIN [SalesLT].[Product] AS p on c.[ProductCategoryID] = p.[ProductCategoryID]
// GROUP BY c.[ProductCategoryID], c.[Name]
let report1 = "todo"

Step 1

mkdir TypeProviderKata
cd TypeProviderKata
yo fsharp
> Create standalone project
> WebSharper.UI.Next Suave
name: ReportingDashboard
Paket: yes
Fake: yes

Step 2

(mono) .paket/paket.exe add -i SQLProvider (voir si il faut migrer en .Net 4.6 pour avoir le package)

  • Créer Script1.fsx.
  • copier le contenu du script fourni.
  • essayer pendant 5 min de jouer avec l'autocompletion pour découvrir la base

Step 3

(mono) .paket/paket.exe add -i FsLab

Executer :

#load "packages/FsLab/Themes/AtomChester.fsx"
#load "packages/FsLab/FsLab.fsx"

open FSharp.Charting
open XPlot
open XPlot.Plotly

let chart1 =
  report1
  |> Chart.Line
  |> Chart.WithLayout (Layout(title = "Products by category"))
  |> Chart.WithLegend true
  |> Chart.WithHeight 500
  |> Chart.WithWidth 700

chart1.GetInlineHtml()

Step 4

Créer le module Reporting contenant la fonction getProductsCountByCategory : unit -> string. Son role est de renvoyer le HTML du rapport.

Créer la page web qui permettra d'afficher le rapport.

let ProductsCategory ctx =
        Templating.Main ctx EndPoint.ProductsCategory "ProductsCategory" [
            h1 [text "Products count by category"]
            div [
              html [
                Doc.Verbatim (Reporting.getProductsCountByCategory())
              ]
            ]
        ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment