Skip to content

Commit

Permalink
checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
theimowski committed Jun 16, 2015
1 parent 69174ab commit dadad5b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 3 deletions.
3 changes: 2 additions & 1 deletion App.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ let musicStore =
resource (UriTemplate.Parse Uris.logon) Logon.pipe
resource (UriTemplate.Parse Uris.register) Register.pipe

resource (UriTemplate.Parse Uris.cart) Cart.pipe } |> FreyaRouter.toPipeline
resource (UriTemplate.Parse Uris.cart) Cart.pipe
resource (UriTemplate.Parse Uris.checkout) Checkout.pipe } |> FreyaRouter.toPipeline

type Project () =
member __.Configuration (appBuilder : Owin.IAppBuilder) =
Expand Down
43 changes: 43 additions & 0 deletions Checkout.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module FreyaMusicStore.Checkout

open Arachne.Http

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

let ok _ =
freya {
return! writeHtml ("checkout", () )
}

let post =
freya {
let ctx = Db.getContext()
let! a = getAuthResult
let userName = userName a.Value
Db.placeOrder userName ctx
}

let seeOther _ =
freya {
return! writeHtml ("checkoutComplete", () )
}

let onUnauthorized _ =
freya {
return! writeHtml ("logon", {Logon.Logon.ReturnUrl = Uris.checkout; Logon.Logon.ValidationMsg = ""})
}

let pipe =
freyaMachine {
including common
authorized isLoggedOn
handleUnauthorized onUnauthorized
methodsSupported ( freya { return [ GET; POST ] } )
postRedirect (Freya.init true)
handleSeeOther seeOther
handleOk ok
doPost post } |> FreyaMachine.toPipeline
12 changes: 12 additions & 0 deletions Db.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,16 @@ let upgradeCarts (cartId : string, username :string) (ctx : DbContext) =
cart.Delete()
| None ->
cart.CartId <- username
ctx.SubmitUpdates()

let placeOrder (username : string) (ctx : DbContext) =
let carts = getCartsDetails username ctx
let total = carts |> List.sumBy (fun c -> (decimal) c.Count * c.Price)
let order = ctx.``[dbo].[Orders]``.Create(DateTime.UtcNow, total)
order.Username <- username
ctx.SubmitUpdates()
for cart in carts do
let orderDetails = ctx.``[dbo].[OrderDetails]``.Create(cart.AlbumId, order.OrderId, cart.Count, cart.Price)
getCart cart.CartId cart.AlbumId ctx
|> Option.iter (fun cart -> cart.Delete())
ctx.SubmitUpdates()
7 changes: 7 additions & 0 deletions FreyaMusicStore.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Genre.fs" />
<Compile Include="Genres.fs" />
<Compile Include="Cart.fs" />
<Compile Include="Checkout.fs" />
<Compile Include="StaticFiles.fs" />
<Compile Include="App.fs" />
<None Include="index.cshtml">
Expand Down Expand Up @@ -105,6 +106,12 @@
<None Include="cart.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="checkout.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="checkoutComplete.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Site.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
3 changes: 2 additions & 1 deletion Uris.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ let genre = "/genre/{0}"
let logon = "/account/logon"
let register = "/account/register"

let cart = "/cart/{0}"
let cart = "/cart/{0}"
let checkout = "/checkout"
2 changes: 1 addition & 1 deletion cart.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<em>Review</em> your cart:
</h3>
<p class="button">
<a href="/">Checkout >></a>
<a href="@Uris.checkout">Checkout >></a>
</p>
<div id="update-message">
</div>
Expand Down
42 changes: 42 additions & 0 deletions checkout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@using FreyaMusicStore
@{
Layout = "mylayout";
}

@section Main
{
<form method="POST" action="@Uris.checkout">
<h2>Address And Payment</h2>
<fieldset>
<legend>Shipping Information</legend>

<div class="editor-label">Address</div>
<div class="editor-field">
<input type="text" name="Address" />
</div>

<div class="editor-label">Phone</div>
<div class="editor-field">
<input type="text" name="Phone" />
</div>

<div class="editor-label">Street</div>
<div class="editor-field">
<input type="text" name="Street" />
</div>

</fieldset>
<fieldset>
<legend>Payment</legend>
<p>We're running a promotion: all music is free with the promo code: "FREE"</p>

<div class="editor-label">Promo Code</div>
<div class="editor-field">
<input type="text" name="PromoCode" />
</div>
</fieldset>

<input type="submit" value="Submit Order" />

</form>
}
17 changes: 17 additions & 0 deletions checkoutComplete.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@using FreyaMusicStore
@{
Layout = "mylayout";
}

@section Main
{

<h2>Checkout Complete</h2>

<p>Thanks for your order!</p>

<p>
How about shopping for some more music in our <a href="@Uris.home">store</a>?
</p>

}

0 comments on commit dadad5b

Please sign in to comment.