public async Task <ActionResult <CategoryViewModel> > Create(CategoryDto categoryDto)
        {
            var category = _mapper.Map <Category>(categoryDto);

            category.CreatedAt  = DateTimeOffset.Now;
            category.ModifiedAt = DateTimeOffset.Now;
            _dbContext.Add(category);
            await _dbContext.SaveChangesAsync();

            return(Ok(_mapper.Map <CategoryViewModel>(category)));
        }
Exemple #2
0
        public async Task <ActionResult <ProductViewModel> > Create(ProductDto product)
        {
            var productEntity = _mapper.Map <Product>(product);

            productEntity.CreatedAt  = DateTimeOffset.Now;
            productEntity.ModifiedAt = DateTimeOffset.Now;

            var productCategory
                = await _dbContext
                  .Categories
                  .FirstOrDefaultAsync(c => c.Id == productEntity.CategoryId);

            if (productCategory != null)
            {
                productEntity.Category = productCategory;
            }

            _dbContext.Add(productEntity);
            await _dbContext.SaveChangesAsync();

            return(Ok(_mapper.Map <ProductViewModel>(productEntity)));
        }