Beispiel #1
0
        public void AddMediaToProductComplianceTest()
        {
            var testUser    = GetTestUser();
            var testCompany = GetTestCompany(testUser);
            var product     = createProduct(testCompany, testUser);

            ProductService.InsertOrUpdateProduct(product, testUser, "");

            var pcList = ProductService.FindProductComplianceListModel(product.Id);

            if (pcList.Items.Count > 0)
            {
                foreach (var pc in pcList.Items)
                {
                    ProductService.DeleteProductCompliance(pc.Id);
                }
            }

            // Check number of pc in list before test
            pcList = ProductService.FindProductComplianceListModel(product.Id);
            int expected = 0;
            int actual   = pcList.Items.Count();

            Assert.IsTrue(actual == expected, $"Error: {actual} items were found when {expected} were expected");

            // Create X number of records
            int randomNoOfRecords = RandomInt(5, 20);

            for (var i = 0; i < randomNoOfRecords; i++)
            {
                // Create Product Compliance
                var prodCom = createProductCompliance(testCompany, product);
                var error   = ProductService.InsertOrUpdateProductCompliance(prodCom, "");
                Assert.IsTrue(!error.IsError, error.Message);
                // Create Media
                var media      = new MediaModel();
                var targetFile = GetTempFile(".jpg");
                error = MediaServices.InsertOrUpdateMedia(media, testCompany, testUser, Evolution.Enumerations.MediaFolder.ProductCompliance,
                                                          targetFile, "", prodCom.ProductId, prodCom.Id, FileCopyType.Move);
                Assert.IsTrue(!error.IsError, error.Message);

                ProductService.AddMediaToProductCompliance(prodCom, media);
            }

            pcList   = ProductService.FindProductComplianceListModel(product.Id);
            expected = randomNoOfRecords;
            actual   = pcList.Items.Count();
            Assert.IsTrue(actual == expected, $"Error: {actual} items were found when {expected} were expected");

            // Delete them
            foreach (var pc in pcList.Items)
            {
                ProductService.DeleteProductCompliance(pc.Id);
            }
            // Check number of pc in list before test
            pcList   = ProductService.FindProductComplianceListModel(product.Id);
            expected = 0;
            actual   = pcList.Items.Count();
            Assert.IsTrue(actual == expected, $"Error: {actual} items were found when {expected} were expected");
        }
Beispiel #2
0
        public ActionResult Save(EditProductComplianceViewModel model, string command)
        {
            string cmd = command.ToLower();

            if (cmd == "save")
            {
                var modelError = ProductService.InsertOrUpdateProductCompliance(model.ProductCompliance, model.LGS);
                if (modelError.IsError)
                {
                    prepareEditModel(model, model.ProductCompliance.Id, model.ProductCompliance.Id);
                    model.SetErrorOnField(ErrorIcon.Error,
                                          modelError.Message,
                                          "ProductCompliance_" + modelError.FieldName);
                }
                else
                {
                    model.SetErrorOnField(ErrorIcon.Information, EvolutionResources.infChangesSuccessfullySaved, "", null, null, null, null, true);

                    // Delete existing attachments
                    foreach (var attachment in model.ProductCompliance.Attachments)
                    {
                        if (attachment.Selected)
                        {
                            ProductService.DeleteProductComplianceAttachment(attachment);
                        }
                    }

                    // Upload the attachments
                    if (model.Files != null || model.Files.Count() > 0)
                    {
                        var fileList     = new List <string>();
                        var targetFolder = MediaServices.GetMediaFolder(CurrentCompany.Id) + "\\Temp\\";
                        MediaService.MediaService.CreateDirectory(targetFolder);

                        foreach (var file in model.Files)
                        {
                            if (file != null)
                            {
                                string targetFile = targetFolder + file.FileName.FileName();
                                try {
                                    db.AddFileToLog(targetFile, 2);
                                    file.SaveAs(targetFile);

                                    var media = new MediaModel();
                                    model.Error = MediaServices.InsertOrUpdateMedia(media, CurrentCompany, CurrentUser,
                                                                                    MediaFolder.ProductCompliance,
                                                                                    targetFile, "",
                                                                                    model.ProductCompliance.ProductId,
                                                                                    model.ProductCompliance.Id,
                                                                                    FileCopyType.Move);
                                    if (model.Error.IsError)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        ProductService.AddMediaToProductCompliance(model.ProductCompliance, media);

                                        model.SetErrorOnField(ErrorIcon.Information, EvolutionResources.infFilesSuccessfullyUploaded, "", null, null, null, null, true);
                                    }
                                } catch (Exception ex) {
                                    model.SetError(ex, "", true);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (!model.Error.IsError)
            {
                return(RedirectToAction("Compliance", new { id = model.ParentId }));
            }
            else
            {
                return(View("Edit", model));
            }
        }