Пример #1
0
        public async Task <Package> UpdatePackageAsync(PackageServiceModel serviceModel)
        {
            var package = await GetPackages().SingleAsync(o => o.Id == serviceModel.Id);

            var newPackage = serviceModel.ToEntity();

            _context.Entry(package).CurrentValues.SetValues(newPackage);
            _context.Entry(package).Property(o => o.CreateByUserId).IsModified = false;
            _context.Entry(package).Property(o => o.CreateDate).IsModified     = false;

            foreach (var newDetail in newPackage.PackageDetails)
            {
                var oldDetail = package.PackageDetails.FirstOrDefault(o => o.Language == newDetail.Language);
                if (oldDetail.Language == newDetail.Language)
                {
                    newDetail.Id = oldDetail.Id;
                    _context.Entry(oldDetail).CurrentValues.SetValues(newDetail);
                }
            }

            _context.TryUpdateManyToMany(package.EnitityFiles, newPackage.EnitityFiles, o => o.FileEntityId);
            _context.TryUpdateManyToMany(package.EntityTaxonomies, newPackage.EntityTaxonomies, o => o.TaxonomyId);

            await _context.SaveChangesAsync();

            return(newPackage);
        }
        public async Task AddEdgePackageTest()
        {
            // Arrange
            const string collectionId = "packages";
            const string key          = "package name";
            var          pkg          = new PackageServiceModel
            {
                Id          = string.Empty,
                Name        = key,
                PackageType = PackageType.EdgeManifest,
                ConfigType  = string.Empty,
                Content     = EdgePackageJson,
            };
            var value = JsonConvert.SerializeObject(pkg);

            this.mockClient
            .Setup(x => x.CreateAsync(
                       It.Is <string>(i => i == collectionId),
                       It.IsAny <string>()))
            .ReturnsAsync(new ValueApiModel
            {
                Key  = key,
                Data = value,
            });

            // Act
            var result = await this.storage.AddPackageAsync(pkg);

            // Assert
            Assert.Equal(pkg.Name, result.Name);
            Assert.Equal(pkg.PackageType, result.PackageType);
            Assert.Equal(pkg.Content, result.Content);
        }
Пример #3
0
 public static int SavePackage(PackageServiceModel model)
 {
     DbParameter[] ObjParam = new DbParameter[3];
     ObjParam[0] = new DbParameter("@ParlourId", DbParameter.DbType.UniqueIdentifier, 0, model.ParlourId);
     ObjParam[1] = new DbParameter("@PackageName", DbParameter.DbType.VarChar, 0, model.PackageName);
     ObjParam[2] = new DbParameter("@ModifiedUser", DbParameter.DbType.VarChar, 0, model.ModifiedUser);
     return(Convert.ToInt32(DbConnection.GetScalarValue(CommandType.StoredProcedure, "PackagesSave", ObjParam)));
 }
Пример #4
0
        private bool IsValidPackage(PackageServiceModel package)
        {
            IPackageValidator validator = PackageValidatorFactory.GetValidator(
                package.PackageType,
                package.ConfigType);

            // Bypass validation for custom _config type
            return(validator == null || validator.Validate());
        }
Пример #5
0
 public PackageApiModel(PackageServiceModel model)
 {
     this.Id          = model.Id;
     this.Name        = model.Name;
     this.packageType = model.PackageType;
     this.DateCreated = model.DateCreated;
     this.Content     = model.Content;
     this.ConfigType  = model.ConfigType;
 }
        public async Task AddADMPackageTest(bool isCustomConfigType)
        {
            // Arrange
            const string collectionId = "packages";
            const string key          = "package name";
            string       configType   = isCustomConfigType ? "Custom-config" : ConfigType.Firmware.ToString();

            var pkg = new PackageServiceModel
            {
                Id          = string.Empty,
                Name        = key,
                PackageType = PackageType.DeviceConfiguration,
                Content     = AdmPackageJson,
                ConfigType  = configType,
            };

            var value = JsonConvert.SerializeObject(pkg);

            this.mockClient
            .Setup(x => x.CreateAsync(
                       It.Is <string>(i => i == collectionId),
                       It.IsAny <string>()))
            .ReturnsAsync(new ValueApiModel
            {
                Key  = key,
                Data = value,
            });

            const string configKey = "config-types";

            this.mockClient
            .Setup(x => x.UpdateAsync(
                       It.Is <string>(i => i == collectionId),
                       It.Is <string>(i => i == configKey),
                       It.Is <string>(i => i == ConfigType.Firmware.ToString()),
                       It.Is <string>(i => i == "*")))
            .ReturnsAsync(new ValueApiModel
            {
                Key  = key,
                Data = value,
            });

            this.mockClient
            .Setup(x => x.GetAsync(
                       It.Is <string>(i => i == collectionId),
                       It.Is <string>(i => i == configKey)))
            .ThrowsAsync(new ResourceNotFoundException());

            // Act
            var result = await this.storage.AddPackageAsync(pkg);

            // Assert
            Assert.Equal(pkg.Name, result.Name);
            Assert.Equal(pkg.PackageType, result.PackageType);
            Assert.Equal(pkg.Content, result.Content);
            Assert.Equal(pkg.ConfigType, result.ConfigType);
        }
