Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] CategoryDto categoryDto)
        {
            if (!categoryDto.AreaId.HasValue)
            {
                throw new NullReferenceException("Category Area Id is Null");
            }

            var categoryArea = await _categoryAreaService.ReadAsync(categoryDto.AreaId.Value);

            if (categoryArea == null)
            {
                throw new EntityNotFoundException($"Category Area with Id: {categoryDto.AreaId.Value} not found.");
            }

            var category = new Category
            {
                Name         = categoryDto.Name,
                Description  = categoryDto.Description,
                CategoryArea = categoryArea
            };
            await _categoryService.CreateAsync(category);

            categoryDto.Id       = category.Id;
            categoryDto.AreaId   = categoryArea?.Id;
            categoryDto.AreaName = categoryArea?.Name;
            return(CreatedAtRoute("GetCategory", new { id = categoryDto.Id }, categoryDto));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Get(long id)
        {
            var categoryArea = await _categoryAreaService.ReadAsync(id);

            var result = CategoryAreaDto.ConvertFrom(categoryArea);

            return(new ObjectResult(result));
        }