コード例 #1
0
        public void Handle(UpdateMyPhoto command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                x => x.Photo,
            })
                         .ByUserName(command.Principal.Identity.Name);

            // delete previous file
            _photoDeleteHandler.Handle(new DeleteMyPhoto(command.Principal)
            {
                NoCommit = true,
            });

            // create new file
            var path         = string.Format(Person.PhotoPathFormat, person.RevisionId, Guid.NewGuid());
            var externalFile = new ExternalFile
            {
                Name     = command.Name,
                Path     = path,
                Length   = command.Content.Length,
                MimeType = command.MimeType,
            };

            person.Photo = externalFile;
            _binaryData.Put(path, command.Content);

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    User = command.Principal.Identity.Name,
                    command.Content,
                    command.Name,
                    command.MimeType,
                }),
                NewState = externalFile.ToJsonAudit(),
            };

            // push to database
            _entities.Create(externalFile);
            _entities.Update(person);
            _entities.Create(audit);
            if (!command.NoCommit)
            {
                _unitOfWork.SaveChanges();
            }
        }
コード例 #2
0
        public void Handle(GrantRoleToUserCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var role = command.Role ??
                       _entities.Get <Role>()
                       .EagerLoad(_entities, new Expression <Func <Role, object> >[]
            {
                r => r.Grants,
            })
                       .By(command.RoleGuid);

            var grant = role.Grants.ByUser(command.UserGuid);

            if (grant != null)
            {
                return;
            }

            var user = _entities.Get <User>().By(command.UserGuid);

            grant = new RoleGrant
            {
                Role = role,
                User = user,
            };
            _entities.Create(grant);
            command.IsNewlyGranted = true;
        }
コード例 #3
0
        public void Handle(DeleteEstablishmentName command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // load target
            var establishmentName = _entities.Get <EstablishmentName>()
                                    .EagerLoad(_entities, new Expression <Func <EstablishmentName, object> >[]
            {
                x => x.ForEstablishment,
                x => x.TranslationToLanguage,
            })
                                    .SingleOrDefault(x => x.RevisionId == command.Id)
            ;

            if (establishmentName == null)
            {
                return;                            // delete idempotently
            }
            // log audit
            var audit = new CommandEvent
            {
                RaisedBy      = command.Principal.Identity.Name,
                Name          = command.GetType().FullName,
                Value         = JsonConvert.SerializeObject(new { command.Id }),
                PreviousState = establishmentName.ToJsonAudit(),
            };

            _entities.Create(audit);
            _entities.Purge(establishmentName);
            _unitOfWork.SaveChanges();
            _eventTrigger.Raise(new EstablishmentChanged());
        }
コード例 #4
0
        public void Handle(UpdateMyPreference command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var preferences = _entities.Get <Preference>();

            if (command.HasPrincipal)
            {
                preferences = preferences.ByPrincipal(command.Principal);
            }
            else if (command.IsAnonymous)
            {
                preferences = preferences.ByAnonymousId(command.AnonymousId);
            }

            var preference = preferences
                             .ByCategory(command.Category)
                             .ByKey(command.Key)
                             .SingleOrDefault();

            if (preference == null)
            {
                if (command.HasPrincipal)
                {
                    preference = new Preference(_entities.Get <User>().ByPrincipal(command.Principal));
                }

                else if (command.IsAnonymous)
                {
                    preference = new Preference(command.AnonymousId);
                }

                else
                {
                    return;
                }

                preference.Category = command.Category.ToString();
                preference.Key      = command.Key.ToString();
                preference.Value    = command.Value;
                _entities.Create(preference);
            }
            else
            {
                preference.Value = command.Value;
                _entities.Update(preference);
            }
        }
コード例 #5
0
        public void Handle(CreateLanguage command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var language = new Language
            {
                TwoLetterIsoCode   = command.TwoLetterIsoCode,
                ThreeLetterIsoCode = command.ThreeLetterIsoCode,
                ThreeLetterIsoBibliographicCode = command.ThreeLetterIsoBibliographicCode,
            };

            _entities.Create(language);
            command.CreatedLanguage = language;
        }
コード例 #6
0
        public void Handle(CreateExternalUrl command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var entity = new ExternalUrl
            {
                PersonId    = command.PersonId,
                Description = command.Description,
                Value       = command.Value,
            };

            _entities.Create(entity);
            _entities.SaveChanges();
            command.Created = entity;
        }