diff --git a/Controllers/AanestysController.cs b/Controllers/AanestysController.cs index f5fc328..bd4adee 100644 --- a/Controllers/AanestysController.cs +++ b/Controllers/AanestysController.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq; using RuokalistaServer.Data; using RuokalistaServer.Models; +using System.Text.Json; namespace RuokalistaServer.Controllers { @@ -31,6 +32,71 @@ public IActionResult Tulokset(string? isApp = "false") return View(); } + [HttpGet] + [Route("api/v1/Aanestys/ProsenttiTuloksetTietyllaAikavalilla")] + public IActionResult ProsenttiTuloksetTietyllaAikavalilla(string start, string? end) + { + var startDate = DateTime.Parse(start); + var endDate = DateTime.Now; + if (end != null && end != "") + { + endDate = DateTime.Parse(end); + } + var startWeek = System.Globalization.ISOWeek.GetWeekOfYear(startDate); + var endWeek = System.Globalization.ISOWeek.GetWeekOfYear(endDate); + + + var ruokalistaObjects = db.Ruokalista.Where(x => x.Year >= startDate.Year && x.WeekId >= startWeek && x.Year <= endDate.Year && x.WeekId <= endWeek); + + var returnList = new List(); + foreach (var r in ruokalistaObjects) + { + + var prosentit = new ViikonProsentit + { + maanantai = 0, + tiistai = 0, + keskiviikko = 0, + torstai = 0, + perjantai = 0 + }; + + var VV = db.Votes.FirstOrDefault(x => x.ruokalistaId == r.Id); + if (VV != null) + { + prosentit.maanantai = CalculateFoodScore(VV.level1_votes_maanantai, VV.level2_votes_maanantai, VV.level3_votes_maanantai, VV.level4_votes_maanantai); + prosentit.tiistai = CalculateFoodScore(VV.level1_votes_tiistai, VV.level2_votes_tiistai, VV.level3_votes_tiistai, VV.level4_votes_tiistai); + prosentit.keskiviikko = CalculateFoodScore(VV.level1_votes_keskiviikko, VV.level2_votes_keskiviikko, VV.level3_votes_keskiviikko, VV.level4_votes_keskiviikko); + prosentit.torstai = CalculateFoodScore(VV.level1_votes_torstai, VV.level2_votes_torstai, VV.level3_votes_torstai, VV.level4_votes_torstai); + prosentit.perjantai = CalculateFoodScore(VV.level1_votes_perjantai, VV.level2_votes_perjantai, VV.level3_votes_perjantai, VV.level4_votes_perjantai); + } + + + + var rObj = new ProsenttiTulos + { + ruokalista = r, + prosentit = prosentit, + + }; + returnList.Add(rObj); + } + + return Ok(JsonConvert.SerializeObject(returnList)); + } + + private double CalculateFoodScore(int level1, int level2, int level3, int level4) + { + var yhteensa = level1 + level2 + level3 + level4; + if(yhteensa == 0) + { + return 0; + } + + return (1 * ((double)level4 / yhteensa)) + (0.75 * ((double)level3 / yhteensa)) + (0.50 * ((double)level2 / yhteensa)) + (0.25 * ((double)level1 / yhteensa)); + + } + [HttpGet] @@ -252,63 +318,12 @@ public async Task Aanesta(int taso) return Ok("aanestys onnistui"); } - [Authorize] - public async Task Tulostaulu() + + public IActionResult Tulostaulu() { - var model = new TulostauluModel(); - var lista = new List<(string, double)>(); - - foreach(var week in db.Ruokalista) - { - var viikonVotet = db.Votes.FirstOrDefault(x => x.ruokalistaId == week.Id); - if (viikonVotet == null) continue; - - var maanantai_yhteensa = viikonVotet.level4_votes_maanantai + viikonVotet.level3_votes_maanantai + viikonVotet.level2_votes_maanantai + viikonVotet.level1_votes_maanantai; - if(maanantai_yhteensa != 0) - { - lista.Add((week.Maanantai, (1 * ((double)viikonVotet.level4_votes_maanantai / maanantai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_maanantai / maanantai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_maanantai / maanantai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_maanantai / maanantai_yhteensa)))); - } - - var tiistai_yhteensa = viikonVotet.level4_votes_tiistai + viikonVotet.level3_votes_tiistai + viikonVotet.level2_votes_tiistai + viikonVotet.level1_votes_tiistai; - if(tiistai_yhteensa != 0) - { - lista.Add((week.Tiistai, (1 * ((double)viikonVotet.level4_votes_tiistai / tiistai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_tiistai / tiistai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_tiistai / tiistai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_tiistai / tiistai_yhteensa)))); - } - - var keskiviikko_yhteensa = viikonVotet.level4_votes_keskiviikko + viikonVotet.level3_votes_keskiviikko + viikonVotet.level2_votes_keskiviikko + viikonVotet.level1_votes_keskiviikko; - if(keskiviikko_yhteensa != 0) - { - lista.Add((week.Keskiviikko, (1 * ((double)viikonVotet.level4_votes_keskiviikko / keskiviikko_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_keskiviikko / keskiviikko_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_keskiviikko / keskiviikko_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_keskiviikko / keskiviikko_yhteensa)))); - } - - var torstai_yhteensa = viikonVotet.level4_votes_torstai + viikonVotet.level3_votes_torstai + viikonVotet.level2_votes_torstai + viikonVotet.level1_votes_torstai; - if(torstai_yhteensa != 0) - { - lista.Add((week.Torstai, (1 * ((double)viikonVotet.level4_votes_torstai / torstai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_torstai / torstai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_torstai / torstai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_torstai / torstai_yhteensa)))); - } - var perjantai_yhteensa = viikonVotet.level4_votes_perjantai + viikonVotet.level3_votes_perjantai + viikonVotet.level2_votes_perjantai + viikonVotet.level1_votes_perjantai; - if(perjantai_yhteensa != 0) - { - lista.Add((week.Perjantai, (1 * ((double)viikonVotet.level4_votes_perjantai / perjantai_yhteensa)) + (0.75 * ((double)viikonVotet.level3_votes_perjantai / perjantai_yhteensa)) + (0.50 * ((double)viikonVotet.level2_votes_perjantai / perjantai_yhteensa)) + (0.25 * ((double)viikonVotet.level1_votes_perjantai / perjantai_yhteensa)))); - } - } - - - lista = lista.OrderBy(x => x.Item2).ToList(); - lista.Reverse(); - - - var dataStr = "[{x: " + lista.First().Item2.ToString().Replace(',', '.') + ", y: '" + lista.First().Item1 + "'}"; - foreach(var item in lista.Skip(1)) - { - dataStr += ", {x: " + item.Item2.ToString().Replace(',', '.') + ", y: '" + item.Item1 + "'}"; - } - dataStr += "]"; - model.data = dataStr; - - return View(model); + return View(); } } } diff --git a/Models/ProsenttiTulos.cs b/Models/ProsenttiTulos.cs new file mode 100644 index 0000000..87aa71b --- /dev/null +++ b/Models/ProsenttiTulos.cs @@ -0,0 +1,17 @@ +namespace RuokalistaServer.Models +{ + public class ProsenttiTulos + { + public Ruokalista ruokalista { get; set; } + public ViikonProsentit prosentit { get; set; } + } + + public class ViikonProsentit + { + public double maanantai { get; set; } + public double tiistai { get; set; } + public double keskiviikko { get; set; } + public double torstai { get; set; } + public double perjantai { get; set; } + } +} diff --git a/Models/TulostauluModel.cs b/Models/TulostauluModel.cs deleted file mode 100644 index dae8c29..0000000 --- a/Models/TulostauluModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Newtonsoft.Json.Linq; - -namespace RuokalistaServer.Models -{ - public class TulostauluModel - { - public string? data { get; set; } - } -} diff --git a/Views/Aanestys/Tulostaulu.cshtml b/Views/Aanestys/Tulostaulu.cshtml index 13b3657..d2fe145 100644 --- a/Views/Aanestys/Tulostaulu.cshtml +++ b/Views/Aanestys/Tulostaulu.cshtml @@ -1,49 +1,73 @@ @using Newtonsoft.Json @using RuokalistaServer.Models; -@model TulostauluModel @{ Layout = "_Layout"; ViewData["Title"] = "Tulostaulu"; } - -

