From 1912feb9cfe51deaedbe3abe9239fd1bcf2b37f8 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Wed, 25 Oct 2023 20:28:48 +0200 Subject: Make voting work --- .../Elements.Backend/Services/FinishVoteService.cs | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 backend/Elements.Backend/Services/FinishVoteService.cs (limited to 'backend/Elements.Backend/Services/FinishVoteService.cs') diff --git a/backend/Elements.Backend/Services/FinishVoteService.cs b/backend/Elements.Backend/Services/FinishVoteService.cs new file mode 100644 index 0000000..8486036 --- /dev/null +++ b/backend/Elements.Backend/Services/FinishVoteService.cs @@ -0,0 +1,95 @@ +using Elements.Data; +using Elements.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace Elements.Backend.Services; + +public class FinishVoteService +{ + private readonly ApplicationDbContext _dbContext; + private readonly ILogger _logger; + + public FinishVoteService(ApplicationDbContext dbContext, ILogger logger) + { + _dbContext = dbContext; + _logger = logger; + } + + public async Task DoSomethingAsync() + { + _logger.LogDebug("Starting finalizing votes"); + List finishedSuggestions = _dbContext.Suggestions + .Include(s => s.Votes) + .Where(s => DateTime.UtcNow > s.VotingEnd) + .ToList(); + + if (finishedSuggestions.Count == 0) + return; + + bool finished = false; + while (!finished) + { + Suggestion currentSuggestion = finishedSuggestions.First(); + int firstElementId = currentSuggestion.FirstElementId; + int secondElementId = currentSuggestion.SecondElementId; + //Get all suggestions with same recipe as current suggestion + List sameRecipeSuggestions = finishedSuggestions.Where(s => + (s.FirstElementId == firstElementId && s.SecondElementId == secondElementId) || + (s.FirstElementId == secondElementId && s.SecondElementId == firstElementId)) + .ToList(); + sameRecipeSuggestions.Sort((s1, s2) => s2.Votes.Count.CompareTo(s1.Votes.Count)); + + Suggestion winner; + if (sameRecipeSuggestions.Count > 1 && + sameRecipeSuggestions[0].Votes.Count == sameRecipeSuggestions[1].Votes.Count) + { + List sameAmountOfVotes = sameRecipeSuggestions + .Where(s => s.Votes.Count == sameRecipeSuggestions[0].Votes.Count) + .OrderBy(s => s.CreationDate) + .ToList(); + + winner = sameAmountOfVotes[0]; + } + else + { + winner = sameRecipeSuggestions[0]; + } + + Element newElement = new() + { + CreationDate = winner.CreationDate, + UserId = winner.UserId, + Name = winner.Name, + State = ElementState.HasColor, + IconPng = winner.Icon + }; + await _dbContext.Elements.AddAsync(newElement); + await _dbContext.SaveChangesAsync(); + + await _dbContext.Recipes.AddAsync(new Recipe + { + FirstElementId = winner.FirstElementId, + SecondElementId = winner.SecondElementId, + ResultElementId = newElement.Id, + }); + + //Delete votes + foreach (Vote vote in sameRecipeSuggestions.SelectMany(suggestion => suggestion.Votes)) + _dbContext.Entry(vote).State = EntityState.Deleted; + + //Delete suggestions + foreach (Suggestion suggestion in sameRecipeSuggestions) + _dbContext.Entry(suggestion).State = EntityState.Deleted; + + await _dbContext.SaveChangesAsync(); + + finishedSuggestions = finishedSuggestions + .Where(s => !(s.FirstElementId == firstElementId && s.SecondElementId == secondElementId) && + !(s.FirstElementId == secondElementId && s.SecondElementId == firstElementId)) + .ToList(); + + if (finishedSuggestions.Count == 0) + finished = true; + } + } +} \ No newline at end of file -- cgit v1.2.3