Пример #7
0
        public static int SavePackageService(PackageServiceModel model)
        {
            DbParameter[] ObjParam = new DbParameter[4];
            ObjParam[0] = new DbParameter("@fkiServiceID", DbParameter.DbType.Int, 0, model.fkiServiceID);
            ObjParam[1] = new DbParameter("@PackageName", DbParameter.DbType.VarChar, 0, model.PackageName);
            ObjParam[2] = new DbParameter("@ModifiedUser", DbParameter.DbType.VarChar, 0, model.ModifiedUser);
            ObjParam[3] = new DbParameter("@fkiPackageID", DbParameter.DbType.Int, 0, model.fkiPackageID);

            return(Convert.ToInt32(DbConnection.GetScalarValue(CommandType.Text, "INSERT INTO [dbo].[PackageServicesSelection] ([packagename],[fkiServiceID],[LastModified],[ModifiedUser],[fkiPackageID]) Values(@packagename,@fkiServiceID,getdate(),@ModifiedUser,@fkiPackageID) Select SCOPE_IDENTITY() AS ID", ObjParam)));
        }
Пример #8
0
        protected void btnAddService_Click(object sender, EventArgs e)
        {
            PackageServiceModel model = new PackageServiceModel();

            model.PackageName  = txtPackageName.Text.Trim();
            model.ModifiedUser = this.UserName;
            model.ParlourId    = this.ParlourId;
            model.fkiPackageID = this.PackageId;
            model.fkiServiceID = Convert.ToInt32(ddlServices.SelectedValue);
            client.SavePackageService(model);
            BindSelectedService();
        }
