protected HttpContext GetContext()
        {
            HttpContext context = HttpContext.Current;

            Fail.IfNull(context, nameof(HttpContext) + "." + nameof(HttpContext.Current) + " is not available");
            return(context);
        }
Ejemplo n.º 2
0
        public static ContestOwnerQuery Create(int contestId, ClaimsPrincipal user)
        {
            Fail.IfNull(contestId);
            Fail.IfNull(user);

            return(new ContestOwnerQuery(contestId, user));
        }
Ejemplo n.º 3
0
        private User GetUserBy(string userId)
        {
            var user = this.FindUserBy(userId);

            Fail.IfNull(user, Violation.Of("There is no user with id '{0}'", userId));
            return(user !);
        }
Ejemplo n.º 4
0
        public static CreateContestCommand Create(string name, ClaimsPrincipal user)
        {
            Fail.IfNullOrEmpty(name);
            Fail.IfNull(user);

            return(new CreateContestCommand(name, user));
        }
        public void IfNullSuccess()
        {
            // ARRANGE
            object thisIsNotNull = "not null";

            // ACT
            Fail.IfNull(thisIsNotNull, "this is null and it shouldn't be");
        }
        public async Task <FileResult> Handle(OrderPaymentInvoiceQuery request, CancellationToken cancellationToken)
        {
            var order = await _unitOfWork.OrderRepository.FindAsync(request.Id);

            Fail.IfNull(order, request.Id);

            return(new FileContentResult(order.DriversInvoice.Content, "application/pdf"));
        }
        public void IfNullForNotNullableType()
        {
            // ARRANGE
            long thisIsNotNull = 123;

            // ACT
            Fail.IfNull(thisIsNotNull, "this is null and it shouldn't be");
        }
Ejemplo n.º 8
0
        public static CompilationQuery Create(string sourceCode, string input, ProgrammingLanguage language)
        {
            Fail.IfNullOrEmpty(sourceCode);
            Fail.IfNull(input);
            Fail.IfNull(language);

            return(new CompilationQuery(sourceCode, input, language));
        }
        public async Task <DriverPhotoQueryDto> Handle(DriverPhotoQuery request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.FindAsync(request.Id);

            Fail.IfNull(driver, request.Id);

            return(new DriverPhotoQueryDto(driver.PersonalInfo.Photo));
        }
Ejemplo n.º 10
0
        public static CreateSolutionCommand Create(string sourceCode, string languageCode, int taskId, string username)
        {
            Fail.IfNullOrEmpty(sourceCode);
            Fail.IfNullOrEmpty(languageCode);
            Fail.IfNull(taskId);
            Fail.IfNull(username);

            return(new CreateSolutionCommand(sourceCode, languageCode, taskId, username));
        }
        public async Task Consume(ConsumeContext <ICustomerRated> context)
        {
            var message  = context.Message;
            var customer = await _unitOfWork.CustomerRepository.GetCustomerWithRides(message.CustomerId);

            Fail.IfNull(customer, message.CustomerId);

            _customerService.RecalculateCustomerGrade(customer, message.Grade);
            await _unitOfWork.CommitAsync();
        }
        private void DisposeSessions()
        {
            Fail.IfNull(Sack, nameof(Sack) + " is null");

            // ReSharper disable once ArrangeStaticMemberQualifier
            SessionsContainer sessionsContainer = Sack.Value;

            // Sack may be empty - when no session was started
            sessionsContainer?.Dispose();
        }
Ejemplo n.º 13
0
            public Code Line(string line)
            {
                Fail.IfNull(line, nameof(line));

                if (this.text == null)
                {
                    return(new Code(line, this.language));
                }
                return(new Code(this.text + Markdown.NL + line, this.language));
            }
            public FieldInfo GetFieldInfo([NotNull] Enum @enum)
            {
                Fail.IfArgumentNull(@enum, nameof(@enum));

                string name = Enum.GetName(this.type, @enum);

                Fail.IfNull(name, Violation.Of("Enum value {0} not found in enum {1}", @enum, this.type));
                FieldInfo field = this.type.GetField(name);

                return(field.OrFail(nameof(field)));
            }
Ejemplo n.º 15
0
        public async Task <Unit> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.FindAsync(request.Id);

            Fail.IfNull(customer, request.Id);

            _customerService.UpdateCustomer(customer, request.Name, request.Surname, request.PhoneNumber, request.Email);

            _unitOfWork.Commit();
            return(Unit.Value);
        }
Ejemplo n.º 16
0
        public void IfNullWithName(object thisIsNull)
        {
            // ACT
            var exception = Assert.Throws <DesignByContractViolationException>(
                // ReSharper disable once ExpressionIsAlwaysNull
                () => Fail.IfNull(thisIsNull, nameof(thisIsNull))
                );

            // ASSERT
            Assert.That(exception.Message, Is.EqualTo("'thisIsNull' is null; and it shouldn't be;"));
        }
