Ejemplo n.º 1
0
        public ActionResult ListForCurrentUser()
        {
            ListForCurrentUserViewModel model = new ListForCurrentUserViewModel();

            model.Filter      = new ListForCurrentUserFilterViewModel();
            model.CurrentPage = 1;
            model.PageSize    = 10;
            NeedForBloodSearchFilter searchFilter = new NeedForBloodSearchFilter();

            searchFilter.CurrentPage         = model.CurrentPage.HasValue ? model.CurrentPage.Value : 1;
            searchFilter.PageSize            = model.PageSize.HasValue ? model.PageSize.Value : 10;
            searchFilter.SortOn              = model.SortOn;
            searchFilter.SortDirection       = model.SortDirection;
            searchFilter.Filter_BloodGroupId = model.Filter.Filter_BloodGroupId;
            model.Filter.Filter_IsFound      = 0;//default
            searchFilter.Filter_IsFound      = model.Filter.Filter_IsFound;
            searchFilter.Filter_HospitalId   = SessionHelper.CurrentUser.HospitalId;

            model.FilterBloodGroupSelectList = GetBloodGroupSelectList();
            model.FilterIsFoundSelectList    = GetIsFoundSelectList();

            try
            {
                model.DataList = _needForBloodService.GetAllPaginatedWithDetailBySearchFilter(searchFilter);
            }
            catch
            {
                model.DataList       = new PaginatedList <NeedForBloodWithDetail>(new List <NeedForBloodWithDetail>(), 0, searchFilter.CurrentPage, searchFilter.PageSize, searchFilter.SortOn, searchFilter.SortDirection);
                ViewBag.ErrorMessage = "Error";
            }
            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult ListForCurrentUser(ListForCurrentUserViewModel model)
        {
            // filter bilgilerinin default boş değerlerle doldurulması sağlanıyor
            if (model.Filter == null)
            {
                model.Filter = new ListForCurrentUserFilterViewModel();
            }

            if (!model.CurrentPage.HasValue)
            {
                model.CurrentPage = 1;
            }

            if (!model.PageSize.HasValue)
            {
                model.PageSize = 10;
            }

            NeedForBloodSearchFilter searchFilter = new NeedForBloodSearchFilter();

            searchFilter.CurrentPage         = model.CurrentPage.HasValue ? model.CurrentPage.Value : 1;
            searchFilter.PageSize            = model.PageSize.HasValue ? model.PageSize.Value : 10;
            searchFilter.SortOn              = model.SortOn;
            searchFilter.SortDirection       = model.SortDirection;
            searchFilter.Filter_BloodGroupId = model.Filter.Filter_BloodGroupId;
            searchFilter.Filter_IsFound      = model.Filter.Filter_IsFound;
            searchFilter.Filter_HospitalId   = SessionHelper.CurrentUser.HospitalId;
            model.FilterBloodGroupSelectList = GetBloodGroupSelectList();
            model.FilterIsFoundSelectList    = GetIsFoundSelectList();

            try
            {
                model.DataList = _needForBloodService.GetAllPaginatedWithDetailBySearchFilter(searchFilter);
            }
            catch
            {
                model.DataList       = new PaginatedList <NeedForBloodWithDetail>(new List <NeedForBloodWithDetail>(), 0, searchFilter.CurrentPage, searchFilter.PageSize, searchFilter.SortOn, searchFilter.SortDirection);
                ViewBag.ErrorMessage = "Error";
            }
            return(View(model));
        }
        public PaginatedList <NeedForBloodWithDetail> GetAllPaginatedWithDetailBySearchFilter(NeedForBloodSearchFilter searchFilter)
        {
            PaginatedList <NeedForBloodWithDetail> resultList = new PaginatedList <NeedForBloodWithDetail>(new List <NeedForBloodWithDetail>(), 0, searchFilter.CurrentPage, searchFilter.PageSize, searchFilter.SortOn, searchFilter.SortDirection);

            using (AppDBContext dbContext = new AppDBContext(_config))
            {
                var query = from nfb in dbContext.NeedForBlood
                            from h in dbContext.Hospital.Where(x => x.Id == nfb.HospitalId).DefaultIfEmpty()
                            from bg in dbContext.BloodGroup.Where(x => x.Id == nfb.BloodGroupId).DefaultIfEmpty()
                            select new NeedForBloodWithDetail()
                {
                    Id            = nfb.Id,
                    BloodGroupId  = nfb.BloodGroupId,
                    HospitalId    = nfb.HospitalId,
                    DonorName     = nfb.DonorName,
                    DonorLastName = nfb.DonorLastName,
                    DonorPhone    = nfb.DonorPhone,
                    FoundDateTime = nfb.FoundDateTime,
                    IsFound       = nfb.IsFound,

                    BloodGroup_Name = bg == null ? string.Empty : bg.Name,
                    Hospital_Name   = h == null ? string.Empty : h.Name
                };

                // filtering
                if (searchFilter.Filter_IsFound.HasValue)
                {
                    bool _isFound = searchFilter.Filter_IsFound.Value == 1 ? true : false;
                    query = query.Where(r => r.IsFound == _isFound);
                }
                if (searchFilter.Filter_BloodGroupId.HasValue)
                {
                    query = query.Where(r => r.BloodGroupId == searchFilter.Filter_BloodGroupId.Value);
                }
                if (searchFilter.Filter_HospitalId.HasValue)
                {
                    query = query.Where(r => r.HospitalId == searchFilter.Filter_HospitalId.Value);
                }
                if (!string.IsNullOrEmpty(searchFilter.Filter_HospitalName))
                {
                    query = query.Where(r => r.Hospital_Name.Contains(searchFilter.Filter_HospitalName));
                }
                // asnotracking
                query = query.AsNoTracking();

                //total count
                var totalCount = query.Count();

                //sorting
                if (!string.IsNullOrEmpty(searchFilter.SortOn))
                {
                    // using System.Linq.Dynamic.Core; nuget paketi ve namespace eklenmelidir, dynamic order by yapmak icindir
                    query = query.OrderBy(searchFilter.SortOn + " " + searchFilter.SortDirection.ToUpper());
                }
                else
                {
                    // deefault sıralama vermek gerekiyor yoksa skip metodu hata veriyor ef 6'da -- 28.10.2019 15:40
                    // https://stackoverflow.com/questions/3437178/the-method-skip-is-only-supported-for-sorted-input-in-linq-to-entities
                    query = query.OrderBy(r => r.Id);
                }

                //paging
                query      = query.Skip((searchFilter.CurrentPage - 1) * searchFilter.PageSize).Take(searchFilter.PageSize);
                resultList = new PaginatedList <NeedForBloodWithDetail>(
                    query.ToList(),
                    totalCount,
                    searchFilter.CurrentPage,
                    searchFilter.PageSize,
                    searchFilter.SortOn,
                    searchFilter.SortDirection
                    );
            }
            return(resultList);
        }