Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OrderStatus #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions OrderAppWithRazorPages/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace OrderApp.Extensions
{
public static class EnumExtensions
{
public static T GetAttribute<T>(this Enum value) where T : Attribute
{
var type = value.GetType();
var memberInfo = type.GetMember(value.ToString());
var attributes = memberInfo[0].GetCustomAttributes(typeof(T), false);

return (T)attributes[0];
}
}
}
22 changes: 22 additions & 0 deletions OrderAppWithRazorPages/Models/OrderStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel;

namespace OrderApp.Models
{
public enum OrderStatus
{
[Description("Ordered")]
Ordered,

[Description("Filled")]
Filled,

[Description("Shipped")]
Shipped,

[Description("Out for Delivery")]
OutForDelivery,

[Description("Delivered")]
Delivered
}
}
22 changes: 2 additions & 20 deletions OrderAppWithRazorPages/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page
@using OrderApp.Models
@model IndexModel
@{
ViewData["Title"] = "Home page";
Expand Down Expand Up @@ -32,26 +33,7 @@
</div>
<div class="row step">
<div class="col-md-1"></div>
<div class="col-md-2 @Model.GetShippingStepClass(0)">
<span class="fa glyphicon glyphicon-shopping-cart"></span>
<p>Ordered</p>
</div>
<div class="col-md-2 @Model.GetShippingStepClass(1)">
<span class="fa glyphicon glyphicon-check"></span>
<p>Order Filled</p>
</div>
<div class="col-md-2 @Model.GetShippingStepClass(2)">
<span class="fa glyphicon glyphicon-plane"></span>
<p>Shipped</p>
</div>
<div class="col-md-2 @Model.GetShippingStepClass(3)">
<span class="fa glyphicon glyphicon-road"></span>
<p>Out for Delivery</p>
</div>
<div class="col-md-2 @Model.GetShippingStepClass(4)">
<span class="fa glyphicon glyphicon-home"></span>
<p>Delivered</p>
</div>
<order-status status="@(OrderStatus)Model.ShippingStatus.Step"></order-status>
<div class="col-md-1"></div>
</div>
</div>
Expand Down
13 changes: 0 additions & 13 deletions OrderAppWithRazorPages/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,5 @@ public void OnGet()
ShippingPercent = (ShippingStatus.Step * 20) + 20;
Total = _ordersService.GetOrderTotal(Order.OrderId);
}

public string GetShippingStepClass(int Step)
{
if (ShippingStatus.Step > Step)
{
return "complete";
}
else if (ShippingStatus.Step == Step)
{
return "active";
}
return string.Empty;
}
}
}
1 change: 1 addition & 0 deletions OrderAppWithRazorPages/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@using OrderApp
@namespace OrderApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, OrderApp
63 changes: 63 additions & 0 deletions OrderAppWithRazorPages/TagHelpers/OrderStatusTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.ComponentModel;
using Microsoft.AspNetCore.Razor.TagHelpers;
using OrderApp.Extensions;
using OrderApp.Models;

namespace OrderApp.TagHelpers
{
public class OrderStatusTagHelper : TagHelper
{
public OrderStatus Status { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
foreach(OrderStatus value in Enum.GetValues(typeof(OrderStatus)))
{
output.Content.AppendHtml($@"<div class=""col-md-2 {GetOrderStatusClass(value)}"">
<span class=""fa glyphicon glyphicon-{GetOrderStatusIcon(value)}""></span>
<p>{value.GetAttribute<DescriptionAttribute>().Description}</p>
</div>");
}
}

private string GetOrderStatusClass(OrderStatus status)
{
if (this.Status > status)
{
return "complete";
}
else if (this.Status == status)
{
return "active";
}

return string.Empty;
}

private string GetOrderStatusIcon(OrderStatus status)
{
var icon = string.Empty;
switch (status)
{
case OrderStatus.Ordered:
icon = "shopping-cart";
break;
case OrderStatus.Filled:
icon = "check";
break;
case OrderStatus.Shipped:
icon = "plane";
break;
case OrderStatus.OutForDelivery:
icon = "road";
break;
case OrderStatus.Delivered:
icon = "home";
break;
}

return icon;
}
}
}