Tulostaulu

+ + + + + + + + + +
- + +
+
+
+

Tulostaulu

+ +
+
+
Aikaväli
+ +
+
+ +
+ +
+
- + + + const today = new Date(); + const dayOfWeek = today.getDay(); + const diffMonday = today.getDate() - dayOfWeek + (dayOfWeek === 0 ? -6 : 1); // Adjust for Sunday + const diffFriday = diffMonday + 4; + + const friday = new Date(today.setDate(diffFriday)); + const monday = new Date(today.setDate(diffMonday)); + + + create_tulostaulu(contentBody, monday, friday) + \ No newline at end of file diff --git a/wwwroot/css/tulostaulu.css b/wwwroot/css/tulostaulu.css new file mode 100644 index 0000000..700567a --- /dev/null +++ b/wwwroot/css/tulostaulu.css @@ -0,0 +1,28 @@ +.date-container { + display: flex; + align-items: center; + justify-content: space-between; + padding: 20px; + box-sizing: border-box; +} + +.input-container { + text-align: left; + margin-right: 20px; +} + +.title { + flex: 1; + margin-right: 20px; +} + +input { + width: 100%; + box-sizing: border-box; + padding: 10px; + background-color: #262626; + color: #ffffff; + border-radius: 5px +} + + diff --git a/wwwroot/js/tulostaulu.js b/wwwroot/js/tulostaulu.js new file mode 100644 index 0000000..d9846a2 --- /dev/null +++ b/wwwroot/js/tulostaulu.js @@ -0,0 +1,39 @@ +function create_tulostaulu(contentBody, startTime, endTime) { + //muista että teet sellasen josta voi valita aikavälin (custom, tämäviikko, viimeviikko, tämä kuukausi, viime kuukausi, viimeiset kolme kuukautta, tämä vuosi, viime vuosi, koko aika ja custom) + //aikavälin keskiarvo + //toinen valinta että näyttää vaan tietyt päivät + //ruokien arvostelujen kehitys ajan funktiona + var startDate = new Date(startTime) + var endDate = new Date(endTime) + console.log("reguested for " + startDate.toISOString() + " - " + endDate.toISOString()) + + const formattedStartDate = formatDateForURL(startDate); + const formattedEndDate = formatDateForURL(endDate); + + const url = `/api/v1/Aanestys/ProsenttiTuloksetTietyllaAikavalilla?start=${formattedStartDate}&end=${formattedEndDate}`; + + //get all of the votes in procentage form + + fetch(url) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(data => { + console.log('Data:', data); + // You can do something with the data here + }) + .catch(error => { + console.error('Error:', error); + }); + +} + + +function formatDateForURL(date) { + const options = { year: 'numeric', month: '2-digit', day: '2-digit' }; + const formattedDate = date.toLocaleDateString('fi-FI', options); + return formattedDate.replace(/\//g, '-'); +} \ No newline at end of file