Ejemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <Product> UpdateProductAsync(int productId, UpdateProductRequest updateRequest)
        {
            var dbProducts = await _context.Products.Where(p => p.Code == updateRequest.Code && p.Id != productId).ToArrayAsync();

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

            dbProducts = await _context.Products.Where(p => p.Id == productId).ToArrayAsync();

            if (dbProducts.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbProduct = dbProducts[0];

            Mapper.Map(updateRequest, dbProduct);
            dbProduct.LastUpdatedBy = _userContext.UserId;

            await _context.SaveChangesAsync();

            return(Mapper.Map <Product>(dbProduct));
        }
        /// <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));
        }
        /// <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 <ProductUpdateResponseDto> UpdateProductAsync(int productId, UpdateProductRequest updateRequest)
        {
            var dbProduct = await _context.Products.FirstOrDefaultAsync(p => p.Code == updateRequest.Code && p.Id != productId).ConfigureAwait(false);

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

            dbProduct = await _context.Products.FirstOrDefaultAsync(p => p.Id == productId).ConfigureAwait(false);

            if (dbProduct == null)
            {
                throw new RequestedResourceNotFoundException(Resources.ProductNotFound);
            }

            Mapper.Map(updateRequest, dbProduct);
            dbProduct.LastUpdatedBy = _userContext.UserId;

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

            return(Mapper.Map <ProductUpdateResponseDto>(dbProduct));
        }