public async Task <int> Handle(CreateDeviceCommand request, CancellationToken cancellationToken)
        {
            var code = StringCleaner.CleanInput(request.Code).Trim();
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(code) || String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Code);
            }

            var lat = (request.Latitude != null) ? StringCleaner.CleanInput(request.Latitude).Trim() : null;
            var lng = (request.Longitude != null) ? StringCleaner.CleanInput(request.Longitude).Trim() : null;

            //Map request to entity
            var entity = new Device
            {
                Code      = code,
                Name      = name,
                Zone      = request.Zone,
                Latitude  = (String.IsNullOrEmpty(lat)) ? null : lat,
                Longitude = (String.IsNullOrEmpty(lng)) ? null : lng
            };

            _context.Devices.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
        public async Task <Unit> Handle(UpdateDeviceCommand request, CancellationToken cancellationToken)
        {
            var code = StringCleaner.CleanInput(request.Code).Trim();
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(code) || String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Id);
            }
            // Retrieve the entity
            var entity = await _context.Devices.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Device), request.Id);
            }

            var lat = (entity.Latitude != null) ? StringCleaner.CleanInput(request.Latitude).Trim() : null;
            var lng = (entity.Longitude != null) ? StringCleaner.CleanInput(request.Longitude).Trim() : null;

            // Update the entity
            entity.Code      = code;
            entity.Name      = name;
            entity.Zone      = request.Zone;
            entity.Latitude  = (String.IsNullOrEmpty(lat)) ? null : lat;
            entity.Longitude = (String.IsNullOrEmpty(lng)) ? null : lng;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #3