Пример #9
0
        public async Task <PackageServiceModel> AddPackageAsync(PackageServiceModel package, string userId, string tenantId)
        {
            bool isValidPackage = this.IsValidPackage(package);

            if (!isValidPackage)
            {
                var msg = "Package provided is a invalid deployment manifest " +
                          $"for type {package.PackageType}";

                msg += package.PackageType.Equals(PackageType.DeviceConfiguration) ?
                       $"and configuration {package.ConfigType}" : string.Empty;

                throw new InvalidInputException(msg);
            }

            try
            {
                JsonConvert.DeserializeObject <Configuration>(package.Content);
            }
            catch (Exception)
            {
                throw new InvalidInputException("Package provided is not a valid deployment manifest");
            }

            package.DateCreated = DateTimeOffset.UtcNow.ToString(DateFormat);
            AuditHelper.AddAuditingData(package, userId);

            var value = JsonConvert.SerializeObject(
                package,
                Formatting.Indented,
                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
            });

            var response = await this.client.CreateAsync(PackagesCollectionId, value);

            if (!string.IsNullOrEmpty(package.ConfigType) && package.PackageType.Equals(PackageType.DeviceConfiguration))
            {
                await this.UpdateConfigTypeAsync(package.ConfigType);
            }

            // Setting the package id before logging
            package.Id = response.Key;

            // Log a custom event to Application Insight
            this.packageLog.LogPackageUpload(package, tenantId, userId);

            return(this.CreatePackageServiceModel(response));
        }
        public async Task InvalidPackageThrowsTest()
        {
            // Arrange
            var pkg = new PackageServiceModel
            {
                Id          = string.Empty,
                Name        = "testpackage",
                PackageType = PackageType.EdgeManifest,
                Content     = "InvalidPackage",
            };

            // Act & Assert
            await Assert.ThrowsAsync <InvalidInputException>(async() =>
                                                             await this.storage.AddPackageAsync(pkg));
        }
 public PackageApiModel(PackageServiceModel model)
 {
     this.Id           = model.Id;
     this.Name         = model.Name;
     this.PackageType  = model.PackageType;
     this.DateCreated  = model.DateCreated;
     this.Content      = model.Content;
     this.ConfigType   = model.ConfigType;
     this.Tags         = model.Tags;
     this.Version      = model.Version;
     this.CreatedDate  = model.CreatedDate;
     this.CreatedBy    = model.CreatedBy;
     this.ModifiedBy   = model.ModifiedBy;
     this.ModifiedDate = model.ModifiedDate;
 }
        public static PackageServiceModel ToPackageServiceModel(this PackageUpdateViewModel viewModel)
        {
            var taxonomyIds = new List <long>()
            {
                viewModel.HouseTypeId, viewModel.DesignThemeId,
            };

            if (viewModel.PackageIncludedItemIds != null)
            {
                taxonomyIds.AddRange(viewModel.PackageIncludedItemIds);
            }

            if (viewModel.PackageFurnitureIncludedItemIds != null)
            {
                taxonomyIds.AddRange(viewModel.PackageFurnitureIncludedItemIds);
            }

            var pictureFileIds = new List <long>();

            if (viewModel.Pictures != null)
            {
                pictureFileIds = pictureFileIds.Concat(viewModel.Pictures.Select(o => o.FileId)).ToList();
            }

            var detail = viewModel.GetPackageDetail();

            var addNewpackageServiceModel = new PackageServiceModel()
            {
                Id             = viewModel.Id,
                Name           = viewModel.Title.ToEntityName(),
                IsPerspective  = viewModel.IsPerspective,
                Detail         = detail,
                TaxonomyIds    = taxonomyIds,
                AvatarFileId   = viewModel.Avatar.FileId,
                PictureFileIds = pictureFileIds,
            };

            if (viewModel.Products != null)
            {
                addNewpackageServiceModel.PackageProducts = new List <PackageProduct>(
                    viewModel.Products.Where(o => o.ProductId != default).Select(o => new PackageProduct {
                    EntityId = viewModel.Id, ProductId = o.ProductId, Quantity = o.Quantity,
                })
                    );
            }

            return(addNewpackageServiceModel);
        }
Пример #13
0
        public void LogPackageUpload(PackageServiceModel package, string tenantId, string userId)
        {
            var eventInfo = new CustomEvent
            {
                EventSource      = Convert.ToString(EventSource.Config),
                EventType        = Convert.ToString(EventType.PackageUpload),
                EventTime        = DateTimeOffset.UtcNow.ToString(AppInsightDateFormat),
                EventDescription = $@"Package {package.Name} of {package.PackageType} uploaded successfully by User {userId}",
                TenantId         = tenantId,
                PackageId        = package.Id,
                PackageName      = package.Name,
                PackageType      = package.PackageType.ToString(),
                UserId           = userId,
            };

            this.LogCustomEvent($"{tenantId.Substring(0, 8)}-{package.Id}", package.Name, eventInfo);
        }
Пример #14
0
        public async Task <PackageServiceModel> AddPackageAsync(PackageServiceModel package)
        {
            bool isValidPackage = this.IsValidPackage(package);

            if (!isValidPackage)
            {
                var msg = "Package provided is a invalid deployment manifest " +
                          $"for type {package.PackageType}";

                msg += package.PackageType.Equals(PackageType.DeviceConfiguration) ?
                       $"and configuration {package.ConfigType}" : string.Empty;

                throw new InvalidInputException(msg);
            }

            try
            {
                JsonConvert.DeserializeObject <Configuration>(package.Content);
            }
            catch (Exception)
            {
                throw new InvalidInputException("Package provided is not a valid deployment manifest");
            }

            package.DateCreated = DateTimeOffset.UtcNow.ToString(DateFormat);
            var value = JsonConvert.SerializeObject(
                package,
                Formatting.Indented,
                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
            });

            var response = await this.client.CreateAsync(PackagesCollectionId, value);

            if (!string.IsNullOrEmpty(package.ConfigType) && package.PackageType.Equals(PackageType.DeviceConfiguration))
            {
                await this.UpdateConfigTypeAsync(package.ConfigType);
            }

            return(this.CreatePackageServiceModel(response));
        }
