Skip to content

Commit

Permalink
GET /albums
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Jun 7, 2015
1 parent 0dfc1f5 commit a238f33
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
26 changes: 26 additions & 0 deletions Albums.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module FreyaMusicStore.Albums

open System

open Arachne.Http

open Freya.Core
open Freya.Router
open Freya.Machine
open Freya.Machine.Extensions.Http

type Albums =
{ Albums : Album.AlbumDetails [] }

let get =
freya {
let ctx = Db.getContext()
let albums = Db.getAlbumsDetails ctx |> Array.map Album.AlbumDetails.fromDb
return! write ("albums", { Albums = albums } )
}

let pipe =
freyaMachine {
including common
methodsSupported ( freya { return [ GET ] } )
handleOk (fun _ -> get) } |> FreyaMachine.toPipeline
1 change: 1 addition & 0 deletions App.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open Freya.Router
let musicStore =
freyaRouter {
resource (UriTemplate.Parse "/") Home.pipe
resource (UriTemplate.Parse "/albums") Albums.pipe
resource (UriTemplate.Parse "/album/{id}") Album.pipe
resource (UriTemplate.Parse "/genres") Genres.pipe
resource (UriTemplate.Parse "/genre/{name}") Genre.pipe } |> FreyaRouter.toPipeline
Expand Down
5 changes: 4 additions & 1 deletion Db.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ let getAlbumDetails id (ctx : DbContext) : AlbumDetails option =
for album in ctx.``[dbo].[AlbumDetails]`` do
where (album.AlbumId = id)
select album
} |> firstOrNone
} |> firstOrNone

let getAlbumsDetails (ctx : DbContext) : AlbumDetails [] =
ctx.``[dbo].[AlbumDetails]`` |> Seq.toArray
4 changes: 4 additions & 0 deletions FreyaMusicStore.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Common.fs" />
<Compile Include="Home.fs" />
<Compile Include="Album.fs" />
<Compile Include="Albums.fs" />
<Compile Include="Genre.fs" />
<Compile Include="Genres.fs" />
<Compile Include="StaticFiles.fs" />
Expand All @@ -71,6 +72,9 @@
<None Include="album.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="albums.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="genres.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
59 changes: 59 additions & 0 deletions albums.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@{
Layout = "mylayout";
}

@helper Truncate(string input, int length)
{
if (input.Length <= length)
{
@input
}
else
{
@input.Substring(0, length)<text>...</text>
}
}

@section Main
{
<h2>Index</h2>
<table>
<tr>
<th>
Genre
</th>
<th>
Artist
</th>
<th>
Title
</th>
<th>
Price
</th>
<th></th>
</tr>

@foreach (var item in Model.Albums)
{
<tr>
<td>
@item.Genre
</td>
<td>
@Truncate(item.Artist, 25)
</td>
<td>
@Truncate(item.Title, 25)
</td>
<td>
@item.Price
</td>
<td>

</td>
</tr>
}

</table>
}
3 changes: 2 additions & 1 deletion index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
<ul id="navlist">
<li class="first"><a href="/" id="current">Home</a></li>
<li><a href="/genres">Genres</a></li>
<li><a href="/albums">Albums</a></li>
<!-- <li> Html.RenderAction("CartSummary", "ShoppingCart");}</li>
<li><a href="Url.Content("~/StoreManager/")">Admin</a></li> -->
-->
</ul>
</div>

Expand Down

0 comments on commit a238f33

Please sign in to comment.