public IActionResult Create(ProductCreateViewModel product)
        {
            List <Attribute> attributes = new List <Attribute>();
            int            arrIndex     = 0;
            ProductHistory history      = new ProductHistory()
            {
                UserID    = _db.Users.FirstOrDefault(x => x.Email == product.Email).Id,
                HistoryID = _unityOfWork.History.GetByName("Created"),
                Date      = DateTime.Now,
                Comment   = "Created"
            };
            Product myProduct = new Product()
            {
                CustomerID         = product.CustomerID,
                Description        = product.Description,
                ProjectID          = product.ProjectID,
                SerialNumber       = product.SerialNumber,
                DeviceTypeID       = product.DeviceTypeID,
                StockInformationID = product.StockInformationID,
            };

            if (product.SelectedAttributes != null)
            {
                foreach (string item in product.SelectedAttributes)
                {
                    Attribute attr = _unityOfWork.Attribute.GetById(Convert.ToInt32(item));
                    myProduct.ProductAttributes.Add(new ProductAttribute()
                    {
                        AttributeID = attr.ID,
                        Value       = product.ValueSelectedAttributes[arrIndex]
                    }
                                                    );;
                    arrIndex++;
                }
            }



            myProduct.ProductHistories.Add(history);
            _unityOfWork.Product.Insert(myProduct);
            _unityOfWork.Save();
            int counterOptionsValues = 0;

            foreach (var optionID in product.SelectedSoftwareOptions)
            {
                ProductSoftwareOptions productSoftwareOption = new ProductSoftwareOptions()
                {
                    ProductID        = myProduct.ID,
                    SoftwareOptionID = Convert.ToInt32(optionID),
                    SoftwareTypeID   = product.SoftwareID ?? 0,
                    Product          = myProduct,
                    SoftwareOption   = _db.SoftwareOptions.FirstOrDefault(x => x.ID == Convert.ToInt32(optionID)),
                    SoftwareType     = _db.SoftwareTypes.FirstOrDefault(c => c.ID == product.SoftwareID),
                    Value            = product.ValueSelectedOptions[counterOptionsValues]
                };
                counterOptionsValues++;
                _db.ProductSoftwareOptions.Add(productSoftwareOption);
                _db.SaveChanges();
            }

            try
            {
                foreach (string productChild in product.TypeChild)
                {
                    var childProduct = _unityOfWork.Product.GetById(Convert.ToInt32(productChild));
                    childProduct.ParentID = myProduct.ID;
                    _unityOfWork.Product.Update(childProduct);
                    _unityOfWork.Save();
                }
            }
            catch { }
            LoggingController.writeLog(product, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString());
            return(RedirectToAction("Index"));
        }
예제 #2
0
        public IActionResult Create(TypeCreateViewModel type)
        {
            int arrIndex = 0;

            PType myType = new PType()
            {
                Name        = type.Name,
                Description = type.Description,
            };

            if (type.SelectedFeatures != null)
            {
                foreach (long feature in type.SelectedFeatures)
                {
                    myType.TypeFeatures.Add(new TypeFeature()
                    {
                        FeatureID = feature
                    });
                }
            }
            if (type.SelectedAttributes != null)
            {
                foreach (string item in type.SelectedAttributes)
                {
                    Attribute attr = _unityOfWork.Attribute.GetByName(item);
                    myType.TypeAttributes.Add(new TypeAttribute()
                    {
                        AttributeID = attr.ID,
                        Value       = type.ValueSelectedAttributes[arrIndex]
                    }
                                              );;
                    arrIndex++;
                }
            }


            List <long> existingChilds = new List <long>();

            if (type.TypeChild != null)
            {
                foreach (long child in type.TypeChild)
                {
                    if (!existingChilds.Contains(child))
                    {
                        myType.Childs.Add(new TypeChild()
                        {
                            ChildID  = child,
                            Quantity = type.TypeChild.Where(p => p == child).Count()
                        });;
                        existingChilds.Add(child);
                    }
                }
            }


            _unityOfWork.Type.Insert(myType);
            _unityOfWork.Save();
            LoggingController.writeLog(myType, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString());


            return(RedirectToAction("Index"));
        }
예제 #3
0
 public void Insert(Attribute attribute)
 {
     _context.Attribute.Add(attribute);
 }
예제 #4
0
 public void Update(Attribute attribute)
 {
     _context.Attribute.Update(attribute);
 }
예제 #5
0
 public void Delete(Attribute attribute)
 {
     _context.Attribute.Remove(attribute);
 }