protected override async Task <UpdateMeterReadingResponse> Execute(
            UpdateMeterReadingRequest request)
        {
            _validator.Validate(request);

            var meterReading = await _repository.Resolve <MeterReading>()
                               .FirstOrDefaultAsync(r => r.MeterReadingId == request.MeterReadingId);

            if (meterReading == null)
            {
                throw new ValidationException(string.Format(ValidationMessages.MeterReadingNotFound, request.MeterReadingId));
            }

            var accountExists = await _repository.Resolve <Account>()
                                .AnyAsync(a => a.AccountId == request.AccountId);

            if (!accountExists)
            {
                throw new ValidationException(string.Format(ValidationMessages.AccountNotFound, request.AccountId));
            }

            meterReading.AccountId            = request.AccountId;
            meterReading.MeterReadValue       = request.MeterReadValue;
            meterReading.MeterReadingDateTime = request.MeterReadingDateTime;

            return(new UpdateMeterReadingResponse());
        }
예제 #2
0
        protected override async Task <DeleteMeterReadingResponse> Execute(
            DeleteMeterReadingRequest request)
        {
            _validator.Validate(request);

            var meterReading = await _repository.Resolve <MeterReading>().FirstOrDefaultAsync(r => r.MeterReadingId == request.MeterReaderId);

            if (meterReading != null)
            {
                _repository.Resolve <MeterReading>().Remove(meterReading);
            }

            return(new DeleteMeterReadingResponse());
        }
예제 #3
0
        protected void PerformWithRepository <TEntity>(Action <IDataSet <TEntity> > action) where TEntity : IEntity
        {
            IDataSet <TEntity> repository = _factory.Resolve <TEntity>();

            try
            {
                action(repository);
            }
            finally
            {
                if (repository != null)
                {
                    _factory.Dispose(repository);
                }
            }
        }
예제 #4
0
        protected override async Task <AddMeterReadingResponse> Execute(
            AddMeterReadingRequest request)
        {
            _validator.Validate(request);

            var accountExists = await _repository.Resolve <Account>()
                                .AnyAsync(a => a.AccountId == request.AccountId);

            if (!accountExists)
            {
                throw new ValidationException(string.Format(ValidationMessages.AccountNotFound, request.AccountId));
            }

            _repository.Resolve <MeterReading>().Add(new MeterReading
            {
                AccountId            = request.AccountId,
                MeterReadValue       = request.MeterReadValue,
                MeterReadingDateTime = request.MeterReadingDateTime
            });

            return(new AddMeterReadingResponse());
        }
        protected override async Task <AccountsResponse> QueryAsync(AccountsRequest request)
        {
            var accounts = _repository.Resolve <Account>()
                           .AsQueryable().Select(a => new AccountResponse
            {
                AccountId = a.AccountId,
                FirstName = a.FirstName,
                LastName  = a.LastName
            }).ToList();

            return(new AccountsResponse {
                Accounts = accounts
            });
        }
        protected override async Task <AccountResponse> QueryAsync(AccountRequest request)
        {
            _validator.Validate(request);
            var account = await _repository.Resolve <Account>()
                          .FirstOrDefaultAsync(a => a.AccountId == request.AccountId);

            if (account == null)
            {
                throw new ValidationException($"Account with accountId {request.AccountId} not found!");
            }

            return(new AccountResponse
            {
                AccountId = account.AccountId, FirstName = account.FirstName, LastName = account.LastName
            });
        }
        protected override async Task <MeterReadingsResponse> QueryAsync(MeterReadingsRequest request)
        {
            _validator.Validate(request);

            var meterReadings = _repository.Resolve <MeterReading>()
                                .Where(a => a.AccountId == request.AccountId).Select(s => new MeterReadingResponse
            {
                MeterReadingId       = s.MeterReadingId,
                AccountId            = s.AccountId,
                MeterReadValue       = s.MeterReadValue,
                MeterReadingDateTime = s.MeterReadingDateTime
            }).ToList();

            return(new MeterReadingsResponse
            {
                MeterReadings = meterReadings
            });
        }
예제 #8
0
        protected override async Task <MeterReadingResponse> QueryAsync(MeterReadingRequest request)
        {
            _validator.Validate(request);
            var meterReading = await _repository.Resolve <MeterReading>()
                               .FirstOrDefaultAsync(a => a.MeterReadingId == request.MeterReadingId);

            if (meterReading == null)
            {
                throw new ValidationException($"Meter reading with meterReadingId {request.MeterReadingId} not found!");
            }

            return(new MeterReadingResponse
            {
                MeterReadingId = meterReading.MeterReadingId,
                AccountId = meterReading.AccountId,
                MeterReadValue = meterReading.MeterReadValue,
                MeterReadingDateTime = meterReading.MeterReadingDateTime
            });
        }
예제 #9
0
        protected override async Task <BulkUploadAccountsResponse> Execute(
            BulkUploadAccountsRequest request)
        {
            _validator.Validate(request);
            request.UploadFile.Position = 0;

            var accountLines = ReadLines(request.UploadFile).Skip(1).Select(FromCsv).ToList();

            var bulkUploadAccountsResponse = new BulkUploadAccountsResponse
            {
                UploadErrors = new List <string>()
            };

            foreach (var accountLine in accountLines)
            {
                var validAccountId = int.TryParse(accountLine.AccountId, out var accountId);
                if (!validAccountId)
                {
                    bulkUploadAccountsResponse.UploadErrors.Add(string.Format(ValidationMessages.NotANumber,
                                                                              accountLine.AccountId, nameof(accountLine.AccountId)));
                    bulkUploadAccountsResponse.FailedUploads++;
                    continue;
                }

                var validFirstName = !string.IsNullOrEmpty(accountLine.FirstName);
                if (!validFirstName)
                {
                    bulkUploadAccountsResponse.UploadErrors.Add(string.Format(ValidationMessages.ValidValueCustom,
                                                                              accountLine.FirstName, nameof(accountLine.FirstName)));
                    bulkUploadAccountsResponse.FailedUploads++;
                    continue;
                }

                var validLastName = !string.IsNullOrEmpty(accountLine.LastName);
                if (!validLastName)
                {
                    bulkUploadAccountsResponse.UploadErrors.Add(string.Format(ValidationMessages.ValidValueCustom,
                                                                              accountLine.LastName, nameof(accountLine.LastName)));
                    bulkUploadAccountsResponse.FailedUploads++;
                    continue;
                }

                var doubleEntry = await _repository.Resolve <Account>().AnyAsync(r =>
                                                                                 r.AccountId == accountId);

                if (doubleEntry)
                {
                    bulkUploadAccountsResponse.UploadErrors.Add(string.Format(ValidationMessages.DoubleEntry,
                                                                              accountId, accountLine.FirstName, accountLine.LastName));
                    bulkUploadAccountsResponse.FailedUploads++;
                    continue;
                }

                _repository.Resolve <Account>().Add(new Account
                {
                    AccountId = accountId,
                    FirstName = accountLine.FirstName,
                    LastName  = accountLine.LastName
                });

                bulkUploadAccountsResponse.SuccesfulUploads++;
            }

            return(bulkUploadAccountsResponse);
        }
        protected override async Task <BulkUploadMeterReadingsResponse> Execute(
            BulkUploadMeterReadingsRequest request)
        {
            _validator.Validate(request);
            request.UploadFile.Position = 0;

            var meterReadingLines = ReadLines(request.UploadFile).Skip(1).Select(FromCsv).ToList();

            var bulkUploadMeterReadingsResponse = new BulkUploadMeterReadingsResponse
            {
                UploadErrors = new List <string>()
            };

            foreach (var meterReadingLine in meterReadingLines)
            {
                var validAccountId = int.TryParse(meterReadingLine.AccountId, out var accountId);
                if (!validAccountId)
                {
                    bulkUploadMeterReadingsResponse.UploadErrors.Add(string.Format(ValidationMessages.NotANumber,
                                                                                   meterReadingLine.AccountId, nameof(meterReadingLine.AccountId)));
                    bulkUploadMeterReadingsResponse.FailedUploads++;
                    continue;
                }

                var validMeterReadingDateTime =
                    DateTime.TryParse(meterReadingLine.MeterReadingDateTime, out var meterReadingDateTime);
                if (!validMeterReadingDateTime)
                {
                    bulkUploadMeterReadingsResponse.UploadErrors.Add(string.Format(ValidationMessages.InvalidDateTime,
                                                                                   meterReadingLine.MeterReadingDateTime, nameof(meterReadingLine.MeterReadingDateTime)));
                    bulkUploadMeterReadingsResponse.FailedUploads++;
                    continue;
                }

                var validMeterReadValue = int.TryParse(meterReadingLine.MeterReadValue, out var meterReadValue) &&
                                          meterReadValue <= 99999;
                if (!validMeterReadValue)
                {
                    bulkUploadMeterReadingsResponse.UploadErrors.Add(string.Format(ValidationMessages.NotANumber,
                                                                                   meterReadingLine.MeterReadValue, nameof(meterReadingLine.MeterReadValue)));
                    bulkUploadMeterReadingsResponse.FailedUploads++;
                    continue;
                }

                var accountExists = await _repository.Resolve <Account>()
                                    .AnyAsync(a => a.AccountId == accountId);

                if (!accountExists)
                {
                    bulkUploadMeterReadingsResponse.UploadErrors.Add(string.Format(ValidationMessages.AccountNotFound,
                                                                                   meterReadingLine.AccountId));
                    bulkUploadMeterReadingsResponse.FailedUploads++;
                    continue;
                }

                var doubleEntry = await _repository.Resolve <MeterReading>().AnyAsync(r =>
                                                                                      r.AccountId == accountId && r.MeterReadValue == meterReadValue &&
                                                                                      r.MeterReadingDateTime == meterReadingDateTime);

                if (doubleEntry)
                {
                    bulkUploadMeterReadingsResponse.UploadErrors.Add(string.Format(ValidationMessages.DoubleEntry,
                                                                                   accountId, meterReadingDateTime.ToString("dd/MM/yyyy HH:mm"), meterReadValue.ToString("D5")));
                    bulkUploadMeterReadingsResponse.FailedUploads++;
                    continue;
                }

                _repository.Resolve <MeterReading>().Add(new MeterReading
                {
                    AccountId            = accountId,
                    MeterReadValue       = meterReadValue,
                    MeterReadingDateTime = meterReadingDateTime
                });

                bulkUploadMeterReadingsResponse.SuccesfulUploads++;
            }

            return(bulkUploadMeterReadingsResponse);
        }