Exemple #1
0
        public async Task <IActionResult> GetNutritionUnits(int pageSize = 20, int pageNumber = 1, string query = "")
        {
            FirebaseUser          user  = HttpContext.GetFirebaseUser();
            GetAllNutritionsQuery model = new GetAllNutritionsQuery()
            {
                query      = query,
                pageNumber = pageNumber <= 0 ? 1 : pageNumber,
                pageSize   = pageSize <= 0 ? 1 : pageSize
            };

            ResultWrapper <GetAllNutritionsOutput> result = new ResultWrapper <GetAllNutritionsOutput>();

            result = await _mediator.Send(model);

            return(Ok(result));
        }
        public async Task <ResultWrapper <GetAllNutritionsOutput> > Handle(GetAllNutritionsQuery request, CancellationToken cancellationToken)
        {
            ResultWrapper <GetAllNutritionsOutput> result = new ResultWrapper <GetAllNutritionsOutput>();

            var tDataList = await _dbContext.TNutrition
                            .Where(x =>
                                   string.IsNullOrEmpty(request.query) ||
                                   x.Code.Contains(request.query) ||
                                   x.Title.Contains(request.query) ||
                                   x.TNutritionGroup.Title.Contains(request.query)
                                   )
                            .Skip(request.pageSize * (request.pageNumber - 1))
                            .Take(request.pageSize)
                            .ToListAsync();

            int totalItems = await _dbContext.TNutrition
                             .Where(x =>
                                    string.IsNullOrEmpty(request.query) ||
                                    x.Code.Contains(request.query) ||
                                    x.Title.Contains(request.query) ||
                                    x.TNutritionGroup.Title.Contains(request.query)
                                    )
                             .CountAsync();

            var list = tDataList.Select(x => new PublicListItem()
            {
                Enabled  = x.Enabled,
                Selected = false,
                Text     = x.Title,
                Value    = x.Id.ToString(),
                Image    = string.Empty
            })
                       .ToList();

            result.Status = true;
            result.Result = new GetAllNutritionsOutput()
            {
                list       = list,
                pageSize   = request.pageSize,
                pageNumber = request.pageNumber,
                totalItems = totalItems
            };

            return(result);
        }