public override void Handle(IDemographicUpdated command) { var existingDemographic = _demographicsRepository.CheckDemographicByExternalRef(command.ExternalRef); existingDemographic.Update(command.Name, command.ShortName, command.DisplayOrder, command.Gameplan); _demographicsRepository.Update(existingDemographic); _demographicsRepository.SaveChanges(); }
public IHttpActionResult Post([FromBody] List <CreateDemographicModel> commands) { if (commands == null || !commands.Any() || !ModelState.IsValid) { return(this.Error().InvalidParameters()); } if (commands.Any(cmd => cmd is null)) { return(this.Error().InvalidParameters("Demographics cannot be null")); } if (commands.Select(c => c.ExternalRef).Distinct().Count() != commands.Count) { return(this.Error().InvalidParameters("ExternalRef must be unique")); } if (commands.Any(c => String.IsNullOrEmpty(c.ExternalRef))) { return(this.Error().InvalidParameters("ExternalRef is not set")); } var externalRefs = commands.Select(x => x.ExternalRef).ToList(); var existingDemographic = _demographicRepository.GetByExternalRef(externalRefs); var addDemographic = commands.Where(c => existingDemographic.All(ex => ex.ExternalRef != c.ExternalRef)); _demographicRepository.Add(_mapper.Map <IEnumerable <Demographic> >(addDemographic)); var updateDemographic = existingDemographic.Select(updated => { var changes = commands.First(ex => ex.ExternalRef == updated.ExternalRef); _mapper.Map(changes, updated); return(updated); }); _demographicRepository.UpdateRange(updateDemographic); _demographicRepository.SaveChanges(); return(Ok()); }
/// <summary> /// Handles the command recieved with Masstransit /// </summary> /// <param name="command">this is bulk model of demographics that need to be updated in database</param> public override void Handle(IBulkDemographicCreatedOrUpdated command) { var demographicEntities = _mapper.Map<List<Demographic>>(command.Data); var existedDemographics = _demographicsRepository.GetByExternalRef(command.Data.Select(c => c.ExternalRef).ToList()); var demographics = new List<Demographic>(); foreach (var demographic in demographicEntities) { var demographicToUpdate = existedDemographics.FirstOrDefault(c => c.ExternalRef == demographic.ExternalRef); if (demographicToUpdate == null) { demographics.Add(demographic); } else { demographicToUpdate.Update(demographic.Name, demographic.ShortName, demographic.DisplayOrder, demographic.Gameplan); demographics.Add(demographicToUpdate); } } _demographicsRepository.InsertOrUpdate(demographics); _demographicsRepository.SaveChanges(); }
public override void Handle(IBulkDemographicDeleted command) { _demographicRepository.DeleteRangeByExternalRefs(command.Data.Select(c => c.ExternalRef)); _demographicRepository.SaveChanges(); }