0
        public async Task <Unit> Handle(UpdateCardCommand request, CancellationToken cancellationToken)
        {
            var number = StringCleaner.CleanInput(request.Number).Trim();

            if (String.IsNullOrEmpty(number))
            {
                throw new NotFoundException("User input is bad.", request.Id);
            }
            // Retrieve the entity
            var entity = await _context.Cards.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Card), request.Id);
            }

            var supplierEntity = await _context.Suppliers.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.SupplierId);

            if (supplierEntity == null)
            {
                throw new NotFoundException(nameof(Product), request.ProductId);
            }

            Pass passEntity = null;

            if (request.PassId != null)
            {
                passEntity = await _context.Passes.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.PassId);

                if (passEntity == null)
                {
                    throw new NotFoundException(nameof(Pass), request.PassId);
                }
            }

            Product productEntity = null;

            if (request.ProductId != null)
            {
                productEntity = await _context.Products.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.ProductId);

                if (productEntity == null)
                {
                    throw new NotFoundException(nameof(Product), request.ProductId);
                }
            }

            var alias = (request.Alias != null) ? StringCleaner.CleanInput(request.Alias).Trim() : null;

            // Update the entity
            entity.Number     = number;
            entity.Alias      = (String.IsNullOrEmpty(request.Alias)) ? null : alias;
            entity.SupplierId = request.SupplierId;
            entity.ProductId  = request.ProductId ?? null;
            entity.PassId     = request.PassId ?? null;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateStageCommand request, CancellationToken cancellationToken)
        {
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Name);
            }

            // Retrieve the entity
            var entity = await _context.Stages.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Stage), request.Id);
            }

            if (request.IsCurrent && entity.IsCurrent != true)
            {
                var current = await _context.Stages.FirstOrDefaultAsync(x => x.IsCurrent == true);

                if (current != null)
                {
                    current.IsCurrent = false;
                }
            }

            // Update the entity
            entity.Name      = name;
            entity.IsCurrent = request.IsCurrent;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateTestCommand request, CancellationToken cancellationToken)
        {
            // Retrieve the entity
            var entity = await _context.Tests.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Test), request.Id);
            }

            var jiraNumber = StringCleaner.CleanInput(request.JiraTestNumber).Trim();

            if (String.IsNullOrEmpty(jiraNumber))
            {
                throw new NotFoundException("User input is bad.", request.Id);
            }

            var existingTest = await _context.Tests.FirstOrDefaultAsync(x => x.JiraTestNumber == jiraNumber);

            if (existingTest == null)
            {
                entity.JiraTestNumber = jiraNumber;
            }
            else
            {
                // TODO Fix the return statements
                throw new NotFoundException(nameof(Test), request.Id);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #6
0
        public CreateDeviceCommandValidator(IApplicationDbContext context)
        {
            _context = context;

            RuleFor(d => d.Code)
            .Transform(d => StringCleaner.CleanInput(d))
            .NotEmpty().WithMessage("Code is required.")
            .MaximumLength(32).WithMessage("Code must not exceed 32 characters.")
            .MustAsync(BeUniqueCode).WithMessage("The specified Code already exists.")
            .Must(NotContainBadCharactersCode).WithMessage("Code can only contain a-zA-Z0-9_.$@-");
            RuleFor(d => d.Name)
            .Transform(d => StringCleaner.CleanInput(d))
            .NotEmpty().WithMessage("Name is required.")
            .MaximumLength(64).WithMessage("Code must not exceed 64 characters.")
            .Must(NotContainBadCharactersName).WithMessage("Name can only contain a-zA-Z0-9_.$@-");
            RuleFor(d => d.Zone)
            .NotEmpty().WithMessage("Zone is required.")
            .ExclusiveBetween(0, 100).WithMessage("Zone must be between 0 and 100.");
            RuleFor(d => d.Latitude)
            .Transform(d => StringCleaner.CleanInput(d))
            .MaximumLength(16).WithMessage("Latitude must not exceed 16 characters")
            .Must(NotContainBadCharactersLat).WithMessage("Latitude can only contain a-zA-Z0-9_.$@-");
            RuleFor(d => d.Longitude)
            .Transform(d => StringCleaner.CleanInput(d))
            .MaximumLength(16).WithMessage("Longitude must not exceed 16 characters")
            .Must(NotContainBadCharactersLng).WithMessage("Longitude can only contain a-zA-Z0-9_.$@-");
        }
        public async Task <Unit> Handle(UpdatePassCommand request, CancellationToken cancellationToken)
        {
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Id);
            }
            // Retrieve the entity
            var entity = await _context.Passes.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Pass), request.Id);
            }

            var tapsToUpdate = await _context.Taps.Where(x => x.Pass == entity.Name).ToListAsync();

            foreach (var tap in tapsToUpdate)
            {
                tap.Pass = name;
            }

            // Update the entity
            entity.Name = name;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #8
0
        public CreateCardCommandValidator(IApplicationDbContext context)
        {
            _context = context;

            RuleFor(c => c.Number)
            .Transform(c => StringCleaner.CleanInput(c))
            .NotEmpty().WithMessage("Number is required.")
            .MaximumLength(32).WithMessage("Number must not exceed 32 characters.")
            .MustAsync(BeUniqueNumber).WithMessage("The specified Number already exists.")
            .Must(NotContainBadCharactersNumber).WithMessage("Number can only contain a-zA-Z0-9_.$@-");
            //When(c => c?.Alias != null, () =>
            //{
            //    RuleFor(c => c.Alias)
            //    .Transform(c => StringCleaner.CleanInput(c))
            //    .MaximumLength(32).WithMessage("Alias must not exceed 32 characters.")
            //    .MustAsync(BeUniqueAlias).WithMessage("The specified Alias already exists.")
            //    .Must(NotContainBadCharactersAlias).WithMessage("Can only contain a-zA-Z0-9_.$@-");
            //});
            RuleFor(c => c.Alias)
            .Transform(c => StringCleaner.CleanInput(c))
            .MaximumLength(32).WithMessage("Alias must not exceed 32 characters.")
            .MustAsync(BeUniqueAlias).WithMessage("The specified Alias already exists.")
            .Must(NotContainBadCharactersAlias).WithMessage("Alias can only contain a-zA-Z0-9_.$@-");
            RuleFor(c => c.SupplierId)
            .NotEmpty().WithMessage("Supplier must not be null.")
            .MustAsync(SupplierExist).WithMessage("Supplier does not exist.");
            RuleFor(c => c.PassId)
            .MustAsync(PassExist).WithMessage("Pass does not exist.").When(c => c.PassId != null);
            RuleFor(c => c.ProductId)
            .MustAsync(ProductExist).WithMessage("Product does not exist.").When(c => c.ProductId != null);
        }
