private ClubMemberModel MapClubMemberModel(dynamic item)
        {
            if (item != null && item.Member != null)
            {

                var model = new ClubMemberModel
                {
                    MemberId = (int) item.Member.MemberId
                };
                return model;
            }
          
                return null;
        }
        public ActionResult SaveMember(ClubMemberModel model, FormCollection form)
        {

            model.Wards = new List<Ward>();
            foreach (var key in form.AllKeys)
            {
                if (key.IndexOf("ward", StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    UpdateWardList(model.Wards, form, key);
                }
            }

            _memberServices.SaveMember(model);

            return Redirect("/club/admin/membership");
        }
        private void SetContact(ISession session, Member member, ClubMemberModel model)
        {
            var contactKey = CustomStringHelper.BuildKey(new[] { model.Email });

            var contact = session.CreateCriteria(typeof(Contact))
                    .List<Contact>()
                    .FirstOrDefault(x => x.ContactKey.Equals(contactKey));

            if (contact == null)
            {
                contact = new Contact
                {
                    ContactKey = contactKey                   
                };              
            }

            contact.Email = model.Email;
            contact.Phone = model.Phone;

            SetAudit(contact);
            session.SaveOrUpdate(contact);

            member.Contact = contact;
            SetAudit(member);
            session.SaveOrUpdate(member);
        }
        private void SetAddress(ISession session, Member member, ClubMemberModel model)
        {
            var addressKey = CustomStringHelper.BuildKey(new[] { model.AddressLine, model.Postcode });

            var address = session.CreateCriteria(typeof(Address))
                    .List<Address>()
                    .FirstOrDefault(x => x.AddressKey.Equals(addressKey));

            if (address == null)
            {
                address = new Address
                {
                    AddressKey = addressKey                   
                };              
            }

            address.AddressLine = model.AddressLine;
            address.Postcode = model.Postcode;
            SetAudit(address);
            session.SaveOrUpdate(address);

            member.Address = address;
            SetAudit(member);
            session.SaveOrUpdate(member);
        }
        private void SetName(ISession session, Member member, ClubMemberModel model)
        {
            var memberKey = CustomStringHelper.BuildKey(new[] { model.Lastname, model.Firstname });

            member.MemberKey = memberKey;
            member.Lastname = model.Lastname;
            member.Firstname = model.Firstname;

            SetAudit(member);
            session.SaveOrUpdate(member);
        }
        public int SaveMember(ClubMemberModel model)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        // get member
                        var member = session.CreateCriteria(typeof (Member))
                            .List<Member>().FirstOrDefault(x => x.Id.Equals(model.MemberId)) ?? new Member();

                        SetName(session, member, model);

                        // deal accordingly with what sort (type) of member they are
                        foreach (var memberType in member.Types)
                        {
                            switch (memberType.Name)
                            {
                                case "Guardian":
                                    // update wards
                                    var guardian = session.CreateCriteria(typeof (Guardian))
                                        .List<Guardian>().FirstOrDefault(x => x.Member.Id.Equals(model.MemberId));
                                    
                                    // remove current ward list
                                    if (guardian != null)
                                    {
                                        SetAddress(session, member, model);
                                        SetContact(session, member, model);                                        
                                        SetWards(session, guardian, model.Wards);                                       

                                        SetAudit(guardian);
                                        session.SaveOrUpdate(guardian);
                                    }

                                    break;
                                case "Player":
                                    var player = session.CreateCriteria(typeof(Player))
                                           .List<Player>()
                                           .FirstOrDefault(x => x.Member.Id.Equals(member.Id));

                                    if (player != null)
                                    {
                                        // junior or senior
                                        if (player.PlayerType == (int) PlayerType.Senior)
                                        {
                                            SetAddress(session, member, model);
                                            SetContact(session, member, model);
                                        }

                                        // set nickname
                                        player.Nickname = model.Nickname;
                                        SetAudit(player);
                                        session.SaveOrUpdate(player);
                                    }
                                    break;
                                case "Official":
                                    break;
                            }
                        }
                        transaction.Commit();
                        return member.Id;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, ex, string.Empty, null);
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }