Basic archive funcionality and page

This commit is contained in:
Tea 2024-03-06 21:32:46 +01:00
parent 038dcc9894
commit 077b8e7f48
6 changed files with 250 additions and 0 deletions

View file

@ -0,0 +1,37 @@
@inject ArchiveService ArchiveService
@using Microsoft.AspNetCore.Components.QuickGrid
<QuickGrid Pagination="Pagination" Items="@ArchiveService.GetAllEntries().AsQueryable()">
<PropertyColumn Property="@(e => e.date)" Format="yyyy.MM.dd" Sortable="true" />
@if (ShowOnlyTotalPizza)
{
<PropertyColumn Title="Pizzas" Property="@(e => (e.MeatPizzas + e.VeggiePizzas + e.VeganPizzas))" Sortable="true" />
} else{
<PropertyColumn Title="🍖" Property="@(e => e.MeatPizzas)" Sortable="false" />
<PropertyColumn Title="🍄🧀" Property="@(e => e.VeggiePizzas)" Sortable="false" />
<PropertyColumn Title="🌽" Property="@(e => e.VeganPizzas)" Sortable="false" />
}
<PropertyColumn Title="🍾" Property="@(e => e.Bottles)" Sortable="false" />
<PropertyColumn Title="€" Property="@(e=>e.TotalCost)" />
</QuickGrid>
<Paginator State="@Pagination" />
<style>
::deep thead {width: 20px;}
</style>
@code {
[Parameter]
public int Number { get; set; }
[Parameter]
public bool ShowOnlyTotalPizza { get; set; }
PaginationState Pagination;
protected override void OnInitialized()
{
base.OnInitialized();
Pagination = new PaginationState { ItemsPerPage = Number };
}
}

View file

@ -0,0 +1,74 @@
@page "/admin{secretpath}/archive"
@inject NavigationManager NavigationManager
@inject PizzaDBService PizzaDBService
@inject GlobalStuffService GlobalStuffService
@inject ArchiveService ArchiveService
@rendermode InteractiveServer
<PageTitle>Pizza Archive</PageTitle>
<h1>Archive</h1>
<h4>Add current to archive</h4>
@if (GlobalStuffService.OrdersLocked)
{
<label> Num. Bottles
<InputNumber @bind-Value="bottleCount" DisplayName="Bottles" />
</label>
<button @onclick="AddToArchive" class="btn btn-primary">
<span>Add</span>
</button>
}
else
{
<span>Order's have to be locked to add today to the archive.</span>
}
<hr>
<div style="overflow-x:scroll;">
<ArchiveListComponent Number="25" />
</div>
@code {
[Parameter]
public string secretpath { get; set; }
int bottleCount;
protected override void OnInitialized()
{
if (secretpath != Environment.GetEnvironmentVariable("ADMIN_PATH"))
{
NavigationManager.NavigateTo("/"); return;
}
}
void AddToArchive()
{
PizzaArchiveEntry entry = new PizzaArchiveEntry();
entry.date = DateTime.Now;
entry.MeatPizzas = GlobalStuffService.MeatPizzas;
entry.VeggiePizzas = GlobalStuffService.VeggiePizzas;
entry.VeganPizzas = GlobalStuffService.VeganPizzas;
entry.TotalCost = GlobalStuffService.TotalCost;
entry.Bottles = bottleCount;
entry.PenaltyType = GlobalStuffService.GetConfig().PenaltyType;
entry.AnonymizedOrders = new List<PizzaArchiveEntry.AnonymizedOrder>();
var requests = PizzaDBService.GetAllRequests();
foreach (var req in requests)
{
PizzaArchiveEntry.AnonymizedOrder anonymized = new PizzaArchiveEntry.AnonymizedOrder();
anonymized.requestedMeatPieces = req.reqPiecesMeat;
anonymized.requestedVeggiePieces = req.reqPiecesVegetarian;
anonymized.requestedVeganPieces = req.reqPiecesVegan;
entry.AnonymizedOrders.Add(anonymized);
}
ArchiveService.AddEntry(entry);
StateHasChanged();
}
}

View file

@ -15,6 +15,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@ -26,6 +27,7 @@
<ItemGroup> <ItemGroup>
<Folder Include="Migrations\" /> <Folder Include="Migrations\" />
<Folder Include="wwwroot\logs\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -17,6 +17,7 @@ builder.Services.AddDbContext<PizzaContext>(options => options.UseMySql(connecti
builder.Services.AddSingleton<JSONService>(); builder.Services.AddSingleton<JSONService>();
builder.Services.AddSingleton<PizzaBalancingService>(); builder.Services.AddSingleton<PizzaBalancingService>();
builder.Services.AddSingleton<PizzaDBService>(); builder.Services.AddSingleton<PizzaDBService>();
builder.Services.AddSingleton<ArchiveService>();
builder.Services.AddSingleton<GlobalStuffService>(); builder.Services.AddSingleton<GlobalStuffService>();

View file

