diff --git a/PizzaBot/Components/ArchiveListComponent.razor b/PizzaBot/Components/ArchiveListComponent.razor new file mode 100644 index 0000000..60efb0b --- /dev/null +++ b/PizzaBot/Components/ArchiveListComponent.razor @@ -0,0 +1,37 @@ +@inject ArchiveService ArchiveService +@using Microsoft.AspNetCore.Components.QuickGrid + + + + @if (ShowOnlyTotalPizza) + { + + } else{ + + + + } + + + + + + + +@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 }; + } +} diff --git a/PizzaBot/Components/Pages/AdminPizzaArchive.razor b/PizzaBot/Components/Pages/AdminPizzaArchive.razor new file mode 100644 index 0000000..7e07ea9 --- /dev/null +++ b/PizzaBot/Components/Pages/AdminPizzaArchive.razor @@ -0,0 +1,74 @@ +@page "/admin{secretpath}/archive" +@inject NavigationManager NavigationManager +@inject PizzaDBService PizzaDBService +@inject GlobalStuffService GlobalStuffService +@inject ArchiveService ArchiveService + +@rendermode InteractiveServer + +Pizza Archive + +

Archive

+ +

Add current to archive

+@if (GlobalStuffService.OrdersLocked) +{ + + +} +else +{ + Order's have to be locked to add today to the archive. +} + +
+ +
+ +
+ +@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(); + + 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(); + } +} diff --git a/PizzaBot/PizzaBot.csproj b/PizzaBot/PizzaBot.csproj index 1e134b0..56f8723 100644 --- a/PizzaBot/PizzaBot.csproj +++ b/PizzaBot/PizzaBot.csproj @@ -15,6 +15,7 @@ + all @@ -26,6 +27,7 @@ + diff --git a/PizzaBot/Program.cs b/PizzaBot/Program.cs index 2624d8c..6e1472d 100644 --- a/PizzaBot/Program.cs +++ b/PizzaBot/Program.cs @@ -17,6 +17,7 @@ builder.Services.AddDbContext(options => options.UseMySql(connecti builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/PizzaBot/Services/ArchiveService.cs b/PizzaBot/Services/ArchiveService.cs new file mode 100644 index 0000000..89e991b --- /dev/null +++ b/PizzaBot/Services/ArchiveService.cs @@ -0,0 +1,98 @@ +using PizzaBot.Models; +using System.Security.Cryptography; + +namespace PizzaBot.Services +{ + using PizzaArchiveType = List; + + 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 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; + } + + /// + /// Adds an entry to the archive + /// + /// the data to add + /// true, if sucessfull, false otherwise + 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); + } + } +} diff --git a/PizzaBot/Services/JSONService.cs b/PizzaBot/Services/JSONService.cs index a379354..a07697a 100644 --- a/PizzaBot/Services/JSONService.cs +++ b/PizzaBot/Services/JSONService.cs @@ -10,6 +10,9 @@ namespace PizzaBot.Services const string CONFIG_DIRECTORY_PATH = "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) { WebHostEnvironment = webHostEnvironment; @@ -34,5 +37,40 @@ namespace PizzaBot.Services { return Path.Combine(WebHostEnvironment.WebRootPath, CONFIG_DIRECTORY_PATH, PIZZA_CONFIG_FILENAME); } + + public List? GetPizzaArchive() + { + return ReadJson>(Path.Combine(LOG_DIRECTORY_PATH, PIZZA_LOG_FILENAME)); + } + + public void WriteNewPizzaArchive(List 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(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(jsonString); + } + + private void WriteJson(T toSerialize, string localpath) + { + string path = Path.Combine(WebHostEnvironment.WebRootPath ,localpath); + string jsonString = JsonSerializer.Serialize(toSerialize); + File.WriteAllText(path, jsonString); + } } }