commitglobal / votemonitor

https://votemonitor.vercel.app
Mozilla Public License 2.0
4 stars 14 forks source link

Endpoints for getting/listing data should not fetch aggregates #107

Open idormenco opened 5 months ago

idormenco commented 5 months ago

Currently when returning data for a GET or a LIST we get whole aggregate/s and then we map to the model this results in slow queries and huge amount of data fetched over the wire.

examples:

public class ListElectionRoundsSpecification : Specification<ElectionRoundAggregate, ElectionRoundBaseModel>
{
    public ListElectionRoundsSpecification(List.Request request)
    {
        Query
            .Search(x => x.Title, "%" + request.TitleFilter + "%", !string.IsNullOrEmpty(request.TitleFilter))
            .Where(x => x.Status == request.Status, request.Status != null)
            .Where(x => x.CountryId == request.CountryId, request.CountryId != null)
            .ApplyOrdering(request)
            .Paginate(request);

        Query.Select(x => new ElectionRoundBaseModel
        {
            Id = x.Id,
            Title = x.Title,
            EnglishTitle = x.EnglishTitle,
            StartDate = x.StartDate,
            Status = x.Status,
            CreatedOn = x.CreatedOn,
            LastModifiedOn = x.LastModifiedOn
        });
    }
}
public class GetElectionRoundByIdSpecification : SingleResultSpecification<ElectionRoundAggregate, ElectionRoundModel>
{
    public GetElectionRoundByIdSpecification(Guid id)
    {
        Query
            .Where(x => x.Id == id)
            .Include(x => x.MonitoringNgos)
            .ThenInclude(x => x.Ngo)
            .Include(x => x.MonitoringObservers)
            .ThenInclude(x => x.Observer)
            .Include(x => x.MonitoringObservers)
            .ThenInclude(x => x.InviterNgo);

        Query.Select(electionRound => new ElectionRoundModel
        {
            Id = electionRound.Id,
            Title = electionRound.Title,
            EnglishTitle = electionRound.EnglishTitle,
            Status = electionRound.Status,
            StartDate = electionRound.StartDate,
            LastModifiedOn = electionRound.LastModifiedOn,
            CreatedOn = electionRound.CreatedOn,
            MonitoringObservers = electionRound.MonitoringObservers.Select(observer => new MonitoringObserverModel
            {
                ObserverId = observer.ObserverId,
                Name = observer.Observer.Name,
                Status = observer.Observer.Status
            }).ToList(),
            MonitoringNgos = electionRound.MonitoringNgos.Select(ngo => new MonitoringNgoModel
            {
                NgoId = ngo.NgoId,
                Name = ngo.Ngo.Name,
                Status = ngo.Ngo.Status
            }).ToList()
        });
    }
}
cosminbuda commented 4 months ago

as putea sa iau eu taskul asta