From bd00b7a6fdf71b26a8aace3297edfbd17cc37610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Form=C3=A1nek?= Date: Wed, 4 Sep 2024 09:51:54 +0200 Subject: [PATCH 1/2] feat: setting the New field in DataProcessingService.cs --- Backend/Services/DataProcessingService.cs | 53 +++++++++++++++++------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/Backend/Services/DataProcessingService.cs b/Backend/Services/DataProcessingService.cs index e0ad03f..dac94e7 100644 --- a/Backend/Services/DataProcessingService.cs +++ b/Backend/Services/DataProcessingService.cs @@ -22,9 +22,9 @@ HouseScoutContext context public async Task ProcessData() { - var existingEstates = _context.Estates.ToList(); + var existingEstates = _context.Estates.ToDictionary(e => e.ApiId); - var fetchedEstates = new List(); + var fetchedEstatesDictionary = new Dictionary(); foreach (var kvp in _clientsAndMappers) { @@ -32,26 +32,53 @@ public async Task ProcessData() IMapper mapper = kvp.Value; var data = await client.FetchDataAsync(); - fetchedEstates.AddRange(mapper.MapResponseToModel(data)); + var fetchedEstates = mapper.MapResponseToModel(data); + foreach (var estate in fetchedEstates) + { + fetchedEstatesDictionary[estate.ApiId] = estate; + } } - var fetchedEstatesSet = new HashSet(fetchedEstates.Select(fe => fe.ApiId)); - var existingEstatesSet = new HashSet(existingEstates.Select(fe => fe.ApiId)); + var estatesToAdd = new List(); + var estatesToRemove = new List(); + + foreach (var estate in fetchedEstatesDictionary.Values) + { + if (!existingEstates.ContainsKey(estate.ApiId)) + { + estate.New = true; + estatesToAdd.Add(estate); + } + } + + foreach (var estate in existingEstates.Values) + { + if (fetchedEstatesDictionary.ContainsKey(estate.ApiId)) + { + estate.New = false; + } + else + { + estatesToRemove.Add(estate); + } + } - var estatesToAdd = fetchedEstates - .Where(fe => !existingEstatesSet.Contains(fe.ApiId)) - .ToList(); + foreach (var estate in estatesToAdd) + { + Console.WriteLine($"Adding {estate.Link}"); + } - var estatesToRemove = existingEstates - .Where(ee => !fetchedEstatesSet.Contains(ee.ApiId)) - .ToList(); + foreach (var estate in estatesToRemove) + { + Console.WriteLine($"Deleting {estate.Link}"); + } - if (estatesToAdd.Any()) + if (estatesToAdd.Count > 0) { await _context.Estates.AddRangeAsync(estatesToAdd); } - if (estatesToRemove.Any()) + if (estatesToRemove.Count > 0) { _context.Estates.RemoveRange(estatesToRemove); } From 68eb544d4dc7ad12c3896a6a913368cf7b43d6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Form=C3=A1nek?= Date: Wed, 4 Sep 2024 09:53:44 +0200 Subject: [PATCH 2/2] refactor: formatting --- Backend/Services/DataProcessingService.cs | 16 ++-------------- DiscordBot/Program.cs | 1 - SharedDependencies/Model/Estate.cs | 2 -- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/Backend/Services/DataProcessingService.cs b/Backend/Services/DataProcessingService.cs index dac94e7..35f0a2c 100644 --- a/Backend/Services/DataProcessingService.cs +++ b/Backend/Services/DataProcessingService.cs @@ -1,4 +1,3 @@ -using System.Diagnostics; using Backend.Clients; using Backend.Mappers; using HouseScout.Model; @@ -41,7 +40,7 @@ public async Task ProcessData() var estatesToAdd = new List(); var estatesToRemove = new List(); - + foreach (var estate in fetchedEstatesDictionary.Values) { if (!existingEstates.ContainsKey(estate.ApiId)) @@ -50,7 +49,7 @@ public async Task ProcessData() estatesToAdd.Add(estate); } } - + foreach (var estate in existingEstates.Values) { if (fetchedEstatesDictionary.ContainsKey(estate.ApiId)) @@ -62,17 +61,6 @@ public async Task ProcessData() estatesToRemove.Add(estate); } } - - foreach (var estate in estatesToAdd) - { - Console.WriteLine($"Adding {estate.Link}"); - } - - foreach (var estate in estatesToRemove) - { - Console.WriteLine($"Deleting {estate.Link}"); - } - if (estatesToAdd.Count > 0) { await _context.Estates.AddRangeAsync(estatesToAdd); diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index f0b8f1a..a2cfcb5 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -4,7 +4,6 @@ using Discord.Interactions; using Discord.WebSocket; using HouseScout.Filters; -using HouseScout.Model; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; diff --git a/SharedDependencies/Model/Estate.cs b/SharedDependencies/Model/Estate.cs index 0b457f3..5a140a6 100644 --- a/SharedDependencies/Model/Estate.cs +++ b/SharedDependencies/Model/Estate.cs @@ -1,5 +1,3 @@ -using System.ComponentModel.DataAnnotations; - namespace HouseScout.Model; public class Estate