コード例 #1
0
        public IEnumerable <CategoryDto> GetCategories(CategoryResources resources)
        {
            if (resources.Includes != null)
            {
                foreach (var item in resources.Includes?.Split(','))
                {
                    _constants.TryGetValue(item.ToLower(), out Action action);

                    action?.Invoke();
                }
            }

            var dto = _mapper.Map <IEnumerable <CategoryDto> >(_query);

            if (resources.Id != null)
            {
                var category = dto.FirstOrDefault(x => x.Id == resources.Id);

                if (category == null)
                {
                    throw new NotFoundException();
                }

                return(new List <CategoryDto> {
                    category
                });
            }

            return(dto);
        }
コード例 #2
0
        public HttpResponseMessage GetByCategory(int id)
        {
            try
            {
                IEnumerable <IResourceItemBase> items;
                string highlightFilters;
                bool   showList;
                bool   showCounties;
                bool   showTooltips;
                var    categories = this.GetCategories();
                var    category   = this.FindInCategoryTree(categories, id);

                if (category != null)
                {
                    var results = this.Index.GetSearcher().CreateQuery("content").Field("parentID", category.Id.ToString()).Execute();
                    items = this.BuildResourceList(results).ToList();
                    var categoryNode = this.Umbraco.Content(category.Id);
                    highlightFilters = this.GetFiltersForCategory(categoryNode);
                    showList         = this.ShowListFirst(categoryNode);
                    showCounties     = this.ShowCounties(categoryNode);
                    showTooltips     = this.ShowTooltips(categoryNode);
                }
                else
                {
                    return(this.Request.CreateResponse(HttpStatusCode.BadRequest, "Category not found."));
                }

                var resources = new CategoryResources
                {
                    Markers          = items,
                    HighlightFilters = highlightFilters,
                    ShowListFirst    = showList,
                    ShowCounties     = showCounties,
                    ShowTooltips     = showTooltips
                };

                return(this.Request.CreateResponse(HttpStatusCode.OK, resources));
            }
            catch (ApiNotFoundException e)
            {
                return(this.Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
            catch (Exception e)
            {
                return(this.Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message));
            }
        }
コード例 #3
0
        public IActionResult GetCategories([FromQuery] CategoryResources resources)
        {
            try
            {
                var categories = _unitOfWork.Categories.GetCategories(resources);

                return(Ok(categories));
            }
            catch (NotFoundException)
            {
                return(NotFound());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
コード例 #4
0
        public async Task <IActionResult> CreateCategory([FromBody] CategoryResources productResources)
        {
            //  individualResourses.Id = 0;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var product = mapper.Map <CategoryResources, Category>(productResources);

            // individual.Id = 0;
            context.Add(product);
            await context.SaveChangesAsync();

            product = await context.Categories.SingleOrDefaultAsync(s => s.Id == product.Id);

            var result = mapper.Map <Category, CategoryResources>(product);


            return(Ok(result));
        }