Esempio n. 1
0
        public async Task <CategoryDto> CreateCategory(CategoryForCreation categoryForCreation)
        {
            var categorytEntity = _mapper.Map <Category>(categoryForCreation);
            var category        = await _repositoryWrapper.Category.Create(categorytEntity);

            var categoryReturn = _mapper.Map <CategoryDto>(category);

            return(categoryReturn);
        }
Esempio n. 2
0
        public ActionResult CreateCategory(CategoryForCreation categoryDto)
        {
            var category = mapper.Map <Category>(categoryDto);

            category.Id = categoryRepository.NumberOfCategories() + 1;
            categoryRepository.Create(category.Name, category.Description);
            return(CreatedAtRoute(
                       nameof(GetCategory),
                       new { categoryId = category.Id },
                       CreateCategoryDto(category)));
        }
Esempio n. 3
0
        public ActionResult UpdateCategory(int id, [FromBody] CategoryForCreation cat)
        {
            bool result = ds.UpdateCategory(id, cat.Name, cat.Description);

            if (!result)
            {
                return(NotFound());
            }

            return(Ok(CreateCategoryDto(ds.GetCategory(id))));
        }
Esempio n. 4
0
        public ActionResult CreateCategory([FromBody] CategoryForCreation cat)
        {
            Category category = ds.CreateCategory(cat.Name, cat.Description);

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

            return(CreatedAtRoute(nameof(GetCategory), new { id = category.Id }, category));
        }
        public async Task <IActionResult> UpdateCategory(int id, CategoryForCreation cateDto)
        {
            try
            {
                var categoryToUpdate = await _categoryServices.UpdateCategory(id, cateDto);

                return(Ok(categoryToUpdate));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(new { Message = ex.Message.ToString() }));
            }
        }
Esempio n. 6
0
        public async Task <IActionResult> AddCategory(CategoryForCreation categoryForCreation)
        {
            var category = this.mapper.Map <Category>(categoryForCreation);

            this.unitOfWork.Add(category);

            if (await this.unitOfWork.CompleteAsync())
            {
                category = await this.repo.GetCategory(category.Id, includeChildren : false);

                var result = this.mapper.Map <CategoryForReturn>(category);
                return(Ok(result));
            }

            return(BadRequest("Could not create the category"));
        }
        public async Task <IActionResult> CreateCategory(CategoryForCreation cateDto)
        {
            try
            {
                var category = await _categoryServices.CreateCategory(cateDto);

                if (category == null)
                {
                    return(new BadRequestObjectResult(new { Message = "Tạo danh mục sản phẩm không thành công" }));
                }
                return(Ok(category));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(new { Message = ex.Message.ToString() }));
            }
        }
Esempio n. 8
0
        public async Task <CategoryForReturn> UpdateCategory(int cateId, CategoryForCreation cateDto)
        {
            var category = await _db.Categories
                           .Include(p => p.Products)
                           .FirstOrDefaultAsync(u => u.CategoryId == cateId);      // 1

            if (category == null)
            {
                return(null);
            }
            if (cateDto != null)
            {
                category.CategoryName = cateDto.CategoryName;

                _db.Categories.Update(category);
                await _db.SaveChangesAsync();
            }
            return(new CategoryForReturn
            {
                CategoryId = category.CategoryId,
                CategoryName = category.CategoryName
            });
        }
Esempio n. 9
0
        public async Task <CategoryForReturn> CreateCategory(CategoryForCreation cateDto)
        {
            var checkcategoryExist = await _db.Categories.Where(p => p.CategoryName == cateDto.CategoryName).FirstOrDefaultAsync();

            if (checkcategoryExist == null)
            {
                Category category = new Category
                {
                    CategoryName = cateDto.CategoryName
                };
                _db.Categories.Add(category);
                await _db.SaveChangesAsync();

                return(new CategoryForReturn
                {
                    CategoryId = category.CategoryId,
                    CategoryName = category.CategoryName
                });
            }
            else
            {
                return(null);
            }
        }
Esempio n. 10
0
        public async Task <ActionResult> CreateCategory([FromBody] CategoryForCreation categoryCreationDto)
        {
            var result = await _categoryService.CreateCategory(categoryCreationDto);

            return(CreatedAtRoute("categoriaId", new { Id = result.Codigo }, result));
        }