Example #1
0
        public IHttpActionResult PostProduct(ProductPostVM productVM)
        {
            Product product = new Product()
            {
                productCode        = productVM.productCode,
                productName        = productVM.productName,
                productLine        = productVM.productLine,
                buyPrice           = productVM.buyPrice,
                MSRP               = productVM.MSRP,
                productDescription = productVM.productDescription,
                productScale       = productVM.productScale,
                productVendor      = productVM.productVendor,
                quantityInStock    = productVM.quantityInStock
            };

            db.Products.Add(product);
            db.SaveChanges();

            return(Ok());
        }
Example #2
0
        //[Authorize(Roles = "Admin")]
        public IActionResult Create([FromBody] ProductPostVM model)
        {
            List <string> err = new List <string>();

            // перевіряємо модель на валідність
            if (!ModelState.IsValid)
            {
                var errors = CustomValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errors));
            }

            // зберігаємо фото
            var bmp = model.PhotoBase64.FromBase64StringToImage();

            if (bmp != null)
            {
                model.PhotoName = Path.GetRandomFileName() + ".jpg";

                string imageNamePath = Path.Combine(dirPathSave, model.PhotoName);
                var    image         = ImageHelper.CreateImage(bmp, 300, 300);
                image.Save(imageNamePath, ImageFormat.Jpeg);
            }
            else
            {
                model.PhotoName = kNamePhotoDefault;
            }

            // передаємо модель в БД
            DbProduct prod = new DbProduct
            {
                Name       = model.Name,
                Price      = model.Price,
                DateCreate = DateTime.Now,
                PhotoName  = model.PhotoName,
                CategoryId = model.CategoryId
            };

            _context.Products.Add(prod);
            _context.SaveChanges();


            int fValueIdType = model.FilterIdType;
            int fNameIdType  = _context.FilterNameGroups
                               .SingleOrDefault(v => v.FilterValueId == model.FilterIdType).FilterNameId;


            int valuePrice = 0;

            if (model.Price < 50)
            {
                valuePrice = 1;
            }
            else if (model.Price >= 50 && model.Price < 100)
            {
                valuePrice = 2;
            }
            else if (model.Price >= 100 && model.Price < 300)
            {
                valuePrice = 3;
            }
            else if (model.Price >= 300 && model.Price < 700)
            {
                valuePrice = 4;
            }
            else if (model.Price >= 700)
            {
                valuePrice = 5;
            }

            Filter filter = new Filter {
                FilterNameId = 1, FilterValueId = valuePrice, ProductId = prod.Id
            };
            var f = _context.Filters.SingleOrDefault(p => p == filter);

            if (f == null)
            {
                _context.Filters.Add(filter);
                _context.SaveChanges();
            }

            filter = new Filter {
                FilterNameId = fNameIdType, FilterValueId = fValueIdType, ProductId = prod.Id
            };
            f = _context.Filters.SingleOrDefault(p => p == filter);
            if (f == null)
            {
                _context.Filters.Add(filter);
                _context.SaveChanges();
            }

            return(Ok(prod.Id));
        }