Пример #1
0
        public async Task <ActionResult <CategoryResource> > UpdateCategory(int id, [FromBody] SaveCategoryResource SaveCategoryResource)
        {
            var validator        = new SaveCategoryResourceValidator();
            var validationResult = await validator.ValidateAsync(SaveCategoryResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var categoryToBeUpdated = await _categoryService.GetCategoryById(id);

            if (categoryToBeUpdated == null)
            {
                return(NotFound());
            }

            var Category = _mapper.Map <SaveCategoryResource, Category>(SaveCategoryResource);

            await _categoryService.UpdateCategory(categoryToBeUpdated, Category);

            var updatedCategory = await _categoryService.GetCategoryById(id);

            var updatedCategoryResource = _mapper.Map <Category, CategoryResource>(updatedCategory);

            return(Ok(updatedCategoryResource));
        }
Пример #2
0
        public async Task <ActionResult <CategoryResource> > CreateCategory([FromBody] SaveCategoryResource SaveCategoryResource)
        {
            var validator        = new SaveCategoryResourceValidator();
            var validationResult = await validator.ValidateAsync(SaveCategoryResource);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var categoryToCreate = _mapper.Map <SaveCategoryResource, Category>(SaveCategoryResource);

            var newCategory = await _categoryService.CreateCategory(categoryToCreate);

            var Category = await _categoryService.GetCategoryById(newCategory.Id);

            var CategoryResource = _mapper.Map <Category, CategoryResource>(Category);

            return(Ok(CategoryResource));
        }