Skip to content

Commit

Permalink
Merge pull request #594 from bcgov/yj
Browse files Browse the repository at this point in the history
chore: performance enhancement
  • Loading branch information
ychung-mot authored Aug 30, 2024
2 parents 02a28a8 + 2e854b9 commit 2610e51
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions server/StrDss.Data/Repositories/RentalListingRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using StrDss.Model.DelistingDtos;
using StrDss.Model.RentalReportDtos;
using System.Diagnostics;
using System.Reflection;

namespace StrDss.Data.Repositories
{
Expand Down Expand Up @@ -65,9 +66,14 @@ var extraSort

var listings = await Page<DssRentalListingVw, RentalListingViewDto>(query, pageSize, pageNumber, orderBy, direction, extraSort);

foreach (var listing in listings.SourceList)
{
await SetExtraProperties(listing);
var contacts = _mapper.Map<List<RentalListingContactDto>>(await _dbContext.DssRentalListingContacts
.AsNoTracking()
.Where(contact => listings.SourceList.Select(listing => listing.RentalListingId).Contains(contact.ContactedThroughRentalListingId))
.ToListAsync());

foreach (var listing in listings.SourceList) {
listing.LastActionDtm = listing.LastActionDtm == null ? null : DateUtils.ConvertUtcToPacificTime(listing.LastActionDtm.Value);
listing.Hosts = contacts.Where(x => x.ContactedThroughRentalListingId == listing.RentalListingId).ToList();
}

return listings;
Expand Down Expand Up @@ -242,9 +248,7 @@ await _dbSet.AsNoTracking()

var contacts = _mapper.Map<List<RentalListingContactDto>>(await _dbContext.DssRentalListingContacts
.AsNoTracking()
.Where(contact => listings
.Select(listing => listing.RentalListingId)
.Contains(contact.ContactedThroughRentalListingId))
.Where(contact => listings.Select(listing => listing.RentalListingId).Contains(contact.ContactedThroughRentalListingId))
.ToListAsync());

foreach (var listing in listings)
Expand Down Expand Up @@ -280,16 +284,6 @@ await _dbSet.AsNoTracking()
return listings;
}

private async Task SetExtraProperties(RentalListingViewDto listing)
{
listing.LastActionDtm = listing.LastActionDtm == null ? null : DateUtils.ConvertUtcToPacificTime(listing.LastActionDtm.Value);
listing.Hosts =
_mapper.Map<List<RentalListingContactDto>>(await
_dbContext.DssRentalListingContacts
.Where(x => x.ContactedThroughRentalListingId == listing.RentalListingId)
.ToListAsync());
}

public async Task<RentalListingViewDto?> GetRentalListing(long rentalListingId, bool loadHistory = true)
{
var listing = _mapper.Map<RentalListingViewDto>(await _dbSet.AsNoTracking().FirstOrDefaultAsync(x => x.RentalListingId == rentalListingId));
Expand All @@ -301,7 +295,13 @@ private async Task SetExtraProperties(RentalListingViewDto listing)
return null;
}

await SetExtraProperties(listing);
listing.LastActionDtm = listing.LastActionDtm == null ? null : DateUtils.ConvertUtcToPacificTime(listing.LastActionDtm.Value);

listing.Hosts =
_mapper.Map<List<RentalListingContactDto>>(await
_dbContext.DssRentalListingContacts
.Where(x => x.ContactedThroughRentalListingId == listing.RentalListingId)
.ToListAsync());

if (!loadHistory) return listing;

Expand Down

0 comments on commit 2610e51

Please sign in to comment.