Ejemplo n.º 17
0
        public async Task <Unit> Handle(DeleteDriverCommand request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.FindAsync(request.Id);

            Fail.IfNull(driver, request.Id);

            _unitOfWork.Delete(driver);
            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UpdateCarCommand request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.FindAsync(request.Id);

            Fail.IfNull(driver, request.Id);

            _driverService.UpdateDriversCar(driver, request.CarModel, request.CarRegistrationPlateNumber);

            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
Ejemplo n.º 19
0
        public void IfNullWithMessage(object thisIsNull)
        {
            // ACT
            var exception = Assert.Throws <DesignByContractViolationException>(
                // ReSharper disable once ExpressionIsAlwaysNull
                // ReSharper disable once HeapView.BoxingAllocation
                () => Fail.IfNull(thisIsNull, Violation.Of("this is null and it shouldn't be {0} {1} {2} {3}", 1, "never", "maybe", "wow"))
                );

            // ASSERT
            Assert.That(exception.Message, Is.EqualTo("this is null and it shouldn't be 1 never maybe wow"));
        }
Ejemplo n.º 20
0
        public async Task <FileResult> Handle(DriverRideInvoiceQuery request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.GetDriverWithRides(request.DriverId);

            Fail.IfNull(driver, request.DriverId);

            var ride = driver.Rides.SingleOrDefault(x => x.Id == request.Id);

            Fail.IfNull(driver, request.Id);
            Fail.IfNull(ride.Invoice, ride, request.Id);

            return(new FileContentResult(ride.Invoice.Content, "application/pdf"));
        }
Ejemplo n.º 21
0
        public static T Read <T>([NotNull] this HttpContent?content, string jsonPath)
        {
            Fail.IfNull(content, nameof(content));
            JToken?json = content.ReadJson();
            var    node = json !.SelectToken(jsonPath).FailIfNull(Violation.Of($"Cannot find JSON node '{jsonPath}'"));

            if (node is JObject)
            {
                return(node.ToObject <T>());
            }

            return(node.Value <T>());
        }
        public async Task <Unit> Handle(UpdateDriversLicenseCommand request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.FindAsync(request.Id);

            Fail.IfNull(driver, request.Id);

            _driverService.UpdateDriversLicense(driver, request.DriversLicenseNumber, request.DriversLicenseDateOfIssue,
                                                request.DriversLicenseExpiryDate);

            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(AddPaymentMethodCommand request, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.GetCustomerWithPaymentMethods(request.Id);

            Fail.IfNull(customer, request.Id);

            var paymentMethod = _paymentMethodService.CreatePaymentMethod(request.Type, request.Email, request.Password,
                                                                          request.PhoneNumber, request.IsAlwaysLoggedIn, request.CardNumber, request.ExpiryDate, request.CvvCode, request.Country);

            customer.AddPaymentMethod(paymentMethod);
            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
Ejemplo n.º 24
0
        public async Task <DriverNextStopDetailsQueryDto> Handle(DriverNextStopDetailsQuery request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.GetDriverWithRides(request.DriverId);

            Fail.IfNull(driver, request.DriverId);

            var ride = driver.Rides.SingleOrDefault(x => x.Id == request.Id);

            Fail.IfNull(ride, request.Id);

            var stop = _rideService.GetNextStop(ride);

            return(new DriverNextStopDetailsQueryDto(stop.Id, _mapper.Map <AddressDto>(stop.Address)));
        }
        public void IfNullWith3Arguments()
        {
            // ARRANGE
            object thisIsNull = null;

            // ACT
            var exception = Assert.Throws <DesignByContractViolationException>(
                // ReSharper disable once ExpressionIsAlwaysNull
                () => Fail.IfNull(thisIsNull, "this is null and it shouldn't be {0} {1} {2}", 1, "never", "maybe")
                );

            // ASSERT
            Assert.That(exception.Message, Is.EqualTo("this is null and it shouldn't be 1 never maybe"));
        }
Ejemplo n.º 26
0
        private WcfOperationContextStorageExtension <T> GetWcfStorageExtension()
        {
            Fail.IfNull(OperationContext.Current, "There is no WCF " + nameof(OperationContext) + " available");

            IExtensionCollection <OperationContext> extensions = OperationContext.Current.Extensions;
            var instance = extensions.Find <WcfOperationContextStorageExtension <T> >();

            if (instance == null)
            {
                instance = new WcfOperationContextStorageExtension <T>();
                extensions.Add(instance);
            }

            return(instance);
        }
Ejemplo n.º 27
0
        public async Task <Unit> Handle(UpdateStopCommand request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.GetDriverWithRides(request.DriverId);

            Fail.IfNull(driver, request.DriverId);

            var ride = driver.Rides.SingleOrDefault(x => x.Id == request.Id);

            Fail.IfNull(ride, request.Id);

            _destinationPointService.UpdateDestinationPoint(ride, request.StopId);
            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
Ejemplo n.º 28
0
        public async Task <Unit> Handle(DeletePaymentMethodCommand request, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.GetCustomerWithPaymentMethods(request.Id);

            Fail.IfNull(customer, request.Id);

            var paymentMethod = customer.PaymentMethods.SingleOrDefault(x => x.Id == request.PaymentMethodId);

            Fail.IfNull(paymentMethod, request.PaymentMethodId);

            customer.RemovePaymentMethod(paymentMethod);
            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(UploadDriverPhotoCommand request, CancellationToken cancellationToken)
        {
            var driver = await _unitOfWork.DriverRepository.FindAsync(request.Id);

            Fail.IfNull(driver, request.Id);

            using (var memoryStream = new MemoryStream())
            {
                request.Photo.OpenReadStream().CopyTo(memoryStream);
                driver.PersonalInfo.Photo = memoryStream.ToArray();
            }

            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }
        public async Task <Unit> Handle(RequestRideCommand request, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.FindAsync(request.Id);

            Fail.IfNull(customer, request.Id);

            customer.AddDomainEvent(new RideRequested(
                                        customer.Id,
                                        _mapper.Map <CustomerDetails>(customer),
                                        _mapper.Map <Address>(request.StartPoint),
                                        _mapper.Map <Address>(request.DestinationPoint)));

            await _unitOfWork.CommitAsync();

            return(Unit.Value);
        }