public AsNullableGenerator()
 {
     SupportedMethods = new[]
     {
         ReflectionHelper.GetMethodDefinition(() => NullableExtensions.AsNullable(0))
     };
 }
예제 #2
0
        public void TryParse_DateTime()
        {
            var value = new DateTime(2019, 11, 26);

            Assert.False(NullableExtensions.TryParse("agfrg", out DateTime? parsed));
            Assert.False(parsed.HasValue);
            Assert.True(NullableExtensions.TryParse(value.ToString(), out parsed));
            Assert.True(parsed.HasValue);
            Assert.Equal(value, parsed.Value);
        }
예제 #3
0
        public void TryParse_Enum()
        {
            var value = TheEnum.Value1;

            Assert.False(NullableExtensions.TryParse("agfrg", out TheEnum? parsed));
            Assert.False(parsed.HasValue);
            Assert.True(NullableExtensions.TryParse(value.ToString(), out parsed));
            Assert.True(parsed.HasValue);
            Assert.Equal(value, parsed.Value);
        }
예제 #4
0
        public void TryParse_long()
        {
            long value = 123;

            Assert.False(NullableExtensions.TryParse("agfrg", out long?parsed));
            Assert.False(parsed.HasValue);
            Assert.True(NullableExtensions.TryParse(value.ToString(), out parsed));
            Assert.True(parsed.HasValue);
            Assert.Equal(value, parsed.Value);
        }
예제 #5
0
        public void Where()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Where <int>(n, null));

            Assert.True(n.Where(i => i == 2).HasValue);
            Assert.False(n.Where(i => i == 3).HasValue);
            Assert.False(n0.Where(i => i == 2).HasValue);
            Assert.False(n0.Where(i => i == 3).HasValue);
        }
예제 #6
0
        public void AnyWithPredicate()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Any <int>(n, null));

            Assert.True(n.Any(i => i == 2));
            Assert.False(n.Any(i => i == 3));
            Assert.False(n0.Any(i => i == 2));
            Assert.False(n0.Any(i => i == 3));
        }
예제 #7
0
        public void Aggregate()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Aggregate <int, int>(n, 2, null));

            Assert.Equal(3, n.Aggregate(1, (sum, i) => sum + i));
            Assert.Equal(1, n0.Aggregate(1, (sum, i) => sum + i));

            // not called
            Assert.Equal(1, n0.Aggregate(1, (_, __) => throw new InvalidOperationException("Shpuld not be called")));
        }
예제 #8
0
        public void Supply()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Supply <int>(n, null));

            Assert.True(n.Supply(() => 1).HasValue);
            Assert.True(n0.Supply(() => 1).HasValue);
            Assert.Equal(2, n.Supply(() => 1).Value);
            Assert.Equal(1, n0.Supply(() => 1).Value);

            // not called
            Assert.True(n.Supply(() => throw new InvalidOperationException("Shpuld not be called")).HasValue);
        }
예제 #9
0
        public void Map()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Map <int, int>(n, null));

            Func <int, int> selector = i => i * 2;

            Assert.True(n.Map(selector).HasValue);
            Assert.Equal(4, n.Map(selector).Value);
            Assert.False(n0.Map(selector).HasValue);

            // selector not called
            Assert.False(n0.Map(new Func <int, int>(_ => throw new InvalidOperationException("Should not be called"))).HasValue);
        }
예제 #10
0
        public static void ApplyChanges <T>(this T entity, KeyValuePair <string, dynamic> change) //where T : IEntityId
        {
            var prop = entity.GetType().GetProperty(change.Key.Replace(" ", ""));

            if (prop == null)
            {
                return;
            }
            if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                prop?.SetValue(entity, NullableExtensions.ToNullable(change.Value, prop.PropertyType));
            }
            else
            {
                prop?.SetValue(entity, Convert.ChangeType(change.Value, prop.PropertyType));
            }
        }
예제 #11
0
        public async Task <IActionResult> Index(ListOptions options)
        {
            options = options ?? DefaultStockMailListOptions;

            var vm = new IndexViewModel
            {
                AllBranches        = await _branchesDao.GetAllAsync(),
                Mail               = new PaginatedList <Post>(PageSize),
                CurrentListOptions = options,
                ReturnUrl          = HttpContext.Request.PathAndQuery()
            };

            if (!bool.TryParse(options.Filters["withoutAddressOnly"], out bool withoutAddresOnly))
            {
                withoutAddresOnly = false;
            }
            NullableExtensions.TryParse(options.Filters["sourceBranchId"], out long?filterSourceBranchId);
            NullableExtensions.TryParse(options.Filters["destinationBranchId"], out long?filterDestinationBranchId);
            var filterPersonFrom = options.Filters["personFrom"];
            var filterPersonTo   = options.Filters["personTo"];
            var filterAddressTo  = options.Filters["addressTo"];

            var branch = await _currentUserService.GetBranchAsync();

            if (branch == null)
            {
                TempData.Set("message", MessageViewModel.MakeError("Setup your branch"));
                return(View(vm));
            }
            var mail = await _mailDao.GetAllForStock(
                branch : branch,
                withoutAddressOnly : withoutAddresOnly,
                filterSourceBranchId : filterSourceBranchId,
                filterDestinationBranchId : filterDestinationBranchId,
                filterPersonFrom : filterPersonFrom,
                filterPersonTo : filterPersonTo,
                filterAddressTo : filterAddressTo,
                sortKey : options.SortKey,
                sortOrder : options.SortOrder);

            vm.Mail = mail.ToPaginatedList(options.Page, PageSize);
            return(View(vm));
        }
