/// <inheritdoc/>
        public async Task <Product> CreateProductAsync(UpdateProductRequest createRequest)
        {
            /*var dbProducts = await _context.Products.Where(p => p.Code == createRequest.Code).ToArrayAsync();
             * if (dbProducts.Length > 0)
             * {
             *  throw new RequestedResourceHasConflictException("code");
             * }*/

            var dbProduct = Mapper.Map <UpdateProductRequest, DbProduct>(createRequest);

            dbProduct.CreatedBy     = _userContext.UserId;
            dbProduct.LastUpdatedBy = _userContext.UserId;
            _context.Products.Add(dbProduct);

            await _context.SaveChangesAsync();

            return(Mapper.Map <Product>(dbProduct));
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public async Task <ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest)
        {
            var dbCategories = await _context.Categories.Where(c => c.Code == createRequest.Code).ToArrayAsync();

            if (dbCategories.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var dbCategory = Mapper.Map <UpdateProductCategoryRequest, DbProductCategory>(createRequest);

            dbCategory.CreatedBy     = _userContext.UserId;
            dbCategory.LastUpdatedBy = _userContext.UserId;
            _context.Categories.Add(dbCategory);

            await _context.SaveChangesAsync();

            return(Mapper.Map <ProductCategory>(dbCategory));
        }
        /// <inheritdoc/>
        public async Task <Product> CreateProductAsync(UpdateProductRequest createRequest)
        {
            var dbProducts = await _context.Products.FirstOrDefaultAsync(p => p.Code == createRequest.Code).ConfigureAwait(false);

            if (dbProducts != null)
            {
                throw new RequestedResourceHasConflictException(Resources.ProductCodeError);
            }

            var dbProduct = Mapper.Map <UpdateProductRequest, DbProduct>(createRequest);

            dbProduct.CreatedBy     = _userContext.UserId;
            dbProduct.LastUpdatedBy = _userContext.UserId;
            _context.Products.Add(dbProduct);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Mapper.Map <Product>(dbProduct));
        }
        /// <inheritdoc/>
        public async Task <ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest)
        {
            var dbCategory = await _context.Categories
                             .FirstOrDefaultAsync(c => c.Code == createRequest.Code)
                             .ConfigureAwait(false);

            if (dbCategory != null)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var category = Mapper.Map <UpdateProductCategoryRequest, DbProductCategory>(createRequest);

            category.CreatedBy     = _userContext.UserId;
            category.LastUpdatedBy = _userContext.UserId;
            _context.Categories.Add(category);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Mapper.Map <ProductCategory>(category));
        }