Skip to content

Commit

Permalink
Merge pull request #34 from xforman2/32-add-new-flag-for-newly-fetche…
Browse files Browse the repository at this point in the history
…d-estates

32 add new flag for newly fetched estates
  • Loading branch information
xforman2 authored Sep 4, 2024
2 parents 3d29dc9 + 68eb544 commit d9d946c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
45 changes: 30 additions & 15 deletions Backend/Services/DataProcessingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics;
using Backend.Clients;
using Backend.Mappers;
using HouseScout.Model;
Expand All @@ -22,36 +21,52 @@ HouseScoutContext context

public async Task ProcessData()
{
var existingEstates = _context.Estates.ToList();
var existingEstates = _context.Estates.ToDictionary(e => e.ApiId);

var fetchedEstates = new List<Estate>();
var fetchedEstatesDictionary = new Dictionary<string, Estate>();

foreach (var kvp in _clientsAndMappers)
{
IClient client = kvp.Key;
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<string>(fetchedEstates.Select(fe => fe.ApiId));
var existingEstatesSet = new HashSet<string>(existingEstates.Select(fe => fe.ApiId));
var estatesToAdd = new List<Estate>();
var estatesToRemove = new List<Estate>();

var estatesToAdd = fetchedEstates
.Where(fe => !existingEstatesSet.Contains(fe.ApiId))
.ToList();

var estatesToRemove = existingEstates
.Where(ee => !fetchedEstatesSet.Contains(ee.ApiId))
.ToList();
foreach (var estate in fetchedEstatesDictionary.Values)
{
if (!existingEstates.ContainsKey(estate.ApiId))
{
estate.New = true;
estatesToAdd.Add(estate);
}
}

if (estatesToAdd.Any())
foreach (var estate in existingEstates.Values)
{
if (fetchedEstatesDictionary.ContainsKey(estate.ApiId))
{
estate.New = false;
}
else
{
estatesToRemove.Add(estate);
}
}
if (estatesToAdd.Count > 0)
{
await _context.Estates.AddRangeAsync(estatesToAdd);
}

if (estatesToRemove.Any())
if (estatesToRemove.Count > 0)
{
_context.Estates.RemoveRange(estatesToRemove);
}
Expand Down
1 change: 0 additions & 1 deletion DiscordBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions SharedDependencies/Model/Estate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.ComponentModel.DataAnnotations;

namespace HouseScout.Model;

public class Estate
Expand Down

0 comments on commit d9d946c

Please sign in to comment.