public ActionResult Edit()
        {
            GroupsService groupService = new GroupsService();
            GroupEditVM   model        = new GroupEditVM();

            TryUpdateModel(model);
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Group g;

            if (model.ID != 0)
            {
                g = groupService.GetByID(model.ID);
            }
            else
            {
                g = new Group();
            }

            if (g == null)
            {
                return(this.RedirectToAction(c => c.List()));
            }
            Mapper.Map(model, g);


            groupService.Save(g);
            return(this.RedirectToAction(c => c.List()));
        }
        public ActionResult Edit()
        {
            GroupsService groupsService = new GroupsService();
            GroupsEditVM  model         = new GroupsEditVM();

            TryUpdateModel(model);

            Group group;

            if (model.ID == 0)
            {
                group = new Group();
            }
            else
            {
                group = groupsService.GetByID(model.ID);
                if (group == null)
                {
                    return(this.RedirectToAction(c => c.List()));
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Mapper.Map(model, group);
            group.UserID = AuthenticationManager.LoggedUser.ID;

            groupsService.Save(group);

            return(this.RedirectToAction(c => c.List()));
        }
        public ActionResult Edit(int?id)
        {
            GroupsService groupService = new GroupsService();
            GroupEditVM   model        = new GroupEditVM();

            Group group;

            if (id.HasValue)
            {
                group = groupService.GetByID(id.Value);
                if (group == null)
                {
                    return(this.RedirectToAction(c => c.List()));
                }
            }
            else
            {
                group = new Group();
            }

            Mapper.Map(group, model);


            return(View(model));
        }
        public JsonResult Add(int[] contactIDs, int groupID)
        {
            UnitOfWork    unitOfWork    = new UnitOfWork();
            GroupsService groupsService = new GroupsService(unitOfWork);

            if (contactIDs == null)
            {
                contactIDs = new int[0];
            }

            Group group = groupsService.GetByID(groupID);

            group.Contacts.Clear();

            foreach (var id in contactIDs)
            {
                Contact contact = new ContactsService(unitOfWork).GetByID(id);
                group.Contacts.Add(contact);
            }

            groupsService.Save(group);

            var contacts = group.Contacts.Select(c => new
            {
                id        = c.ID,
                firstName = c.FirstName,
                lastName  = c.LastName
            });

            return(Json(contacts, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Remove(int contactID, int groupID)
        {
            UnitOfWork    unitOfWork    = new UnitOfWork();
            GroupsService groupsService = new GroupsService(unitOfWork);

            Group group = groupsService.GetByID(groupID);

            group.Contacts = group.Contacts.Where(c => c.ID != contactID).ToList();

            groupsService.Save(group);

            return(Json(new object[] { new object() }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Delete(int?id)
        {
            if (!id.HasValue)
            {
                return(this.RedirectToAction(c => c.List()));
            }

            UnitOfWork    unitOfWork    = new UnitOfWork();
            GroupsService groupsService = new GroupsService(unitOfWork);

            groupsService.GetByID(id.Value).Contacts.Clear();
            groupsService.Delete(id.Value);

            return(this.RedirectToAction(c => c.List()));
        }
        public JsonResult Remove(int[] contactsID, int groupID)
        {
            UnitOfWork    unit          = new UnitOfWork();
            GroupsService groupsService = new GroupsService(unit);

            Group group = groupsService.GetByID(groupID);

            group.Contacts.Clear();
            foreach (var item in contactsID)
            {
                Contact contact = new ContactsService().GetByID(item);
                group.Contacts.Add(contact);
            }

            groupsService.Save(group);

            return(Json(new object[] { new object() }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult Add(int[] contactsID, int groupID)
        {
            UnitOfWork    unit          = new UnitOfWork();
            GroupsService groupsService = new GroupsService(unit);

            Group group = groupsService.GetByID(groupID);

            group.Contacts.Clear();
            if (contactsID != null)
            {
                foreach (var item in contactsID)
                {
                    Contact contact = new ContactsService(unit).GetByID(item);

                    group.Contacts.Add(contact);
                }
            }
            else
            {
                contactsID = new int[0];
                groupsService.Save(group);
                return(Json(new object[] { new object() }, JsonRequestBehavior.AllowGet));
            }


            groupsService.Save(group);

            var data = group.Contacts.Select(c => new
            {
                id        = c.ID,
                firstName = c.FirstName,
                lastName  = c.LastName
            });


            return(Json(data, JsonRequestBehavior.AllowGet));
        }