Ejemplo n.º 1
0
        public async Task <TMapTo> CreateProductAsync <TMapTo>(ProductCreate productCreate)
            where TMapTo : ProductBL
        {
            try
            {
                var mappedDtoModel = _mapper.Map <ProductCreate, ProductDTO>(productCreate);

                var createdEntity = await _productsContext.Products.AddAsync(mappedDtoModel);

                if (createdEntity is null)
                {
                    throw new CreateOperationException(
                              $"Can not create entity {typeof(ProductDTO)} from parameter {nameof(productCreate)}");
                }

                await _productsContext.SaveChangesAsync();

                var mappedCreatedEntity = _mapper.Map <ProductDTO, TMapTo>(createdEntity);

                return(mappedCreatedEntity);
            }
            catch (Exception ex)
            {
                throw new CreateOperationException("Some error occured while creating new product", ex);
            }
        }
Ejemplo n.º 2
0
        public async Task <DtoProduct> DeleteAsync(Guid id)
        {
            try
            {
                var product = await dbContext.Products.FirstOrDefaultAsync(x => x.Id == id);

                if (product is null)
                {
                    throw new NullReferenceException($"Not found product with id {id}");
                }

                var result = dbContext.Products.Remove(product);
                await dbContext.SaveChangesAsync();

                if (result.State != EntityState.Detached)
                {
                    throw new NotDeletedException(id);
                }

                return(mapper.Map <Product, DtoProduct>(product));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Error on {nameof(DeleteAsync)} in {nameof(ProductsService)} with id: {id}");

                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task Create(Product product)
        {
            var newProduct = TinyMapper.Map <Persistence.Product>(product);

            newProduct.Id = Guid.NewGuid();
            _productsContext.Products.Add(newProduct);
            await _productsContext.SaveChangesAsync();
        }