From 595a691da287940a18f4eefa8cb17c05548f21ba Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sat, 2 Dec 2023 22:21:17 +0000 Subject: [PATCH] Refactor Home page to Razor component --- Groceries/Components/Sidebar.razor | 2 +- Groceries/Home/HomeController.cs | 41 ++-------------- Groceries/Home/HomePage.razor | 79 ++++++++++++++++++++++++++++++ Groceries/Home/Index.cshtml | 42 ---------------- 4 files changed, 84 insertions(+), 80 deletions(-) create mode 100644 Groceries/Home/HomePage.razor delete mode 100644 Groceries/Home/Index.cshtml diff --git a/Groceries/Components/Sidebar.razor b/Groceries/Components/Sidebar.razor index 287778b..5b5a32a 100644 --- a/Groceries/Components/Sidebar.razor +++ b/Groceries/Components/Sidebar.razor @@ -15,7 +15,7 @@
  • - + @* receipt long icon *@ Lists diff --git a/Groceries/Home/HomeController.cs b/Groceries/Home/HomeController.cs index fd68401..a56ff19 100644 --- a/Groceries/Home/HomeController.cs +++ b/Groceries/Home/HomeController.cs @@ -1,47 +1,14 @@ namespace Groceries.Home; -using Groceries.Data; - +using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; [Route("/")] -public class HomeController : Controller +public class HomeController : ControllerBase { - private readonly AppDbContext dbContext; - - public HomeController(AppDbContext dbContext) - { - this.dbContext = dbContext; - } - [HttpGet] - public async Task IndexAsync() + public IResult Index() { - var randomTagQuantity = await dbContext.ItemTagQuantities - .FromSql($""" - SELECT tag, quantity, coalesce(unit_name, unit) AS unit, is_metric, is_divisible - FROM ( - SELECT - unnest(tags) AS tag, - round(sum((item_quantity->'amount')::numeric * quantity), 1) AS quantity, - item_quantity->>'unit' AS unit, - (item_quantity->'is_metric')::boolean AS is_metric, - (item_quantity->'is_divisible')::boolean AS is_divisible - FROM item_purchases - JOIN items USING (item_id) - CROSS JOIN item_quantity(name) - WHERE array_length(tags, 1) > 0 - AND age(created_at) <= '90 days' - AND item_quantity IS NOT NULL - GROUP BY tag, item_quantity->>'unit', item_quantity->'is_metric', item_quantity->'is_divisible' - ORDER BY random() - FETCH FIRST ROW ONLY - ) AS random_item_tag_quantity - LEFT JOIN item_tags USING (tag) - """) - .FirstOrDefaultAsync(); - - return View(randomTagQuantity); + return new RazorComponentResult(); } } diff --git a/Groceries/Home/HomePage.razor b/Groceries/Home/HomePage.razor new file mode 100644 index 0000000..a37f0d7 --- /dev/null +++ b/Groceries/Home/HomePage.razor @@ -0,0 +1,79 @@ +@using Groceries.Data +@using Humanizer +@using Microsoft.EntityFrameworkCore + +@layout Layout + +@inject AppDbContext DbContext +@inject IHttpContextAccessor HttpContextAccessor + + + + + +Groceries + +
    +
    +

    Item Quantity (last 90 days)

    +
    +
    + @if (model != null) + { + if (model.IsDivisible) + { + var quantity = Convert.ToDouble(model.Quantity); + var weekQuantity = Math.Round(quantity / 12); + + @(model.IsMetric ? quantity.ToMetric() : quantity)@model.Unit @model.Tag + (@(model.IsMetric ? weekQuantity.ToMetric() : weekQuantity)@model.Unit per week) + } + else + { + var name = model.Unit != null ? $"{model.Tag} {model.Unit}" : model.Tag; + + var averageQuantity = model.Quantity / 12; + var averagePeriod = "week"; + if (averageQuantity < 1) + { + averageQuantity *= 4; + averagePeriod = "month"; + } + + @name.ToQuantity(Convert.ToInt32(model.Quantity)) + (@name.ToQuantity(Convert.ToInt32(averageQuantity)) per @averagePeriod) + } + } +
    +
    + +@code { + private ItemTagQuantity? model; + + protected override async Task OnInitializedAsync() + { + model = await DbContext.ItemTagQuantities + .FromSqlRaw(@" + SELECT tag, quantity, coalesce(unit_name, unit) AS unit, is_metric, is_divisible + FROM ( + SELECT + unnest(tags) AS tag, + round(sum((item_quantity->'amount')::numeric * quantity), 1) AS quantity, + item_quantity->>'unit' AS unit, + (item_quantity->'is_metric')::boolean AS is_metric, + (item_quantity->'is_divisible')::boolean AS is_divisible + FROM item_purchases + JOIN items USING (item_id) + CROSS JOIN item_quantity(name) + WHERE array_length(tags, 1) > 0 + AND age(created_at) <= '90 days' + AND item_quantity IS NOT NULL + GROUP BY tag, item_quantity->>'unit', item_quantity->'is_metric', item_quantity->'is_divisible' + ORDER BY random() + FETCH FIRST ROW ONLY + ) AS random_item_tag_quantity + LEFT JOIN item_tags USING (tag) + ") + .FirstOrDefaultAsync(HttpContextAccessor.HttpContext!.RequestAborted); + } +} diff --git a/Groceries/Home/Index.cshtml b/Groceries/Home/Index.cshtml deleted file mode 100644 index b295c62..0000000 --- a/Groceries/Home/Index.cshtml +++ /dev/null @@ -1,42 +0,0 @@ -@using Groceries.Data -@using Humanizer - -@model ItemTagQuantity? - -@section head { - @**@ -} - -
    -
    -

    Item Quantity (last 90 days)

    -
    -
    - @if (Model != null) - { - @if (Model.IsDivisible) - { - var quantity = Convert.ToDouble(Model.Quantity); - var weekQuantity = Math.Round(quantity / 12); - - @(Model.IsMetric ? quantity.ToMetric() : quantity)@Model.Unit @Model.Tag - (@(Model.IsMetric ? weekQuantity.ToMetric() : weekQuantity)@Model.Unit per week) - } - else - { - var name = Model.Unit != null ? $"{Model.Tag} {Model.Unit}" : Model.Tag; - - var avgQuantity = Model.Quantity / 12; - var avgPeriod = "week"; - if (avgQuantity < 1) - { - avgQuantity *= 4; - avgPeriod = "month"; - } - - @name.ToQuantity(Convert.ToInt32(Model.Quantity)) - (@name.ToQuantity(Convert.ToInt32(avgQuantity)) per @avgPeriod) - } - } -
    -