@ -0,0 +1,98 @@
using PizzaBot.Models;
using System.Security.Cryptography;
namespace PizzaBot.Services
{
using PizzaArchiveType = List<PizzaArchiveEntry>;
public struct PizzaArchiveEntry
{
// id should be a random number
public int id { get; set; }
public DateTime date { get; set; }
public int MeatPizzas { get; set; }
public int VeggiePizzas { get; set; }
public int VeganPizzas { get; set; }
public float TotalCost { get; set; }
public int Bottles { get; set; }
public List<AnonymizedOrder> AnonymizedOrders { get; set; }
public PenaltyType PenaltyType { get; set; }
public string Annotation { get; set; }
public struct AnonymizedOrder
{
public int requestedMeatPieces { get; set; }
public int requestedVeggiePieces { get; set; }
public int requestedVeganPieces { get; set; }
}
}
public class ArchiveService
{
JSONService _jsonService;
PizzaArchiveType _pizzaArchive;
Random _rng;
public ArchiveService(JSONService jSONService)
{
_rng = new Random();
_jsonService = jSONService;
PizzaArchiveType? loaded = _jsonService.GetPizzaArchive();
_pizzaArchive = loaded == null ? new PizzaArchiveType() : loaded;
SortArchive();
}
public PizzaArchiveType GetLatestNEntries(int n, int startPoint = 0)
{
return _pizzaArchive.Skip(startPoint).Take(n).ToList();
}
public PizzaArchiveType GetAllEntries()
{
PizzaArchiveType copy = _pizzaArchive;
PizzaArchiveType? loaded = _jsonService.GetPizzaArchive();
_pizzaArchive = loaded == null ? new PizzaArchiveType() : loaded;
return copy;
}
/// <summary>
/// Adds an entry to the archive
/// </summary>
/// <param name="entry">the data to add</param>
/// <returns>true, if sucessfull, false otherwise</returns>
public bool AddEntry(PizzaArchiveEntry entry)
{
if (_pizzaArchive == null)
{
return false;
}
entry.id = _rng.Next();
_pizzaArchive.Add(entry);
SortArchive();
_jsonService.WriteNewPizzaArchive(_pizzaArchive);
return true;
}
public bool RemoveEntry(PizzaArchiveEntry entry)
{
return _pizzaArchive.Remove(entry);
}
public bool RemoveEntry(int id)
{
int index = _pizzaArchive.FindIndex((PizzaArchiveEntry o) => { return o.id == id; });
if (index == -1)
{
return false;
}
_pizzaArchive.RemoveAt(index);
return true;
}
private void SortArchive()
{
_pizzaArchive.OrderByDescending(o => o.date);
}
}
}

View file

@ -10,6 +10,9 @@ namespace PizzaBot.Services
const string CONFIG_DIRECTORY_PATH = "config"; const string CONFIG_DIRECTORY_PATH = "config";
const string PIZZA_CONFIG_FILENAME = "pizza.config"; const string PIZZA_CONFIG_FILENAME = "pizza.config";
const string LOG_DIRECTORY_PATH = "logs";
const string PIZZA_LOG_FILENAME = "pizza.log";
public JSONService(IWebHostEnvironment webHostEnvironment) public JSONService(IWebHostEnvironment webHostEnvironment)
{ {
WebHostEnvironment = webHostEnvironment; WebHostEnvironment = webHostEnvironment;
@ -34,5 +37,40 @@ namespace PizzaBot.Services
{ {
return Path.Combine(WebHostEnvironment.WebRootPath, CONFIG_DIRECTORY_PATH, PIZZA_CONFIG_FILENAME); return Path.Combine(WebHostEnvironment.WebRootPath, CONFIG_DIRECTORY_PATH, PIZZA_CONFIG_FILENAME);
} }
public List<PizzaArchiveEntry>? GetPizzaArchive()
{
return ReadJson<List<PizzaArchiveEntry>>(Path.Combine(LOG_DIRECTORY_PATH, PIZZA_LOG_FILENAME));
}
public void WriteNewPizzaArchive(List<PizzaArchiveEntry> logs)
{
string path = Path.Combine(WebHostEnvironment.WebRootPath, LOG_DIRECTORY_PATH, PIZZA_LOG_FILENAME);
if (File.Exists(path))
{
string newPath = Path.Combine(WebHostEnvironment.WebRootPath, LOG_DIRECTORY_PATH, (DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "." + PIZZA_LOG_FILENAME));
File.Move(path,newPath);
}
WriteJson(logs, Path.Combine(LOG_DIRECTORY_PATH, PIZZA_LOG_FILENAME));
}
private T? ReadJson<T>(string localpath) where T : class
{
string path = Path.Combine(WebHostEnvironment.WebRootPath, localpath);
if(!File.Exists(path))
{
return null;
}
string jsonString = File.ReadAllText(path);
return JsonSerializer.Deserialize<T>(jsonString);
}
private void WriteJson<T>(T toSerialize, string localpath)
{
string path = Path.Combine(WebHostEnvironment.WebRootPath ,localpath);
string jsonString = JsonSerializer.Serialize<T>(toSerialize);
File.WriteAllText(path, jsonString);
}
} }
} }