示例#1
0
        public ActionResult Edit(CreateProductHistoryViewModel model)
        {
            var            productHistoryToUpdate = new ProductHistory();
            ProductHistory newProductHistory      = new ProductHistory()
            {
                ProductID = model.ProductID,
                HistoryID = model.HistoryID,
                Comment   = model.Comment,
                Date      = model.DateTimeNow,
                UserID    = model.Email
            };
            var  product = _unityOfWork.Product.getProductWithHistory(model.ProductID);
            long id      = product.ID;

            // find history entry in list of histories
            foreach (var productHistory in product.ProductHistories)
            {
                // a unique history entry is given by ProductID, HistoryID, Date
                if (productHistory.Date.ToString() == model.DateTimeNow.ToString() && model.OldHistoryID == productHistory.HistoryID && model.Email == productHistory.UserID)
                {
                    productHistoryToUpdate = productHistory;
                    break;
                }
            }
            product.ProductHistories.Remove(productHistoryToUpdate);
            product.ProductHistories.Add(newProductHistory);
            _unityOfWork.Product.Update(product);
            _unityOfWork.Save();
            newProductHistory.User   = null;
            newProductHistory.UserID = null;
            LoggingController.writeLog(newProductHistory, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString());
            return(RedirectToAction("Details", new { id = id }));
        }
        public async Task <JsonResult> QuickAddHistory(CreateProductHistoryViewModel model)
        {
            if (model.UploadFile != null)
            {
                var uploads = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads");



                if (model.UploadFile.Length > 0)

                {
                    using (var fileStream = new FileStream(Path.Combine(uploads, model.UploadFile.FileName), FileMode.Create))

                    {
                        await model.UploadFile.CopyToAsync(fileStream);
                    }
                    Upload myFile = new Upload()
                    {
                        RelativPath = Path.Combine(uploads, model.UploadFile.FileName),
                        Size        = model.UploadFile.Length.ToString(),
                        Format      = model.UploadFile.ContentType.ToString(),
                    };
                    _unityOfWork.Upload.Insert(myFile);
                    _unityOfWork.Save();
                    model.UploadID = myFile.ID;
                }
            }



            try
            {
                Product        product        = _unityOfWork.Product.GetById(model.ProductID);
                ProductHistory productHistory = new ProductHistory
                {
                    UserID    = _db.Users.FirstOrDefault(x => x.Email == model.Email).Id,
                    Comment   = model.Comment,
                    Date      = DateTime.Now,
                    ProductID = model.ProductID,
                    HistoryID = model.HistoryID,
                    FileID    = model.UploadID
                };



                product.ProductHistories.Add(productHistory);
                _unityOfWork.Product.Update(product);
                _unityOfWork.Save();
                productHistory.User   = null;
                productHistory.UserID = null;
                LoggingController.writeLog(productHistory, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString());
                return(Json(productHistory));
            }
            catch
            {
                return(Json(null));
            }
        }
        public async Task <IActionResult> AddHistory(CreateProductHistoryViewModel model)
        {
            if (model.UploadFile != null)
            {
                var uploads = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads");

                if (model.UploadFile.Length > 0)

                {
                    string fName             = Path.GetFileNameWithoutExtension(model.UploadFile.FileName);
                    string fExt              = Path.GetExtension(model.UploadFile.FileName);
                    string hashCode          = RandomString(5);
                    string newUniqueFileName = String.Concat(fName, "_" + hashCode, fExt);

                    using (var fileStream = new FileStream(Path.Combine(uploads, newUniqueFileName), FileMode.Create))

                    {
                        await model.UploadFile.CopyToAsync(fileStream);
                    }
                    Upload myFile = new Upload()
                    {
                        RelativPath = Path.Combine(uploads, newUniqueFileName),
                        Size        = model.UploadFile.Length.ToString(),
                        Format      = model.UploadFile.ContentType.ToString(),
                    };
                    _unityOfWork.Upload.Insert(myFile);
                    _unityOfWork.Save();
                    model.UploadID = myFile.ID;
                }
            }
            Product        product        = _unityOfWork.Product.GetById(model.ProductID);
            ProductHistory productHistory = new ProductHistory
            {
                UserID    = _db.Users.FirstOrDefault(x => x.Email == model.Email).Id,
                Comment   = model.Comment,
                Date      = DateTime.Now,
                ProductID = model.ProductID,
                HistoryID = model.HistoryID,
                FileID    = model.UploadID
            };

            product.ProductHistories.Add(productHistory);
            _unityOfWork.Product.Update(product);
            _unityOfWork.Save();
            productHistory.User   = null;
            productHistory.UserID = null;
            LoggingController.writeLog(productHistory, User.Identity.Name, this.ControllerContext.RouteData.Values["action"].ToString(), this.ControllerContext.RouteData.Values["controller"].ToString());
            return(Redirect(Request.Headers["Referer"]));
        }