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); } }
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; } }
public async Task Create(Product product) { var newProduct = TinyMapper.Map <Persistence.Product>(product); newProduct.Id = Guid.NewGuid(); _productsContext.Products.Add(newProduct); await _productsContext.SaveChangesAsync(); }