Beispiel #1
0
        /// <summary>
        /// Make a default package when none exists
        /// </summary>
        /// <param name="performingUserId"></param>
        /// <param name="portfolioService"></param>
        /// <param name="optionId"></param>
        /// <returns></returns>
        public static IServiceRequestPackageDto GetPackage(int performingUserId, IPortfolioService portfolioService,
                                                           int optionId)
        {
            IServiceRequestPackageDto package = null;

            package = new ServiceRequestPackageDto();
            package.ServiceOptionCategoryTags = new List <IServiceOptionCategoryTagDto>();
            //it consists of just the option category
            int categoryId = portfolioService.GetServiceOption(performingUserId, optionId).ServiceOptionCategoryId;

            package.ServiceOptionCategoryTags.Add(
                new ServiceOptionCategoryTagDto
            {
                ServiceOptionCategory   = portfolioService.GetServiceOptionCategory(performingUserId, categoryId),
                ServiceOptionCategoryId = categoryId
            });
            package.Name = package.ServiceOptionCategoryTags.First().ServiceOptionCategory.Name;
            return(package);
        }
        public ActionResult SavePackage(PackageModel package)
        {
            if (!ModelState.IsValid)
            {
                TempData["MessageType"] = WebMessageType.Failure;
                TempData["Message"]     = "Failed to retrieve service package due to invalid data";
                if (package.Id == 0)
                {
                    return(RedirectToAction("AddPackage"));
                }
                return(RedirectToAction("UpdatePackage", new { id = package.Id }));
            }
            IServiceRequestPackageDto newPackage = new ServiceRequestPackageDto();             //transfer data to new package

            newPackage.Name   = package.Name;
            newPackage.Action = package.Action;
            newPackage.ServiceOptionCategoryTags = new List <IServiceOptionCategoryTagDto>();
            newPackage.ServiceTags = new List <IServiceTagDto>();
            List <string> associations = new List <string>();

            if (package.Associations != null)                           //these are optional
            {
                associations.AddRange(package.Associations);            //put the primary to the front so it can all be done at once
            }
            associations.Insert(0, package.Primary);
            int i = 1;                                                                             //used to order the category tags in a package

            foreach (var association in associations)                                              //build new package
            {
                int associationId = int.Parse(association.Substring(2, (association.Length - 2))); //extract the actual Id
                if (association.Contains("C_"))
                {
                    newPackage.ServiceOptionCategoryTags.Add(new ServiceOptionCategoryTagDto {
                        ServiceOptionCategoryId = associationId, Order = i
                    });
                }
                else if (association.Contains("S_"))
                {
                    newPackage.ServiceTags.Add(new ServiceTagDto {
                        ServiceId = associationId, Order = i
                    });
                }
                i++;
            }
            try
            {
                if (package.Id > 0)
                {
                    _portfolioService.ModifyServiceRequestPackage(UserId, new ServiceRequestPackageDto {
                        Id = package.Id
                    }, EntityModification.Delete);
                }
                _portfolioService.ModifyServiceRequestPackage(UserId, newPackage, EntityModification.Create);
            }
            catch (Exception exception)
            {
                TempData["MessageType"] = WebMessageType.Failure;
                TempData["Message"]     = $"Failed to save service package, error: {exception.Message}";
            }

            TempData["MessageType"] = WebMessageType.Success;
            TempData["Message"]     = "Successfully saved package";

            return(RedirectToAction("ShowPackages", new { id = 0 }));
        }