예제 #12
0
        public void Bind()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Bind <int, int>(n, null));

            Func <int, int?> binder1 = i => i * 2;
            Func <int, int?> binder2 = _ => null;

            Assert.True(n.Bind(binder1).HasValue);
            Assert.Equal(4, n.Bind(binder1).Value);
            Assert.False(n0.Bind(binder1).HasValue);

            Assert.False(n.Bind(binder2).HasValue);
            Assert.False(n0.Bind(binder2).HasValue);

            // selector not called
            Assert.False(n0.Bind(new Func <int, int?>(_ => throw new InvalidOperationException("Should not be called"))).HasValue);
        }
예제 #13
0
        public async Task <IActionResult> Index(ListOptions options)
        {
            if (options == null)
            {
                options = DefaultListOptions;
                options.Filters["from"] = DateTime.Now.AddHours(-1).ToString("g");
            }

            NullableExtensions.TryParse(options.Filters["type"], out ActivityType? filterType);
            var filterMessage = options.Filters["message"];

            NullableExtensions.TryParse(options.Filters["from"], out DateTime? filterFrom);
            NullableExtensions.TryParse(options.Filters["to"], out DateTime? filterTo);
            var filterUser = options.Filters["user"];

            NullableExtensions.TryParse(options.Filters["postId"], out long?filterPostId);
            NullableExtensions.TryParse(options.Filters["branchId"], out long?filterBranchId);
            NullableExtensions.TryParse(options.Filters["carId"], out long?filterCarId);

            var activities = await _activitiesDao.GetAllAsync(
                filterType,
                filterMessage,
                filterFrom,
                filterTo,
                filterUser,
                filterPostId,
                filterBranchId,
                filterCarId);

            return(View(new IndexViewModel
            {
                AllActivityTypes = Activity.AllTypes,
                AllBranches = await _branchesDao.GetAllAsync(),
                AllCars = await _carsDao.GetAllAsync(),
                Activities = activities.ToPaginatedList(options.Page, PageSize),
                CurrentListOptions = options,
                ReturnUrl = HttpContext.Request.PathAndQuery()
            }));
        }
예제 #14
0
        public void Zip()
        {
            Nullable <int> n  = 2;
            Nullable <int> n0 = null;

            Assert.Throws <ArgumentNullException>(() => NullableExtensions.Zip <int, int, int>(n, n, null));

            Func <int, int, int> selector = (a, b) => a + b;
            Func <int, int, int> fail     = (a, b) => throw new InvalidOperationException("Should not be called");

            Assert.True(n.Zip(n, selector).HasValue);
            Assert.False(n0.Zip(n, selector).HasValue);
            Assert.False(n.Zip(n0, selector).HasValue);
            Assert.False(n0.Zip(n0, selector).HasValue);

            Assert.Equal(4, n.Zip(n, selector).Value);

            Assert.False(n0.Zip(n, fail).HasValue);
            Assert.False(n.Zip(n0, fail).HasValue);
            Assert.False(n0.Zip(n0, fail).HasValue);

            Assert.Equal((2, 2), n.Zip(n).Value);
        }
예제 #15
0
        public async Task <IActionResult> StockMail(ListOptions options)
        {
            options = options ?? DefaultListOptions;

            NullableExtensions.TryParse(options.Filters["sourceBranchId"], out long?filterSourceBranchId);
            NullableExtensions.TryParse(options.Filters["destinationBranchId"], out long?filterDestinationBranchId);

            var vm = new MailViewModel
            {
                AllBranches        = await _branchesDao.GetAllAsync(),
                Mail               = new PaginatedList <Post>(PageSize),
                CurrentListOptions = options,
                ReturnUrl          = HttpContext.Request.PathAndQuery()
            };

            var branch = await _currentUserService.GetBranchAsync();

            var car = await _currentUserService.GetCarAsync();

            if (branch == null || car == null)
            {
                TempData.Set("message", MessageViewModel.MakeError("Setup your branch and car"));
                return(View(vm));
            }

            var mail = await _mailDao.GetAllAsync(
                filterBranchId : branch.Id,
                filterSourceBranchId : filterSourceBranchId,
                filterDestinationBranchId : filterDestinationBranchId,
                filterState : PostState.InBranchStock,
                sortKey : options.SortKey,
                sortOrder : options.SortOrder);

            vm.Mail = mail.ToPaginatedList(options.Page, PageSize);
            return(View(vm));
        }