public async Task DeleteAsync(BurnRuleModel burnRuleModel) { using (var context = _msSqlContextFactory.CreateDataContext()) { burnRuleModel.IsDeleted = true; var entity = _mapper.Map <BurnRuleEntity>(burnRuleModel); context.BurnRules.Update(entity); await context.SaveChangesAsync(); } }
public async Task UpdateAsync(BurnRuleModel burnRuleModel) { var entity = _mapper.Map <BurnRuleEntity>(burnRuleModel); using (var context = _msSqlContextFactory.CreateDataContext()) { // Skip updating the burn rule partners because we handle that separately entity.BurnRulePartners = null; context.BurnRules.Update(entity); await context.SaveChangesAsync(); } }
public async Task <Guid> InsertAsync(BurnRuleModel burnRuleModel) { using (var context = _msSqlContextFactory.CreateDataContext()) { var entity = _mapper.Map <BurnRuleEntity>(burnRuleModel); context.Add(entity); await context.SaveChangesAsync(); return(entity.Id); } }
public async Task UpdateAsync(BurnRuleModel burnRuleModel) { var oldEntity = await _burnRuleRepository.GetAsync(burnRuleModel.Id); if (oldEntity == null) { throw new EntityNotFoundException($"Burn rule with id {burnRuleModel.Id} does not exist."); } var response = _burnRuleContentValidation. ValidateHaveInvalidOrEmptyIds(burnRuleModel.BurnRuleContents.Select(c => c.Id).ToList(), oldEntity.BurnRuleContents.Select(c => c.Id).ToList()); if (!response.IsValid) { throw new EntityNotValidException(string.Join(Environment.NewLine, response.ValidationMessages)); } burnRuleModel.CreationDate = oldEntity.CreationDate; burnRuleModel.CreatedBy = oldEntity.CreatedBy; if (burnRuleModel.UsePartnerCurrencyRate) { burnRuleModel.AmountInTokens = null; burnRuleModel.AmountInCurrency = null; } var contentsToRemove = oldEntity.BurnRuleContents.Where(c1 => burnRuleModel.BurnRuleContents.All(c2 => c1.Id != c2.Id)).ToList(); await _burnRuleContentRepository.DeleteAsync(contentsToRemove); foreach (var content in contentsToRemove) { if (content.RuleContentType == RuleContentType.UrlForPicture) { await _fileService.DeleteAsync(content.Id); } } var partnersToRemove = oldEntity.PartnerIds.Where(c1 => burnRuleModel.PartnerIds.All(c2 => c1 != c2)).ToList(); var partnersToAdd = burnRuleModel.PartnerIds.Where(c1 => oldEntity.PartnerIds.All(c2 => c1 != c2)).ToList(); await _burnRulePartnerRepository.DeleteAsync(partnersToRemove, burnRuleModel.Id); await _burnRulePartnerRepository.InsertAsync(partnersToAdd, burnRuleModel.Id); await _burnRuleRepository.UpdateAsync(burnRuleModel); await PublishSpendRuleChangeEvent(burnRuleModel, ActionType.Edited); }
public async Task <Guid> InsertAsync(BurnRuleModel burnRuleModel) { burnRuleModel.CreationDate = DateTime.UtcNow; if (burnRuleModel.UsePartnerCurrencyRate) { burnRuleModel.AmountInTokens = null; burnRuleModel.AmountInCurrency = null; } var earnRuleId = await _burnRuleRepository.InsertAsync(burnRuleModel); await PublishSpendRuleChangeEvent(burnRuleModel, ActionType.Created); return(earnRuleId); }
private async Task PublishSpendRuleChangeEvent(BurnRuleModel spendRule, ActionType actionType) { var @event = new SpendRuleChangedEvent { Id = Guid.NewGuid(), SpendRuleId = spendRule.Id, Title = spendRule.Title, Description = spendRule.Description, AmountInTokens = spendRule.AmountInTokens, AmountInCurrency = spendRule.AmountInCurrency, TimeStamp = DateTime.UtcNow, Action = actionType }; _log.Info("Spend rule change event published", @event); await _spendRuleChangeEventPublisher.PublishAsync(@event); }