Exemple #9
0
        public async Task <int> Handle(CreateStageCommand request, CancellationToken cancellationToken)
        {
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Name);
            }

            //Map request to entity
            var entity = new Stage
            {
                Name      = name,
                IsCurrent = request.IsCurrent
            };

            if (request.IsCurrent)
            {
                var current = await _context.Stages.FirstOrDefaultAsync(x => x.IsCurrent == true);

                if (current != null)
                {
                    current.IsCurrent = false;
                }
            }

            _context.Stages.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
        public async Task <int> Handle(CreateCardCommand request, CancellationToken cancellationToken)
        {
            //var number2 = StringCleaner.HasBadCharacters(request.Number);
            var number = StringCleaner.CleanInput(request.Number).Trim();

            if (String.IsNullOrEmpty(number))
            {
                throw new NotFoundException("User input is bad.", request.Number);
            }

            var supplierEntity = await _context.Suppliers.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.SupplierId);

            if (supplierEntity == null)
            {
                throw new NotFoundException(nameof(Product), request.ProductId);
            }

            Pass passEntity = null;

            if (request.PassId != null)
            {
                passEntity = await _context.Passes.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.PassId);

                if (passEntity == null)
                {
                    throw new NotFoundException(nameof(Pass), request.PassId);
                }
            }

            Product productEntity = null;

            if (request.ProductId != null)
            {
                productEntity = await _context.Products.AsNoTracking().FirstOrDefaultAsync(s => s.Id == request.ProductId);

                if (productEntity == null)
                {
                    throw new NotFoundException(nameof(Product), request.ProductId);
                }
            }

            var alias = (request.Alias != null) ? StringCleaner.CleanInput(request.Alias).Trim() : null;

            //Map request to entity
            var entity = new Card
            {
                Number     = number,
                Alias      = String.IsNullOrEmpty(alias) ? null : alias,
                SupplierId = request.SupplierId,
                PassId     = request.PassId ?? null,
                ProductId  = request.ProductId ?? null
            };

            _context.Cards.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
        public CreateTapCommandValidator(IApplicationDbContext context)
        {
            _context = context;

            RuleFor(t => t.TestExecutionId)
            .NotEmpty().WithMessage("Test Execution must not be null.")
            .MustAsync(ExecutionExist).WithMessage("Test Execution does not exist.");

            RuleFor(t => t.CardId)
            .NotEmpty().WithMessage("Card must not be null.")
            .MustAsync(CardExist).WithMessage("Card does not exist.");

            RuleFor(t => t.DeviceId)
            .NotEmpty().WithMessage("Device must not be null.")
            .MustAsync(DeviceExist).WithMessage("Device does not exist.");

            RuleFor(t => t.Tester)
            .NotEmpty().WithMessage("Tester must not be null.")
            .MaximumLength(64).WithMessage("Tester must not exceed 64 characters.");

            RuleFor(t => t.CaseNumber)
            .Transform(t => StringCleaner.CleanInput(t))
            .MaximumLength(16).WithMessage("CaseNumber must not exceed 16 characters.")
            .Must(NotContainBadCharactersCase).WithMessage("Case Number can only contain a-zA-Z0-9_.$@-");

            RuleFor(t => t.Result)
            .IsInEnum();

            RuleFor(t => t.WasResultExpected)
            .IsInEnum();

            RuleFor(t => t.TimeOf)
            .NotEmpty().WithMessage("Time must not be null.");
            // TODO: Do better on validation of time

            RuleFor(t => t.Fare)
            .ScalePrecision(2, 5);

            RuleFor(t => t.BalanceBefore)
            .ScalePrecision(2, 5);

            RuleFor(t => t.BalanceAfter)
            .ScalePrecision(2, 5);

            RuleFor(t => t.Notes)
            .Transform(t => StringCleaner.CleanInput(t))
            .MaximumLength(256).WithMessage("Notes must not exceed 256 characters.")
            .Must(NotContainBadCharactersNotes).WithMessage("Notes can only contain a-zA-Z0-9_.$@-");

            RuleFor(t => t.Action)
            .IsInEnum();

            RuleFor(t => t.PassId)
            .MustAsync(PassExist).WithMessage("Pass does not exist.").When(c => c.PassId != null);

            RuleFor(t => t.ProductId)
            .MustAsync(ProductExist).WithMessage("Product does not exist.").When(c => c.ProductId != null);
        }
        public UpdatePassCommandValidator(IApplicationDbContext context)
        {
            _context = context;

            RuleFor(c => c.Name)
            .Transform(c => StringCleaner.CleanInput(c))
            .NotEmpty().WithMessage("Name is required.")
            .MaximumLength(32).WithMessage("Name must not exceed 32 characters.")
            .MustAsync(BeUniqueName).WithMessage("The specified Name already exists.")
            .Must(NotContainBadCharactersName).WithMessage("Name can only contain a-zA-Z0-9_.$@-");
        }
        public CreateTestCommandValidator(IApplicationDbContext context)
        {
            _context = context;

            RuleFor(t => t.JiraTestNumber)
            .Transform(t => StringCleaner.CleanInput(t))
            .NotEmpty().WithMessage("Jira Test Number is required.")
            .MaximumLength(16).WithMessage("Jira Test Number must not exceed 16 characters.")
            .Must(NotContainBadCharactersJira).WithMessage("Jira Test Number can only contain a-zA-Z0-9_.$@-");
            RuleFor(v => v.StageId)
            .NotEmpty().WithMessage("Stage must not be null.")
            .MustAsync(StageExist).WithMessage("Stage does not exist.");
        }
