Inheritance: RevisableEntity
Ejemplo n.º 1
0
        internal static string ToJsonAudit(this EstablishmentUrl entity)
        {
            var state = JsonConvert.SerializeObject(new
            {
                Id = entity.RevisionId,
                ForEstablishmentId = entity.ForEstablishment.RevisionId,
                entity.Value,
                entity.IsOfficialUrl,
                entity.IsFormerUrl,
            });

            return(state);
        }
Ejemplo n.º 2
0
        public void Handle(CreateEstablishmentUrl 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.Urls,
            })
                                .Single(x => x.RevisionId == command.OwnerId.Value)
            ;

            // update previous official URL and owner when changing official URL
            if (command.IsOfficialUrl)
            {
                var officialUrl = establishment.Urls.SingleOrDefault(x => x.IsOfficialUrl);
                if (officialUrl != null)
                {
                    var changeCommand = new UpdateEstablishmentUrl(command.Principal)
                    {
                        Id            = officialUrl.RevisionId,
                        Value         = officialUrl.Value,
                        IsFormerUrl   = officialUrl.IsFormerUrl,
                        IsOfficialUrl = false,
                        NoCommit      = true,
                    };
                    _updateHandler.Handle(changeCommand);
                }
                establishment.WebsiteUrl = command.Value;
            }

            // create new establishment URL
            var establishmentUrl = new EstablishmentUrl
            {
                Value         = command.Value,
                IsOfficialUrl = command.IsOfficialUrl,
                IsFormerUrl   = command.IsFormerUrl,
            };

            establishment.Urls.Add(establishmentUrl);
            establishmentUrl.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.Value,
                    command.IsFormerUrl,
                    command.IsOfficialUrl,
                }),
                NewState = establishmentUrl.ToJsonAudit(),
            };

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

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