Exemplo n.º 1
0
        public async Task <JsonResult> LoadPagedData(DTParameters param, Guid viewModelId, ICollection <Filter> filters)
        {
            var response = new DTResult <object>
            {
                Data = new List <object>(),
                Draw = param.Draw
            };

            if (viewModelId == Guid.Empty)
            {
                return(Json(response));
            }
            var viewModel = await _pagesContext.ViewModels
                            .Include(x => x.TableModel)
                            .ThenInclude(x => x.TableFields)
                            .Include(x => x.ViewModelFields)
                            .ThenInclude(x => x.TableModelFields)
                            .Include(x => x.ViewModelFields)
                            .ThenInclude(x => x.Configurations)
                            .FirstOrDefaultAsync(x => x.Id.Equals(viewModelId));

            if (viewModel == null)
            {
                return(Json(response));
            }
            var orderConf = new Dictionary <string, EntityOrderDirection>();

            if (param.Order.Any())
            {
                foreach (var order in param.Order)
                {
                    var field = viewModel.ViewModelFields.FirstOrDefault(x => x.Order == order.Column - 1);
                    if (field != null)
                    {
                        orderConf.Add(field.TableModelFields?.Name ?? field.Name, (EntityOrderDirection)order.Dir);
                    }
                }
            }

            var page            = param.Start / param.Length + 1;
            var pageDataRequest = await _service.GetPaginatedResultAsync(viewModel.TableModel.Name, (uint)page,
                                                                         (uint)param.Length, param.Search.Value, filters, orderConf);

            if (pageDataRequest.IsSuccess)
            {
                var final = (await LoadManyToManyReferences(pageDataRequest.Result.ViewModel.Values.ToList(), viewModel)).ToList();
                response.Data            = final;
                response.RecordsTotal    = final.Count;
                response.RecordsFiltered = (int)pageDataRequest.Result.Total;
            }

            var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                DateFormatString = GearSettings.Date.DateFormat
            };

            return(Json(response, serializerSettings));
        }
        /// <summary>
        /// Get notifications with pagination
        /// </summary>
        /// <param name="page"></param>
        /// <param name="perPage"></param>
        /// <param name="onlyUnread"></param>
        /// <returns></returns>
        public virtual async Task <ResultModel <PaginatedNotificationsViewModel> > GetUserNotificationsWithPaginationAsync(uint page = 1, uint perPage = 10, bool onlyUnread = true)
        {
            var userRequest = await _userManager.GetCurrentUserAsync();

            if (!userRequest.IsSuccess)
            {
                return(userRequest.Map <PaginatedNotificationsViewModel>());
            }
            var user    = userRequest.Result;
            var filters = new List <Filter>
            {
                new Filter(nameof(SystemNotifications.UserId), user.Id)
            };

            if (onlyUnread)
            {
                filters.Add(new Filter(nameof(BaseModel.IsDeleted), false));
            }

            var sortableDirection = new Dictionary <string, EntityOrderDirection>
            {
                { nameof(SystemNotifications.Created), EntityOrderDirection.Desc }
            };

            var paginatedResult = await _dataService.GetPaginatedResultAsync <SystemNotifications>(page, perPage, null, filters, sortableDirection, false);

            if (!paginatedResult.IsSuccess)
            {
                return(paginatedResult.Map(new PaginatedNotificationsViewModel
                {
                    Page = page,
                    PerPage = perPage,
                    Total = 0
                }));
            }
            var result = new PaginatedNotificationsViewModel
            {
                PerPage       = paginatedResult.Result.PerPage,
                Page          = paginatedResult.Result.Page,
                Total         = paginatedResult.Result.Total,
                Notifications = paginatedResult.Result.ViewModel.Values
            };

            return(new SuccessResultModel <PaginatedNotificationsViewModel>(result));
        }