Esempio n. 1
0
        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)));
        }
Esempio n. 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)));
        }
Esempio n. 3
0
        public async Task<Unit> Handle(RequestModels.Delete request, CancellationToken cancellationToken)
        {
            var product = context.Products.FindAsync(request.Id);

            if (product == null)            
                throw new CustomException(HttpStatusCode.NotFound, new { description = "Product not found" });
            
            context.Remove(product);

            var res = await context.SaveChangesAsync(cancellationToken);

            if (res > 0)
                return Unit.Value;
            else
                throw new CustomException(HttpStatusCode.InternalServerError, new { description = "Transaction not completed" });
        }
Esempio n. 4
0
        public async Task <Unit> Handle(RequestModels.Create request, CancellationToken cancellationToken)
        {
            var product = new Product
            {
                Name  = request.Name,
                Price = request.Price,
                Stock = request.Stock
            };

            await context.AddAsync(product);

            var res = await context.SaveChangesAsync(cancellationToken);

            if (res > 0)
            {
                return(Unit.Value);
            }
            else
            {
                throw new CustomException(HttpStatusCode.InternalServerError, new { description = "Transaction not completed" });
            }
        }
Esempio n. 5
0
        public async Task <Unit> Handle(RequestModels.Update request, CancellationToken cancellationToken)
        {
            var product = await context.Products.FindAsync(request.Id, cancellationToken);

            if (product == null)
            {
                throw new CustomException(HttpStatusCode.NotFound, new { description = "Product not found" });
            }

            product.Name  = request.Name ?? product.Name;
            product.Price = request.Price ?? product.Price;
            product.Stock = request.Stock ?? product.Stock;

            var res = await context.SaveChangesAsync(cancellationToken);

            if (res > 0)
            {
                return(Unit.Value);
            }
            else
            {
                throw new CustomException(HttpStatusCode.InternalServerError, new { description = "Transaction not completed" });
            }
        }