Пример #15
0
        protected void btnSavePackage_Click(object sender, EventArgs e)
        {
            PackageServiceModel model = new PackageServiceModel();

            model.PackageName  = txtPackageName.Text.Trim();
            model.ModifiedUser = this.UserName;
            model.ParlourId    = this.ParlourId;
            int currentPackageId = client.SavePackage(model);

            if (currentPackageId == 0)
            {
                this.ShowMessage(ref lblMessage, MessageType.Warning, string.Format("{0} Package already exist", txtPackageName.Text.Trim()));
            }
            else
            {
                this.PackageId = currentPackageId;
                GetPackageList();
                btnAddService.Visible = true;
                btnAddService_Click(null, null);
            }
        }
Пример #16
0
        public async Task <Package> UpdatePackageAsync(PackageServiceModel serviceModel)
        {
            var package = await GetPackages().SingleAsync(o => o.Id == serviceModel.Id);

            var newPackage = serviceModel.ToEntity();

            _context.Entry(package).CurrentValues.SetValues(newPackage);
            _context.Entry(package).Property(o => o.Name).IsModified           = false;
            _context.Entry(package).Property(o => o.CreateByUserId).IsModified = false;
            _context.Entry(package).Property(o => o.CreateDate).IsModified     = false;

            foreach (var newDetail in newPackage.Details)
            {
                var oldDetail = package.Details.FirstOrDefault(o => o.Language == newDetail.Language);
                if (oldDetail.Language == newDetail.Language)
                {
                    newDetail.Id = oldDetail.Id;
                    _context.Entry(oldDetail).CurrentValues.SetValues(newDetail);
                }
            }

            _context.TryUpdateManyToMany(package.EntityFiles, newPackage.EntityFiles, o => o.FileEntityId);
            _context.TryUpdateManyToMany(package.EntityTaxonomies, newPackage.EntityTaxonomies, o => o.TaxonomyId);

            _context.TryUpdateManyToMany(package.EntityProducts, newPackage.EntityProducts, o => o.ProductId);
            foreach (var entityProduct in package.EntityProducts)
            {
                var pdEntry = _context.Entry(entityProduct);
                if (pdEntry.State == EntityState.Deleted)
                {
                    continue;
                }

                pdEntry.Property(o => o.Quantity).CurrentValue = newPackage.EntityProducts.FirstOrDefault(o => o.ProductId == entityProduct.ProductId).Quantity;
            }

            await _context.SaveChangesAsync();

            return(newPackage);
        }
Пример #17
0
        public static PackageServiceModel ToPackageServiceModel(this PackageUpdateViewModel viewModel)
        {
            var taxonomyIds = new List <long>(viewModel.PackageIncludedItemIds)
            {
                viewModel.HouseTypeId, viewModel.DesignThemeId,
            };

            var pictureFileIds = new List <long>(viewModel.Pictures.Select(o => o.FileId));
            var detail         = viewModel.GetPackageDetail();

            var addNewpackageServiceModel = new PackageServiceModel()
            {
                Id             = viewModel.Id,
                Name           = viewModel.Title.ToEntityName(),
                Detail         = detail,
                TaxonomyIds    = taxonomyIds,
                AvatarFileId   = viewModel.Avatar.FileId,
                PictureFileIds = pictureFileIds
            };

            return(addNewpackageServiceModel);
        }
Пример #18
0
        public async Task <Package> CreateNewPackage(PackageServiceModel serviceModel)
        {
            var newPackage = new Package
            {
                Name           = serviceModel.Name,
                CreateByUserId = serviceModel.User.Id,
                PackageDetails = new List <PackageDetail>()
                {
                    serviceModel.Detail
                },
                EnitityFiles     = serviceModel.GetEntityFiles(),
                EntityTaxonomies = new List <PackageTaxonomy>(
                    serviceModel.TaxonomyIds.Select(taxonomyId => new PackageTaxonomy {
                    TaxonomyId = taxonomyId
                }))
            };

            var add = await _context.Package.AddAsync(newPackage);

            _context.SaveChanges();

            return(add.Entity);
        }
Пример #19
0
 public int SavePackageService(PackageServiceModel model)
 {
     return(FuneralPackageBAL.SavePackageService(model));
 }