示例#1
0
        public Product CreateProduct(
            CreateProductOptions options) {

            if (options == null)
            {
                return null;
            }

            var product = new Product() { 
                ProductId = options.ProductId,
                Description = options.Description,
                Name = options.Name,
                Category = options.Category,
                Price = options.Price                
            };

            context.Add(product);

            if (context.SaveChanges() > 0)
            {
                return product;
            }

            return null;
        }
示例#2
0
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            var product = new Product()
            {
                ProductId   = options.ProductId,
                Name        = options.Name,
                Description = options.Description,
                Category    = options.ProductCategory.Value,
                Price       = options.Price,
            };

            context_.Add(product);

            if (context_.SaveChanges() > 0)
            {
                return(product);
            }

            return(null);
        }
示例#3
0
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (SearchProduct(new ProductSearchOptions()
            {
                ProductId = options.ProductId
            }).Any())
            {
                return(null);
            }

            var product = new Product()
            {
                ProductId   = options.ProductId,
                Name        = options.Name,
                Description = options.Description,
                Category    = options.Category,
                Price       = options.Price
            };

            dbContext.Add(product);

            if (dbContext.SaveChanges() > 0)
            {
                return(product);
            }

            return(null);
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("Id,Code,Name,Description,Price,Quantity")] ProductDto product)
        {
            if (ModelState.IsValid)
            {
                await _productService.CreateProductAsync(CreateProductOptions.MapFromProductDto(product));

                return(RedirectToAction(nameof(Index)));
            }

            return(View(product));
        }
示例#5
0
        public Product CreateProduct(CreateProductOptions opt)
        {
            var product = new Product()
            {
                Name     = opt.Name,
                Price    = opt.Price,
                Category = opt.Category,
            };

            db_.Add(product);
            db_.SaveChanges();
            return(product);
        }
        public async Task <Result <ProductDto> > CreateProductAsync(CreateProductOptions options)
        {
            if (options == null)
            {
                return(new Result <ProductDto>(ErrorCode.BadRequest, "Null options."));
            }

            if (string.IsNullOrWhiteSpace(options.Code) ||
                string.IsNullOrWhiteSpace(options.Description) ||
                string.IsNullOrWhiteSpace(options.Name) ||
                options.Price <= 0 ||
                options.Quantity <= 0)
            {
                return(new Result <ProductDto>(ErrorCode.BadRequest, "Not all required product options provided."));
            }

            var productWithSameCode = await _context.Products.SingleOrDefaultAsync(pro => pro.Code == options.Code);

            if (productWithSameCode != null)
            {
                return(new Result <ProductDto>(ErrorCode.Conflict, $"Product with code #{options.Code} already exists."));
            }

            var newProduct = new Product
            {
                Name        = options.Name,
                Price       = options.Price,
                Code        = options.Code,
                Description = options.Description,
                Quantity    = options.Quantity
            };

            await _context.Products.AddAsync(newProduct);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);

                return(new Result <ProductDto>(ErrorCode.InternalServerError, "Could not save product."));
            }

            return(new Result <ProductDto>
            {
                Data = ProductDto.MapFromProduct(newProduct)
            });
        }
示例#7
0
        public void CreateProduct_Success()
        {
            IProductService productService = new ProductService(context_);

            var options = new CreateProductOptions()
            {
                Id       = "123",
                Name     = "Mac",
                Category = TinyCrm.Core.Model.ProductCategory.Computers,
                Price    = 1000
            };

            var product = productService.CreateProduct(options);

            Assert.NotNull(product);
        }
示例#8
0
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null)
            {
                return(null);
            }
            var product = new Product()
            {
                Name = options.Name
            };

            // context_.Add(product);
            if (context_.SaveChanges() > 0)
            {
                return(product);
            }
            return(null);
        }
示例#9
0
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null || options.ProductId == null) // can't accept product with no Id
            {
                return(null);
            }
            // checking if product exists in the DB
            var checkprod = context_
                            .Set <Product>()
                            .Where(p => p.ProductId == options.ProductId)
                            .SingleOrDefault();

            if (checkprod != null)
            {
                throw new Exception("The product already exists!");
            }

            // product create
            var product = new Product()
            {
                ProductId   = options.ProductId,
                Name        = options.Name,
                Description = options.Description
            };

            if (options.Category != null)
            {
                product.ProductCategory = (ProductCategory)options.Category;
            }
            if (options.Price != null)
            {
                product.Price = (decimal)options.Price;
            }

            context_.Add(product);
            if (context_.SaveChanges() > 0)
            {
                return(product);
            }

            return(null);
        }
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.Id) ||
                string.IsNullOrWhiteSpace(options.Name) ||
                options.Price <= 0)
            {
                return(null);
            }

            var product = new Product();

            if (product != null)
            {
                return(null);
            }

            if (options.Name != null ||
                string.IsNullOrWhiteSpace(options.Name))
            {
                product.Name = options.Name;
            }

            if (options.Price > 0)
            {
                product.Price = options.Price;
            }

            if (options.Category == 0)
            {
                product.Category = options.Category;
            }

            context.Set <Product>().Add(product);
            context.SaveChanges();
            return(product);
        }
示例#11
0
        public Result <Product> CreateProduct(CreateProductOptions options)
        {
            if (options == null)
            {
                return(Result <Product> .CreateFailed(
                           StatusCode.BadRequest, "Null options"));
            }

            var product = new Product()
            {
                Category    = options.Category,
                Description = options.Description,
                Name        = options.Name,
                Price       = options.Price,
            };

            context_.Add(product);

            var rows = 0;

            try
            {
                rows = context_.SaveChanges();
            }
            catch (Exception ex)
            {
                return(Result <Product> .CreateFailed(
                           StatusCode.InternalServerError, ex.ToString()));
            }

            if (rows <= 0)
            {
                return(Result <Product> .CreateFailed(
                           StatusCode.InternalServerError,
                           "Product could not be created"));
            }

            return(Result <Product> .CreateSuccessful(product));
        }
示例#12
0
        public Product CreateProduct(CreateProductOptions options)
        {
            if (options == null || options.ProductId == null)
            {
                return(null);
            }

            var checkForExistingProduct = context_
                                          .Set <Product>()
                                          .Where(x => x.ProductId == options.ProductId)
                                          .SingleOrDefault();

            if (checkForExistingProduct != null)
            {
                throw new Exception("This product already exists.");
            }

            var product = new Product()
            {
                ProductId   = options.ProductId,
                Description = options.Description,
                Name        = options.Name,
            };

            if (options.Category != null)
            {
                product.Category = (ProductCategory)options.Category;
            }

            if (options.Price != null)
            {
                product.Price = (decimal)options.Price;
            }

            context_.Add(product);

            return(context_.SaveChanges() > 0 ? product : null);
        }
示例#13
0
 public Product AddProduct([FromBody] CreateProductOptions options)
 {
     return(productService.CreateProduct(options));
 }