Example #1
0
        public ApiResponseBase <int> AddContact([FromBody]  Contact c)
        {
            try
            {
                IUnitofWork uow        = new UnitOfWork();
                int         totalCount = 0;

                if (c.GroupIdsTemp.Length > 0)
                {
                    HashSet <Group> grps = new HashSet <Group>();
                    foreach (string gid  in c.GroupIdsTemp.Split(','))
                    {
                        grps.Add(uow.GroupsRepository.Get(int.Parse(gid)));
                    }
                    c.Groups = grps;
                }

                if (c.ContactId > 0)
                {
                    uow.ContactRepository.Update(c);
                }
                else
                {
                    uow.ContactRepository.Insert(c);
                }

                uow.Save();

                ApiResponseBase <int> resp = new ApiResponseBase <int>
                {
                    AddnlInfo     = c.ContactId.ToString(),
                    PayLoad       = c.ContactId,
                    StatusMessage = "Success",
                    Success       = true
                };
                return(resp);
            }
            catch (Exception ex)
            {
                // TODO - log error
                ApiResponseBase <int> resp = new ApiResponseBase <int>
                {
                    StatusMessage = "Failed with server error.",
                    Success       = false
                };
                return(resp);
            }
        }
Example #2
0
        public ApiResponseBase <List <Contact> > GetAllContacts([FromUri] int page, [FromUri] int pageSize)
        {
            //http://localhost:21395/api/contactsapp/contacts/1/10
            try
            {
                page = page - 1; // kendo gives 1 based
                IUnitofWork    uow        = new UnitOfWork();
                int            totalCount = 0;
                List <Contact> data       = uow.ContactRepository.GetPage(page, pageSize, ref totalCount).ToList();


                foreach (Contact c in data)
                {
                    if (c.Groups.Count > 0)
                    {
                        c.GroupIdsTemp = string.Join(",", c.Groups.Select(g => g.GroupId).ToList());
                    }
                }


                ApiResponseBase <List <Contact> > resp = new ApiResponseBase <List <Contact> >
                {
                    AddnlInfo     = totalCount.ToString(),
                    PayLoad       = data,
                    StatusMessage = "Success",
                    Success       = true
                };
                return(resp);
            }
            catch (Exception ex)
            {
                // TODO - log error
                ApiResponseBase <List <Contact> > resp = new ApiResponseBase <List <Contact> >
                {
                    StatusMessage = "Failed with server error.",
                    Success       = false
                };
                return(resp);
            }
        }