ViewModel for the Person class
Ejemplo n.º 1
0
        public static PersonViewModel Create(IRepository repository, IFirmService firmService, string site, Seminar seminar = null, Person person = null, string email = null, Firm firm = null)
        {
            Check.Require(repository != null, "Repository must be supplied");

            var viewModel = new PersonViewModel()
            {
                Person = person ?? new Person(),
                Addresses = repository.OfType<AddressType>().Queryable.Select(a => new Address() { AddressType = a}).ToList(),
                Contacts = repository.OfType<ContactType>().Queryable.Select( a => new Contact(){ContactType = a}).ToList(),
                Countries = repository.OfType<Country>().GetAll(),
                CommunicationOptions = repository.OfType<CommunicationOption>().GetAll(),
                SeminarPerson = person != null ? person.GetLatestRegistration(site) : null,
                Email = email,
                Seminar = seminar,
                Commodities = repository.OfType<Commodity>().Queryable.Where(a=>a.IsActive).ToList(),
                Firm = firm ?? new Firm()
            };

            if (firm == null && viewModel.SeminarPerson != null)
            {
                viewModel.Firm = viewModel.SeminarPerson.Firm;
            }

            // find any addresses and replace them into the list
            if (person != null)
            {
                foreach(var a in person.Addresses)
                {
                    var addr = viewModel.Addresses.Where(b => b.AddressType == a.AddressType).FirstOrDefault();

                    if (addr != null) viewModel.Addresses.Remove(addr);

                    viewModel.Addresses.Add(a);
                }

                foreach (var a in person.Contacts)
                {
                    var ct = viewModel.Contacts.Where(b => b.ContactType == a.ContactType).FirstOrDefault();

                    if (ct != null) viewModel.Contacts.Remove(ct);

                    viewModel.Contacts.Add(a);
                }
            }

            // reorder so always in the same order
            viewModel.Addresses = viewModel.Addresses.OrderBy(a => a.AddressType.Id).ToList();
            viewModel.Contacts = viewModel.Contacts.OrderBy(a => a.ContactType.Id).ToList();

            // get the firms and add the "Other" option
            var firms = new List<Firm>(firmService.GetAllFirms());
            firms.Add(new Firm() { Name = "Other (Not Listed)" });

            viewModel.Firms = firms;
            if (viewModel.Person.User != null) viewModel.UserName = viewModel.Person.User.UserName;

            return viewModel;
        }
Ejemplo n.º 2
0
        public static PersonViewModel Create(IRepository repository, IFirmService firmService, string site, Seminar seminar = null, Person person = null, string email = null, Firm firm = null)
        {
            Check.Require(repository != null, "Repository must be supplied");

            var viewModel = new PersonViewModel()
            {
                Person    = person ?? new Person(),
                Addresses = repository.OfType <AddressType>().Queryable.Select(a => new Address()
                {
                    AddressType = a
                }).ToList(),
                Contacts = repository.OfType <ContactType>().Queryable.Select(a => new Contact()
                {
                    ContactType = a
                }).ToList(),
                Countries                                         = repository.OfType <Country>().GetAll(),
                CommunicationOptions                              = repository.OfType <CommunicationOption>().GetAll(),
                SeminarPerson                                     = person != null?person.GetLatestRegistration(site) : null,
                                                      Email       = email,
                                                      Seminar     = seminar,
                                                      Commodities = repository.OfType <Commodity>().Queryable.Where(a => a.IsActive).ToList(),
                                                      Firm        = firm ?? new Firm()
            };

            if (firm == null && viewModel.SeminarPerson != null)
            {
                viewModel.Firm = viewModel.SeminarPerson.Firm;
            }

            // find any addresses and replace them into the list
            if (person != null)
            {
                foreach (var a in person.Addresses)
                {
                    var addr = viewModel.Addresses.Where(b => b.AddressType == a.AddressType).FirstOrDefault();

                    if (addr != null)
                    {
                        viewModel.Addresses.Remove(addr);
                    }

                    viewModel.Addresses.Add(a);
                }

                foreach (var a in person.Contacts)
                {
                    var ct = viewModel.Contacts.Where(b => b.ContactType == a.ContactType).FirstOrDefault();

                    if (ct != null)
                    {
                        viewModel.Contacts.Remove(ct);
                    }

                    viewModel.Contacts.Add(a);
                }
            }

            // reorder so always in the same order
            viewModel.Addresses = viewModel.Addresses.OrderBy(a => a.AddressType.Id).ToList();
            viewModel.Contacts  = viewModel.Contacts.OrderBy(a => a.ContactType.Id).ToList();

            // get the firms and add the "Other" option
            var firms = new List <Firm>(firmService.GetAllFirms());

            firms.Add(new Firm()
            {
                Name = "Other (Not Listed)"
            });

            viewModel.Firms = firms;
            if (viewModel.Person.User != null)
            {
                viewModel.UserName = viewModel.Person.User.UserName;
            }

            return(viewModel);
        }