コード例 #1
0
        public void Handle(LockAccountCommand message)
        {
            var aggregate = this.Repository.GetById <AccountDomainModel>(message.Id);

            aggregate.LockAccount();

            // here we are sending the aggregate to the NEventStore
            // to serialize the current state or current delta
            this.Repository.Save(aggregate);
        }
        public async Task <ResultWithError <ErrorData> > Handle(LockAccountCommand request, CancellationToken cancellationToken)
        {
            var result = await this.Process(request, cancellationToken);

            var dbResult = await this._userRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            if (!dbResult)
            {
                return(ResultWithError.Fail(new ErrorData(
                                                ErrorCodes.SavingChanges, "Failed To Save Database")));
            }

            return(result);
        }
        private async Task <ResultWithError <ErrorData> > Process(LockAccountCommand request, CancellationToken cancellationToken)
        {
            var whenHappened = this._clock.GetCurrentInstant().ToDateTimeUtc();
            var userMaybe    = await this._userRepository.Find(request.UserId, cancellationToken);

            if (userMaybe.HasNoValue)
            {
                return(ResultWithError.Fail(new ErrorData(ErrorCodes.UserNotFound)));
            }

            var user = userMaybe.Value;

            if (user.CheckAndApplyAccountLock(whenHappened))
            {
                user.RandomizePassword(
                    this._passwordHasher.HashPassword(this._passwordGenerator.Generate()),
                    whenHappened);
                this._userRepository.Update(user);
                return(ResultWithError.Ok <ErrorData>());
            }

            return(ResultWithError.Fail(new ErrorData(ErrorCodes.AccountCannotBeLocked)));
        }