示例#1
0
        /// <summary>
        /// Handle the PPE Types on LEFT (i.e. basic details HWCs submit)
        /// </summary>
        void updateBasicPpeDetails(EditNeedsPost s)
        {
            List <PpeTypes> existingPpeTypes = NeedPpeTypes.SelectToList(p => (PpeTypes)p.PpeTypeId);

            //create new
            List <NeedPpeTypeViewModel> ppeTypesToAdd = s.Request.PpeTypes.WhereToList(p => p.Selected && !existingPpeTypes.Contains(p.Type));

            foreach (NeedPpeTypeViewModel needPpeTypeViewModel in ppeTypesToAdd)
            {
                NeedPpeType ppeTypeToAdd = NeedPpeType.Create_FromViewModel(needPpeTypeViewModel, this);
                NeedPpeTypes.Add(ppeTypeToAdd);
            }

            //remove deselected
            List <NeedPpeTypeViewModel> ppeTypesToRemove = s.Request.PpeTypes.WhereToList(p => !p.Selected && existingPpeTypes.Contains(p.Type));

            foreach (NeedPpeTypeViewModel needPpeTypeViewModel in ppeTypesToRemove)
            {
                NeedPpeType typeToRemove = NeedPpeTypes.Single(p => p.PpeTypeId == (byte)needPpeTypeViewModel.Type);
                NeedPpeTypes.Remove(typeToRemove);
            }

            //edit rest (existing)
            List <NeedPpeTypeViewModel> ppeTypesToModify = s.Request.PpeTypes.WhereToList(p => p.Selected && existingPpeTypes.Contains(p.Type));

            foreach (NeedPpeTypeViewModel needPpeTypeViewModel in ppeTypesToModify)
            {
                NeedPpeType needExisting = NeedPpeTypes.SingleOrDefault(p => p.PpeTypeId == (byte)needPpeTypeViewModel.Type);
                needExisting?.Modify(needPpeTypeViewModel);                 //works??
            }
        }
        /// <summary>
        /// For Inserts
        /// </summary>
        public static Need CreateFromViewModel(NeedsViewModel s)
        {
            Need respVal = new Need
            {
                PublishAnonymously = s.PublishAnonymously,
                Timestamp          = DateTimeOffset.Now,          //N.B for inserts
                StatusId           = (int)PostStatus.UnderReview, //N.B for inserts
                ContactName        = s.ContactName ?? "",
                JobTitle           = s.JobTitle,
                Email            = s.Email,
                PhoneNumber      = s.PhoneNumber,
                OrganisationName = s.OrganisationName,
                Department       = s.Department,
                OrgTypeId        = (int)s.OrgType,
                OrgTypeOther     = s.OrgTypeOther,
                TownOrCity       = s.TownOrCity,
                AddressLineOne   = s.AddressLineOne,
                AddressLineTwo   = s.AddressLineTwo,
                Postcode         = s.Postcode,
                TellUsMore       = s.TellUsMore,
            };

            respVal.NeedPpeTypes = s.PpeTypes.Where(nt => nt.Selected).Select(nt => NeedPpeType.Create_FromViewModel(nt, respVal)).ToList();
            return(respVal);
        }