Exemple #14
0
        public async Task <int> Handle(CreateTestCommand request, CancellationToken cancellationToken)
        {
            var stage = await _context.Stages.FindAsync(request.StageId);

            if (stage == null)
            {
                throw new NotFoundException(nameof(Stage), request.StageId);
            }

            var newEntity = new StageTest
            {
                Stage = stage
            };

            var jiraNumber = StringCleaner.CleanInput(request.JiraTestNumber).Trim();

            if (string.IsNullOrEmpty(jiraNumber))
            {
                throw new NotFoundException("User input is bad.", request.JiraTestNumber);
            }

            var existingTest = await _context.Tests.Include(t => t.StageTests).Where(x => x.JiraTestNumber == jiraNumber).FirstOrDefaultAsync();

            if (existingTest != null)
            {
                if (existingTest.StageTests.Where(x => x.StageId == request.StageId).FirstOrDefault() != null)
                {
                    // Test already exists for the requested stage
                    return(-1);
                }

                // Add the existing test to the requested stage
                newEntity.Test = existingTest;
            }
            else
            {
                // Test doesnt exist so add it to the DB
                newEntity.Test = new Test {
                    JiraTestNumber = jiraNumber
                };
            }

            _context.StageTests.Add(newEntity);
            await _context.SaveChangesAsync(cancellationToken);

            return(newEntity.TestId);
        }
Exemple #15
0

        
        public async Task <Unit> Handle(UpdateSupplierCommand request, CancellationToken cancellationToken)
        {
            var name = StringCleaner.CleanInput(request.Name).Trim();

            if (String.IsNullOrEmpty(name))
            {
                throw new NotFoundException("User input is bad.", request.Name);
            }

            // Retrieve the entity
            var entity = await _context.Suppliers.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Supplier), request.Id);
            }

            // Update the entity
            entity.Name = name;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #17
