Inheritance: RevisableEntity
 public void HasGetSet()
 {
     var value = new Language();
     var entity = new EstablishmentName { TranslationToLanguage = value };
     entity.ShouldNotBeNull();
     entity.TranslationToLanguage.ShouldEqual(value);
 }
 public void HasGetSet()
 {
     var value = new Establishment();
     var entity = new EstablishmentName { ForEstablishment = value };
     entity.ShouldNotBeNull();
     entity.ForEstablishment.ShouldEqual(value);
 }
            public void HasGetSet()
            {
                var value  = new Language();
                var entity = new EstablishmentName {
                    TranslationToLanguage = value
                };

                entity.ShouldNotBeNull();
                entity.TranslationToLanguage.ShouldEqual(value);
            }
            public void HasGetSet()
            {
                var value  = new Establishment();
                var entity = new EstablishmentName {
                    ForEstablishment = value
                };

                entity.ShouldNotBeNull();
                entity.ForEstablishment.ShouldEqual(value);
            }
        internal static string ToJsonAudit(this EstablishmentName entity)
        {
            var state = JsonConvert.SerializeObject(new
            {
                Id = entity.RevisionId,
                ForEstablishmentId      = entity.ForEstablishment.RevisionId,
                TranslationToLanguageId = (entity.TranslationToLanguage != null)
                    ? entity.TranslationToLanguage.Id : (int?)null,
                entity.Text,
                entity.IsOfficialName,
                entity.IsFormerName,
                entity.IsContextName,
            });

            return(state);
        }
Beispiel #6
0
        public void Handle(CreateEstablishmentName command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load owner
            var establishment = command.Owner ?? _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                x => x.Names.Select(y => y.TranslationToLanguage),
            })
                                .Single(x => x.RevisionId == command.OwnerId.Value)
            ;

            // update previous official name and owner when changing official name
            if (command.IsOfficialName)
            {
                var officialName = establishment.Names.SingleOrDefault(x => x.IsOfficialName);
                if (officialName != null)
                {
                    var changeCommand = new UpdateEstablishmentName(command.Principal)
                    {
                        Id             = officialName.RevisionId,
                        Text           = officialName.Text,
                        IsFormerName   = officialName.IsFormerName,
                        IsOfficialName = false,
                        LanguageCode   = (officialName.TranslationToLanguage != null)
                            ? officialName.TranslationToLanguage.TwoLetterIsoCode : null,
                        NoCommit = true,
                    };
                    _updateHandler.Handle(changeCommand);
                }
                establishment.OfficialName = command.Text;
            }

            // get new language
            var language = _entities.Get <Language>()
                           .SingleOrDefault(x => x.TwoLetterIsoCode.Equals(command.LanguageCode, StringComparison.OrdinalIgnoreCase));

            // create new establishment name
            var establishmentName = new EstablishmentName
            {
                Text = command.Text,
                TranslationToLanguage = language,
                IsOfficialName        = command.IsOfficialName,
                IsFormerName          = command.IsFormerName,
                IsContextName         = command.IsContextName,
            };

            establishment.Names.Add(establishmentName);
            establishmentName.ForEstablishment = establishment;

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    OwnerId = command.Owner != null ? command.Owner.RevisionId : command.OwnerId,
                    command.Text,
                    command.IsFormerName,
                    command.IsOfficialName,
                    command.IsContextName,
                    command.LanguageCode,
                }),
                NewState = establishmentName.ToJsonAudit(),
            };

            _entities.Create(audit);
            if (establishment.RevisionId != default(int))
            {
                _entities.Update(establishment);
            }
            command.CreatedEntity = establishmentName;
            if (command.NoCommit)
            {
                return;
            }

            _entities.SaveChanges();
            command.Id = establishmentName.RevisionId;
            _eventTrigger.Raise(new EstablishmentChanged());
        }