Ejemplo n.º 1
0
        protected override bool TryDoAction(ILog log, IEnumerable <ICommandActionListener> listeners = null)
        {
            CreateCommandResult <Account> result = new CreateCommandResult <Account>(this, false, null);

            if (Repositories.AccountRepository.DoesNameExist(AccountName.GetValue(String.Empty)))
            {
                TransmitResult(result, listeners);
                return(false);
            }
            else if (CategoryNameOption.IsDataValid &&
                     !Repositories.AccountRepository.DoesNameExist(CategoryNameOption.GetValue(String.Empty)))
            {
                //invalid category name
                TransmitResult(result, listeners);
                return(false);
            }

            AccountDto accountDto = BuildAccountDto();

            bool successful = Repositories.AccountRepository.Upsert(accountDto);

            AccountId.SetData(accountDto.Id.Value);

            if (!accountDto.Id.HasValue)
            {
                log?.WriteLine("Error occurred while adding account. Account was not assigned a valid Id.", LogLevel.Error);
                TransmitResult(result, listeners);
                return(false);
            }

            AccountStateDto accountStateDto = BuildAccountStateDto(accountDto.Id.Value);

            successful &= Repositories.AccountStateRepository.Upsert(accountStateDto);

            if (successful)
            {
                log?.WriteLine($"Added account \"{accountDto.Name}\"", LogLevel.Normal);

                result = new CreateCommandResult <Account>(this, successful, DtoToModelTranslator.FromDto(accountDto, DateTime.Today, Repositories));
            }

            TransmitResult(result, listeners);
            return(successful);
        }