0
        public async Task <Unit> Handle(UpdateTapCommand request, CancellationToken cancellationToken)
        {
            // Retrieve the entity
            var entity = await _context.Taps.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Tap), request.Id);
            }

            var testExecution = await _context.TestExecutions.FindAsync(request.TestExecutionId);

            if (testExecution == null)
            {
                throw new NotFoundException(nameof(TestExecution), request.TestExecutionId);
            }
            var card = await _context.Cards.FindAsync(request.CardId);

            if (card == null)
            {
                throw new NotFoundException(nameof(Card), request.CardId);
            }
            var device = await _context.Devices.FindAsync(request.DeviceId);

            if (device == null)
            {
                throw new NotFoundException(nameof(Device), request.DeviceId);
            }
            var pass = await _context.Passes.FindAsync(request.PassId);

            var product = await _context.Products.FindAsync(request.ProductId);


            var styles      = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
            var parseWorked = DateTime.TryParse(request.TimeOf, System.Globalization.CultureInfo.InvariantCulture, styles, out DateTime dateTime);

            if (!parseWorked)
            {
                // TODO Fix
                throw new NotFoundException("DateTime", request.TimeOf);
            }

            var userNotes = (request.Notes != null) ? StringCleaner.CleanInput(request.Notes).Trim() : null;

            // Update the entity
            entity.TestExecutionId = request.TestExecutionId;
            entity.CardId          = request.CardId;
            entity.DeviceId        = request.DeviceId;
            //entity.Tester = request.Tester;
            entity.CaseNumber        = request.CaseNumber;
            entity.Result            = request.Result;
            entity.WasResultExpected = request.WasResultExpected;
            entity.TimeOf            = dateTime;
            entity.Fare          = request?.Fare;
            entity.BalanceBefore = request?.BalanceBefore;
            entity.BalanceAfter  = request?.BalanceAfter;
            entity.Notes         = (String.IsNullOrEmpty(userNotes)) ? null : userNotes;
            entity.Action        = request.Action;
            entity.Product       = product?.Name;
            entity.Pass          = pass?.Name;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <int> Handle(CreateTapCommand request, CancellationToken cancellationToken)
        {
            //Map request to entity
            var testExecution = await _context.TestExecutions.FindAsync(request.TestExecutionId);

            if (testExecution == null)
            {
                throw new NotFoundException(nameof(TestExecution), request.TestExecutionId);
            }
            var card = await _context.Cards.FindAsync(request.CardId);

            if (card == null)
            {
                throw new NotFoundException(nameof(Card), request.CardId);
            }
            var device = await _context.Devices.FindAsync(request.DeviceId);

            if (device == null)
            {
                throw new NotFoundException(nameof(Device), request.DeviceId);
            }
            var pass = await _context.Passes.FindAsync(request.PassId);

            //if (pass == null)
            //{
            //    throw new NotFoundException(nameof(Pass), request.PassId);
            //}
            var product = await _context.Products.FindAsync(request.ProductId);

            //if (product == null)
            //{
            //    throw new NotFoundException(nameof(Product), request.ProductId);
            //}


            var styles      = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal;
            var parseWorked = DateTime.TryParse(request.TimeOf, System.Globalization.CultureInfo.InvariantCulture, styles, out DateTime dateTime);

            if (!parseWorked)
            {
                // TODO Fix
                throw new NotFoundException("DateTime", request.TimeOf);
            }
            var userId = _currentUserService.UserId;
            var user   = await _identityService.GetUserNameAsync(userId);

            var userNotes = (request.Notes != null) ? StringCleaner.CleanInput(request.Notes).Trim() : null;

            var entity = new Tap
            {
                TestExecutionId   = testExecution.Id,
                CardId            = card.Id,
                DeviceId          = device.Id,
                Tester            = user,
                CaseNumber        = request.CaseNumber,
                Result            = request.Result,
                WasResultExpected = request.WasResultExpected,
                TimeOf            = dateTime,
                Fare          = request?.Fare,
                BalanceBefore = request?.BalanceBefore,
                BalanceAfter  = request?.BalanceAfter,
                Notes         = (String.IsNullOrEmpty(userNotes)) ? null : userNotes,
                Action        = request.Action,
                Product       = product?.Name,
                Pass          = pass?.Name,
            };

            _context.Taps.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }