public async Task <IActionResult> CreateCategory([FromBody] CreateUpdateCategoryDto category)
        {
            try
            {
                if (category == null)
                {
                    _logger.LogError("Category object sent from client is null.");
                    return(BadRequest("Category object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid category object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var categoryEntity = _mapper.Map <Category>(category);
                categoryEntity.CreatedDate = DateTime.Now;
                _repository.Category.CreateCategory(categoryEntity);
                await _repository.SaveAsync();

                var createdCategory = _mapper.Map <CategoryDto>(categoryEntity);

                return(CreatedAtRoute("CategoryById", new { id = createdCategory.Id }, createdCategory));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateCategory action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
        private void OpenCreateCategoryModal()
        {
            CreateValidationsRef.ClearAll();

            NewCategory = new CreateUpdateCategoryDto();
            CreateCategoryModal.Show();
        }
        public async Task <IActionResult> UpdateCategory(int id, [FromBody] CreateUpdateCategoryDto category)
        {
            try
            {
                if (category == null)
                {
                    _logger.LogError("Category object sent from client is null.");
                    return(BadRequest("Category object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid category object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var categoryEntity = await _repository.Category.GetCategoryByIdAsync(id);

                if (categoryEntity == null)
                {
                    _logger.LogError($"Category with id: {id}, hasn't been found in db.");
                    return(NotFound());
                }

                _mapper.Map(category, categoryEntity);
                categoryEntity.UpdatedDate = DateTime.Now;

                _repository.Category.UpdateCategory(categoryEntity);
                await _repository.SaveAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateCategory action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Example #4
0
 public Task <CategoryDto> UpdateAsync(Guid id, CreateUpdateCategoryDto input)
 {
     return(_service.UpdateAsync(id, input));
 }
Example #5
0
 public Task <CategoryDto> CreateAsync(CreateUpdateCategoryDto input)
 {
     return(_service.CreateAsync(input));
 }
        public async Task OnGetAsync()
        {
            var dto = await _service.GetAsync(Id);

            Category = ObjectMapper.Map <CategoryDto, CreateUpdateCategoryDto>(dto);
        }
Example #7
0
        public async Task OnGetAsync()
        {
            var bookDto = await _bookAppService.GetAsync(Id);

            Category = ObjectMapper.Map <CategoryDto, CreateUpdateCategoryDto>(bookDto);
        }
 public CategoriesList()
 {
     NewCategory     = new CreateUpdateCategoryDto();
     EditingCategory = new CreateUpdateCategoryDto();
 }