<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using GiveCRM.Models;
using Simple.Data;

namespace GiveCRM.DataAccess
{
    public class Members
    {
        private readonly dynamic _db = Database.OpenNamedConnection("GiveCRM");

        public Member Get(int id)
        {
            var record = _db.Members.FindById(id);
            Member member = record;
            member.PhoneNumbers = record.PhoneNumbers.ToList<PhoneNumber>();
            member.Donations = record.Donations.ToList<Donation>();
            return member;
        }

        public IEnumerable<Member> All()
        {
            var query = _db.Members.All()
                .Select(_db.Members.Id, _db.Members.Reference, _db.Members.Title, _db.Members.FirstName,
                        _db.Members.LastName, _db.Members.Salutation, _db.Members.EmailAddress,
                        _db.Members.AddressLine1, _db.Members.AddressLine2, _db.Members.City, _db.Members.Region,
                        _db.Members.PostalCode, _db.Members.Country,
                        _db.Members.Archived,
                        _db.Members.PhoneNumbers.Id.As("PhoneNumberId"), _db.Members.PhoneNumbers.PhoneNumberType,
                        _db.Members.PhoneNumbers.Number,
                        _db.Members.Donations.Amount.Sum().As("TotalDonations"))
                .OrderBy(_db.Members.Id);

            Member member = null;

            foreach (dynamic record in query)
            {
                if (member != null && member.Id != record.Id)
                {
                    yield return member;
                    member = null;
                }
                if (member == null)
                {
                    member = record;
                    member.PhoneNumbers = new List<PhoneNumber>();
                }
                
                member.PhoneNumbers.Add(new PhoneNumber { Id = record.PhoneNumberId ?? 0, MemberId = member.Id, Number = record.Number ?? string.Empty, PhoneNumberType = (PhoneNumberType)record.PhoneNumberType });
            }

            if (member != null)
            {
                 yield return member;
            }
        }

        public Member Insert(Member member)
        {
            if (member.PhoneNumbers != null && member.PhoneNumbers.Count > 0)
            {
                return InsertWithPhoneNumbers(member);
            }<commit_msg>Fixed bug where members with no phone numbers would cause Members.All to throw an exception<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using GiveCRM.Models;
using Simple.Data;

namespace GiveCRM.DataAccess
{
    public class Members
    {
        private readonly dynamic _db = Database.OpenNamedConnection("GiveCRM");

        public Member Get(int id)
        {
            var record = _db.Members.FindById(id);
            Member member = record;
            member.PhoneNumbers = record.PhoneNumbers.ToList<PhoneNumber>();
            member.Donations = record.Donations.ToList<Donation>();
            return member;
        }

        public IEnumerable<Member> All()
        {
            var query = _db.Members.All()
                .Select(_db.Members.Id, _db.Members.Reference, _db.Members.Title, _db.Members.FirstName,
                        _db.Members.LastName, _db.Members.Salutation, _db.Members.EmailAddress,
                        _db.Members.AddressLine1, _db.Members.AddressLine2, _db.Members.City, _db.Members.Region,
                        _db.Members.PostalCode, _db.Members.Country,
                        _db.Members.Archived,
                        _db.Members.PhoneNumbers.Id.As("PhoneNumberId"), _db.Members.PhoneNumbers.PhoneNumberType,
                        _db.Members.PhoneNumbers.Number,
                        _db.Members.Donations.Amount.Sum().As("TotalDonations"))
                .OrderBy(_db.Members.Id);

            Member member = null;

            foreach (dynamic record in query)
            {
                if (member != null && member.Id != record.Id)
                {
                    yield return member;
                    member = null;
                }
                if (member == null)
                {
                    member = record;
                    member.PhoneNumbers = new List<PhoneNumber>();
                }

                if (record.PhoneNumberId != null)
                {
                    member.PhoneNumbers.Add(new PhoneNumber { Id = record.PhoneNumberId ?? 0, MemberId = member.Id, Number = record.Number ?? string.Empty, PhoneNumberType = (PhoneNumberType)record.PhoneNumberType });
                }
            }

            if (member != null)
            {
                 yield return member;
            }
        }

        public Member Insert(Member member)
        {
            if (member.PhoneNumbers != null && member.PhoneNumbers.Count > 0)
            {
                return InsertWithPhoneNumbers(member);
            }
{'fraction_non_alphanumeric': 0.09388733519776268, 'fraction_numerical': 0.0015980823012385138, 'mean_word_length': 1.5403348554033485, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3202574', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1217, 'n_words': 249}