Initial commit
This commit is contained in:
47
Groceries/Home/HomeController.cs
Normal file
47
Groceries/Home/HomeController.cs
Normal file
@ -0,0 +1,47 @@
|
||||
namespace Groceries.Home;
|
||||
|
||||
using Groceries.Data;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
[Route("/")]
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly AppDbContext dbContext;
|
||||
|
||||
public HomeController(AppDbContext dbContext)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> IndexAsync()
|
||||
{
|
||||
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' AS is_metric,
|
||||
item_quantity->'is_divisible' 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);
|
||||
}
|
||||
}
|
42
Groceries/Home/Index.cshtml
Normal file
42
Groceries/Home/Index.cshtml
Normal file
@ -0,0 +1,42 @@
|
||||
@using Groceries.Data
|
||||
@using Humanizer
|
||||
|
||||
@model ItemTagQuantity?
|
||||
|
||||
@section head {
|
||||
@*<meta name="turbo-cache-control" content="no-preview" />*@
|
||||
}
|
||||
|
||||
<section class="card">
|
||||
<header class="card__header">
|
||||
<h2>Item Quantity (last 90 days)</h2>
|
||||
</header>
|
||||
<div class="card__content">
|
||||
@if (Model != null)
|
||||
{
|
||||
@if (Model.IsDivisible)
|
||||
{
|
||||
var quantity = Convert.ToDouble(Model.Quantity);
|
||||
var weekQuantity = Math.Round(quantity / 12);
|
||||
|
||||
<strong>@(Model.IsMetric ? quantity.ToMetric() : quantity)@Model.Unit @Model.Tag</strong>
|
||||
<small>(@(Model.IsMetric ? weekQuantity.ToMetric() : weekQuantity)@Model.Unit per week)</small>
|
||||
}
|
||||
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";
|
||||
}
|
||||
|
||||
<strong>@name.ToQuantity(Convert.ToInt32(Model.Quantity))</strong>
|
||||
<small>(@name.ToQuantity(Convert.ToInt32(avgQuantity)) per @avgPeriod)</small>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</section>
|
Reference in New Issue
Block a user