Skip to content

Commit

Permalink
Minor performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Nesterov committed Apr 29, 2020
1 parent 7dafb86 commit ec14471
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ICampaignsRepository
Task<Guid> CreateAsync(VoucherCampaign campaign);
Task UpdateAsync(VoucherCampaign campaign);
Task DeleteAsync(VoucherCampaign campaign);
Task<VoucherCampaign> GetByIdAsync(Guid campaignId);
Task<VoucherCampaign> GetByIdAsync(Guid campaignId, bool includeContents);
Task<IReadOnlyCollection<VoucherCampaign>> GetCampaignsByIdsAsync(Guid[] campaignIds);
Task<CampaignsPage> GetCampaignsAsync(CampaignListRequest request);
Task<(int publishedCampaingsVouchersCount, int activeCampaingsVouchersCount)>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<Guid> CreateAsync(VoucherCampaign campaign)

public async Task<UpdateCampaignError> UpdateAsync(VoucherCampaign campaign)
{
var oldCampaign = await _campaignsRepository.GetByIdAsync(campaign.Id);
var oldCampaign = await _campaignsRepository.GetByIdAsync(campaign.Id, true);
if (oldCampaign == null)
{
_log.Error($"Campaign {campaign.Id} not found for update");
Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task<UpdateCampaignError> UpdateAsync(VoucherCampaign campaign)

public async Task<bool> DeleteAsync(Guid campaignId)
{
var campaign = await _campaignsRepository.GetByIdAsync(campaignId);
var campaign = await _campaignsRepository.GetByIdAsync(campaignId, true);

if (campaign == null)
{
Expand All @@ -116,7 +116,7 @@ public async Task<bool> DeleteAsync(Guid campaignId)

public async Task<VoucherCampaign> GetByIdAsync(Guid campaignId)
{
var campaign = await _campaignsRepository.GetByIdAsync(campaignId);
var campaign = await _campaignsRepository.GetByIdAsync(campaignId, true);

if (campaign == null)
return null;
Expand All @@ -142,7 +142,7 @@ public Task<IReadOnlyCollection<VoucherCampaign>> GetCampaignsByIdsAsync(Guid[]

public async Task<ImageSaveError> SaveCampaignContentImage(FileModel file)
{
var campaign = await _campaignsRepository.GetByIdAsync(file.CampaignId);
var campaign = await _campaignsRepository.GetByIdAsync(file.CampaignId, true);

if (campaign == null)
return ImageSaveError.VoucherCampaignNotFound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Lykke.RabbitMqBroker" Version="7.13.1" />
<PackageReference Include="MAVN.Service.PaymentManagement.Client" Version="1.7.0" />
<PackageReference Include="MAVN.Service.PaymentManagement.Client" Version="1.9.0" />
<PackageReference Include="StackExchange.Redis" Version="2.1.30" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<ProcessingVoucherError> ProcessPaymentRequestAsync(Guid paymen

public async Task<VoucherReservationResult> ReserveVoucherAsync(Guid voucherCampaignId, Guid ownerId)
{
var campaign = await _campaignsRepository.GetByIdAsync(voucherCampaignId);
var campaign = await _campaignsRepository.GetByIdAsync(voucherCampaignId, false);
if (campaign == null)
return new VoucherReservationResult { ErrorCode = ProcessingVoucherError.VoucherCampaignNotFound };

Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task<RedeemVoucherError> RedeemVoucherAsync(string voucherShortCode
if (voucher == null)
return RedeemVoucherError.VoucherNotFound;

var campaign = await _campaignsRepository.GetByIdAsync(voucher.CampaignId);
var campaign = await _campaignsRepository.GetByIdAsync(voucher.CampaignId, false);
if (campaign == null)
return RedeemVoucherError.VoucherCampaignNotFound;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,17 @@ public async Task DeleteAsync(VoucherCampaign campaign)
}
}

public async Task<VoucherCampaign> GetByIdAsync(Guid campaignId)
public async Task<VoucherCampaign> GetByIdAsync(Guid campaignId, bool includeContents)
{
using (var context = _contextFactory.CreateDataContext())
{
var entity = await context.VoucherCampaigns
.Where(c => c.Id == campaignId)
.Include(c => c.LocalizedContents)
.FirstOrDefaultAsync();
var query = context.VoucherCampaigns
.Where(c => c.Id == campaignId);

if (includeContents)
query = query.Include(c => c.LocalizedContents);

var entity = await query.FirstOrDefaultAsync();

return _mapper.Map<VoucherCampaign>(entity);
}
Expand Down

0 comments on commit ec14471

Please sign in to comment.