Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

32 add new flag for newly fetched estates #34

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -17,7 +15,7 @@
public bool New { get; set; }

// for seeding to work, do not remove
public Estate() { }

Check warning on line 18 in SharedDependencies/Model/Estate.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ApiId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 18 in SharedDependencies/Model/Estate.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 18 in SharedDependencies/Model/Estate.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Link' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public Estate(
ApiType apiType,
Expand Down
Loading