Skip to content

Commit

Permalink
View for album
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Jun 7, 2015
1 parent 1feb476 commit cf55377
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 6 deletions.
36 changes: 36 additions & 0 deletions Db.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Db

open FSharp.Data.Sql

type Sql =
SqlDataProvider<
"Server=(LocalDb)\\v11.0;Database=FreyaMusicStore;Trusted_Connection=True;MultipleActiveResultSets=true",
DatabaseVendor=Common.DatabaseProviderTypes.MSSQLSERVER >

type DbContext = Sql.dataContext
type Album = DbContext.``[dbo].[Albums]Entity``
type Genre = DbContext.``[dbo].[Genres]Entity``
type AlbumDetails = DbContext.``[dbo].[AlbumDetails]Entity``

let getContext() = Sql.GetDataContext()

let firstOrNone s = s |> Seq.tryFind (fun _ -> true)

let getGenres (ctx : DbContext) : Genre list =
ctx.``[dbo].[Genres]`` |> Seq.toList

let getAlbumsForGenre genreName (ctx : DbContext) : Album list =
query {
for album in ctx.``[dbo].[Albums]`` do
join genre in ctx.``[dbo].[Genres]`` on (album.GenreId = genre.GenreId)
where (genre.Name = genreName)
select album
}
|> Seq.toList

let getAlbumDetails id (ctx : DbContext) : AlbumDetails option =
query {
for album in ctx.``[dbo].[AlbumDetails]`` do
where (album.AlbumId = id)
select album
} |> firstOrNone
19 changes: 19 additions & 0 deletions FreyaMusicStore.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,30 @@
</Choose>
<Import Project="$(FSharpTargetsPath)" />
<ItemGroup>
<Compile Include="Db.fs" />
<Compile Include="View.fs" />
<Compile Include="Program.fs" />
<None Include="index.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="home.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="album.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="genres.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="genre.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Site.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="placeholder.gif">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="App.config" />
<Content Include="packages.config" />
</ItemGroup>
Expand Down Expand Up @@ -148,6 +162,10 @@
<HintPath>packages\FSharp.Core.3.1.2.1\lib\net40\FSharp.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FSharp.Data.SqlProvider">
<HintPath>packages\SQLProvider.0.0.9-alpha\lib\net40\FSharp.Data.SqlProvider.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Hekate">
<HintPath>packages\Hekate.1.0.0\lib\net40\Hekate.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -175,6 +193,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Numerics" />
<Reference Include="System.Web.Razor">
<HintPath>packages\Microsoft.AspNet.Razor.3.0.0\lib\net45\System.Web.Razor.dll</HintPath>
Expand Down
40 changes: 34 additions & 6 deletions Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,33 @@ let common =
using http
mediaTypesSupported commonMediaTypes }

let getAlbum =
let albumId =
freya {
let! id = Freya.getLensPartial (Route.atom "id")
return! write ("home", {Greeting = sprintf "Album no %d" (Int32.Parse id.Value) })
match Int32.TryParse id.Value with
| true, id -> return Some id
| _ -> return None
}

let getAlbum =
freya {
let! id = albumId
let ctx = Db.getContext()
let album = Db.getAlbumDetails id.Value ctx |> Option.get |> View.toAlbum
return! write ("album", album)
}

let albumMalformed =
freya {
let! id = Freya.getLensPartial (Route.atom "id")
match Int32.TryParse id.Value with
| true, _ -> return false
| _ -> return true
let! id = albumId
return Option.isNone id
}

let albumExists =
freya {
let! id = albumId
let ctx = Db.getContext()
return Db.getAlbumDetails id.Value ctx |> Option.isSome
}

let getGenre =
Expand All @@ -84,6 +99,7 @@ let album =
freyaMachine {
including common
malformed albumMalformed
exists albumExists
methodsSupported ( freya { return [ GET ] } )
handleOk (fun _ -> getAlbum) } |> FreyaMachine.toPipeline

Expand Down Expand Up @@ -123,6 +139,8 @@ let resource key =
let private cssContent =
resource "Site.css"

let private placeholderContent =
resource "placeholder.gif"

let private firstNegotiatedOrElse def =
function | Negotiated (x :: _) -> x
Expand All @@ -143,15 +161,25 @@ let private getContent content n =
let private getCss =
getContent cssContent

let private getPlaceholder =
getContent placeholderContent

let private css =
freyaMachine {
including defaults
mediaTypesSupported (Freya.init [ MediaType.Css ])
handleOk getCss } |> FreyaMachine.toPipeline

let private placeholder =
freyaMachine {
including defaults
mediaTypesSupported (Freya.init [ MediaType.Parse "image/gif" ])
handleOk getPlaceholder } |> FreyaMachine.toPipeline

let musicStore =
freyaRouter {
resource (UriTemplate.Parse "/Site.css") css
resource (UriTemplate.Parse "/placeholder.gif") placeholder
resource (UriTemplate.Parse "/") home
resource (UriTemplate.Parse "/album/{id}") album
resource (UriTemplate.Parse "/genres") genres
Expand Down
17 changes: 17 additions & 0 deletions View.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module View

type AlbumDetails = {
Title : string
AlbumArtUrl : string
Price : decimal
Artist : string
Genre : string
}

let toAlbum (a: Db.AlbumDetails) = {
Title = a.Title
AlbumArtUrl = a.AlbumArtUrl
Price = a.Price
Artist = a.Artist
Genre = a.Genre
}
20 changes: 20 additions & 0 deletions album.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,24 @@

@section Main
{
<h2>@Model.Title</h2>

<p>
<img alt="@Model.Title" src="@Model.AlbumArtUrl" />
</p>

<div id="album-details">
<p>
<em>Genre:</em>
@Model.Genre
</p>
<p>
<em>Artist:</em>
@Model.Artist
</p>
<p>
<em>Price:</em>
@String.Format("{0:F}", Model.Price)
</p>
</div>
}
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
<package id="Microsoft.Owin.Hosting" version="3.0.1" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />
<package id="RazorEngine" version="3.7.0" targetFramework="net451" />
<package id="SQLProvider" version="0.0.9-alpha" targetFramework="net451" />
</packages>
Binary file added placeholder.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cf55377

Please sign in to comment.