Exemple #1
0
        public async Task <int> CreateAsync(CategoriesRequestModel model)
        {
            var category = new Category
            {
                Name = model.Name
            };

            await this.Data.AddAsync(category);

            await this.Data.SaveChangesAsync();

            return(category.Id);
        }
Exemple #2
0
        public async Task <Result> UpdateAsync(
            int id, CategoriesRequestModel model)
        {
            var category = await this.FindByIdAsync(id);

            if (category == null)
            {
                return(false);
            }

            category.Name = model.Name;

            await this.Data.SaveChangesAsync();

            return(true);
        }
        public async Task <IEnumerable <CatalogCategory> > GetCatalogCategoriesAsync(
            string language,
            int pageIndex,
            int itemsPerPage,
            string orderBy)
        {
            var categoriesRequestModel = new CategoriesRequestModel
            {
                PageIndex    = pageIndex,
                ItemsPerPage = itemsPerPage,
                OrderBy      = orderBy
            };

            var apiRequest = new ApiRequest <CategoriesRequestModel>
            {
                Language        = language,
                Data            = categoriesRequestModel,
                EndpointAddress = $"{this.settings.Value.CatalogUrl}{ApiConstants.Catalog.CategoriesApiEndpoint}"
            };

            var response = await this.apiClientService.GetAsync <ApiRequest <CategoriesRequestModel>, CategoriesRequestModel, PagedResults <IEnumerable <CatalogCategory> > >(apiRequest);

            if (response.IsSuccessStatusCode && response.Data?.Data != null && response.Data.Data.Count() > 0)
            {
                var categories = new List <CatalogCategory>();

                categories.AddRange(response.Data.Data);

                int totalPages = (int)Math.Ceiling(response.Data.Total / (double)itemsPerPage);

                for (int i = PaginationConstants.SecondPage; i <= totalPages; i++)
                {
                    apiRequest.Data.PageIndex = i;

                    var nextPagesResponse = await this.apiClientService.GetAsync <ApiRequest <CategoriesRequestModel>, CategoriesRequestModel, PagedResults <IEnumerable <CatalogCategory> > >(apiRequest);

                    if (nextPagesResponse.IsSuccessStatusCode && nextPagesResponse.Data?.Data != null && nextPagesResponse.Data.Data.Count() > 0)
                    {
                        categories.AddRange(nextPagesResponse.Data.Data);
                    }
                }

                return(categories);
            }

            return(default);