public async Task<ActionResult> Create(ContactGroupViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            IEnumerable<ContactGroup> records = (await databaseApi.CreateRecordsAsync("contact_group", new List<ContactGroup> { model.ContactGroup }, new SqlQuery())).Records;

            List<ContactContactGroup> relationshipModel = model.Contacts
                .Where(x => x.InGroup)
                .Select(x => new ContactContactGroup
                {
                    ContactId = x.ContactId,
                    ContactGroupId = records.Select(y => y.Id).FirstOrDefault()
                })
                .ToList();

            if (relationshipModel.Count > 0)
            {
                await databaseApi.CreateRecordsAsync("contact_group_relationship", relationshipModel, new SqlQuery());
            }

            return RedirectToAction("List");
        }
        public async Task<ActionResult> Create()
        {
            List<ContactContactGroupViewModel> contacts = (await databaseApi.GetRecordsAsync<Contact>("contact", new SqlQuery()))
                .Records
                .Select(x => new ContactContactGroupViewModel
                {
                    ContactId = x.Id.Value,
                    ContactName = string.Format("{0} {1}", x.FirstName, x.LastName),
                    InGroup = false
                }).ToList();

            ContactGroupViewModel model = new ContactGroupViewModel
            {
                ContactGroup = new ContactGroup(),
                Contacts = contacts
            };

            return View(model);
        }
        public async Task<ActionResult> Edit(int id)
        {
            SqlQuery groupQuery = new SqlQuery
            {
                Filter = "id = " + id,
                Related = "contact_by_contact_group_relationship"
            };

            Task<DatabaseResourceWrapper<Contact>> contactsTask = databaseApi.GetRecordsAsync<Contact>("contact", new SqlQuery());
            Task<DatabaseResourceWrapper<ContactGroup>> contactGroupTask = databaseApi.GetRecordsAsync<ContactGroup>("contact_group", groupQuery);
            await Task.WhenAll(contactsTask, contactGroupTask);

            var contactGroup = contactGroupTask.Result.Records.FirstOrDefault();

            ContactGroupViewModel model = new ContactGroupViewModel
            {
                ContactGroup = contactGroup,
                Contacts = contactsTask.Result.Records
                .Select(x => new ContactContactGroupViewModel
                {
                    ContactId = x.Id.Value,
                    ContactName = string.Format("{0} {1}", x.FirstName, x.LastName),
                    InGroup = contactGroup.Contacts.Any(y => y.Id == x.Id)
                })
                .ToList()
            };

            return View(model);
        }
        public async Task<ActionResult> Edit(ContactGroupViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            SqlQuery contactsInGroupQuery = new SqlQuery
            {
                Filter = "contact_group_id = " + model.ContactGroup.Id
            };

            ContactContactGroup tmp;

            Dictionary<int, ContactContactGroup> relationshipsInDb =
                (await databaseApi.GetRecordsAsync<ContactContactGroup>("contact_group_relationship", contactsInGroupQuery))
                .Records
                .ToDictionary(x => x.ContactId.Value, x => x);
            Dictionary<int, ContactContactGroup> relationshipsInModel =
                model.Contacts
                .Where(x => x.InGroup)
                .ToDictionary(x => x.ContactId, x => new ContactContactGroup
                {
                    ContactGroupId = model.ContactGroup.Id,
                    ContactId = x.ContactId
                });

            List<ContactContactGroup> relationshipsToDelete =
                relationshipsInDb.Values
                .Where(relationship => !relationshipsInModel.TryGetValue(relationship.ContactId.Value, out tmp))
                .ToList();

            List<ContactContactGroup> relationshipsToAdd =
                relationshipsInModel.Values
                .Where(relationship => !relationshipsInDb.TryGetValue(relationship.ContactId.Value, out tmp))
                .ToList();

            List<Task> tasks = new List<Task>();

            tasks.Add(databaseApi.UpdateRecordsAsync("contact_group", new List<ContactGroup> { model.ContactGroup }, new SqlQuery()));

            if (relationshipsToAdd.Count > 0)
            {
                tasks.Add(databaseApi.CreateRecordsAsync("contact_group_relationship", relationshipsToAdd, new SqlQuery()));
            }

            if (relationshipsToDelete.Count > 0)
            {
                tasks.Add(databaseApi.DeleteRecordsAsync("contact_group_relationship", relationshipsToDelete, new SqlQuery()));
            }

            await Task.WhenAll(tasks);

            return RedirectToAction("List");
        }