public TaskResult<bool> DeleteById(int id)
        {
            TaskResult<bool> result = null;
            try
            {
                if(id!=0)
                {
                    User findedItem = _userRepository.GetSingle(a => a.Id == id);
                    if(findedItem!=null)
                    {
                        _userRepository.Delete(findedItem);
                        _userRepository.SaveChanges();
                        result = new TaskResult<bool> { Data = true };
                    }
                    else
                    {
                        throw new Exception();
                    }

                }
                else
                {
                    throw new Exception();
                }

            }
            catch(Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<bool> { Data = false };
            }
            return result;
        }
 public TaskResult<bool> DeleteById(int  id)
 {
     TaskResult<bool> result = null;
      try
      {
          _storyRepository.Delete(_storyRepository.GetSingle(a => a.Id == id));
          _storyRepository.SaveChanges();
          result = new TaskResult<bool> { Data = true, state = StatusState.DoneState };
      }
      catch(Exception e)
      {
          Logger.Instance.LogException(e);
          result = new TaskResult<bool> { Data = false, state = StatusState.CancelState };
      }
      return result;
 }
 public TaskResult<UserDetail> GetUserDetails(int userId)
 {
     TaskResult<UserDetail> result = null;
     try
     {
        User uentity=  _userRepository.GetSingle(u => u.Id == userId);
        UserDetail detail = new UserDetail { Id = uentity.Id, USerName = uentity.UserName };
        result = new TaskResult<UserDetail>() { Data = detail, state = StatusState.DoneState };
     }
     catch(Exception e)
     {
         Logger.Instance.LogException(e);
         result = new TaskResult<UserDetail>() { Data = null, state = StatusState.CancelState };
     }
     return result;
 }
        public TaskResult<IEnumerable<int>> GetUserGroups(int id)
        {
            TaskResult<IEnumerable<int>> result = null;
            HttpClient client = GetHTTPClient();
            HttpResponseMessage response = client.PostAsync("api/Group/GetGroupsByUser/"+id, null).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<IEnumerable<int>>>().Result;

            }
            else
            {
                result = new TaskResult<IEnumerable<int>> { state = StatusState.CancelState, Data = null };
            }
            return result;
        }
        public TaskResult<IEnumerable<StoryDetail>> GetStoriesForGroupByRequest(int groupId, DataRequest request)
        {
            TaskResult < IEnumerable < StoryDetail >> result = null;
               try
               {
               result = new TaskResult<IEnumerable<StoryDetail>> { state = StatusState.DoneState };

               IEnumerable<Story>  stories= _storyRepository.GetStoriesForGroup((int)groupId, request);
               result.Data = stories.Select(a => new StoryDetail { Content = a.Content, Description = a.Description, GroupDetails = a.Groups.Select(g => new GroupDetail { Description=g.Description,Id=g.Id,Name=g.Name}), Id = a.Id, PostedOn = a.DateCreated.Value, Title = a.Title, UserId = a.UserId }).AsEnumerable<StoryDetail>();
               }
               catch(Exception e)
               {
               Logger.Instance.LogException(e);
               result = new TaskResult<IEnumerable<StoryDetail>> { Data = null, state = StatusState.CancelState };
               }
               return result;
        }
        public TaskResult<IEnumerable<GroupDetail>> GetGroupsWithDetail(DataRequest request)
        {
            TaskResult<IEnumerable<GroupDetail>> result = null;
            try
            {

                result = new TaskResult<IEnumerable<GroupDetail>> { state = StatusState.DoneState };
                IEnumerable<GroupDetail> groups = _groupRepository.GetGroupsWithDetailsByRequest(request);
                result.Data = groups;
            }
            catch (Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<IEnumerable<GroupDetail>> { Data = null, state = StatusState.CancelState };
            }
            return result;
        }
        public TaskResult<IEnumerable<GroupDetail>> GetGroupsByRequest(DataRequest request)
        {
            TaskResult<IEnumerable<GroupDetail>> result = null;
            try
            {
                result = new TaskResult<IEnumerable<GroupDetail>> { state = StatusState.DoneState };

                IEnumerable<Group> groups = _groupRepository.GetGroupsByRequest(request);

                result.Data = groups.Select(a => new GroupDetail { Description = a.Description, Id = a.Id, Name = a.Name }).AsEnumerable<GroupDetail>();
            }
            catch (Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<IEnumerable<GroupDetail>> { Data = null, state = StatusState.CancelState };
            }
            return result;
        }
        public TaskResult<IEnumerable<GroupDetail>> GetGroupsWithDetails(DataRequest request)
        {
            TaskResult<IEnumerable<GroupDetail>> result = null;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(DataRequest), request, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/Group/GetGroupsWithDetails", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<IEnumerable<GroupDetail>>>().Result;

            }
            else
            {
                result = new TaskResult<IEnumerable<GroupDetail>> { state = StatusState.CancelState, Data = null };
            }
            return result;
        }
        public TaskResult<bool> SaveUser(UserDetail user)
        {
            TaskResult<bool> result;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(UserDetail), user, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/User/Save", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<bool>>().Result;

            }
            else
            {
                result = new TaskResult<bool> { state = StatusState.CancelState, Data = false };
            }
            return result;
        }
        public TaskResult<StoryDetail> GetById(int id)
        {
            TaskResult<StoryDetail> result;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(int), id, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/Story/GetById", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<StoryDetail>>().Result;

            }
            else
            {
                result = new TaskResult<StoryDetail> { state = StatusState.CancelState, Data = null };
            }
            return result;
        }
        public TaskResult<UserDetail> GetUserDetail(string name)
        {
            TaskResult<UserDetail> result;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(string), name, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/User/GetDetailByName", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<UserDetail>>().Result;

            }
            else
            {
                result = new TaskResult<UserDetail> { state = StatusState.CancelState, Data = null };
            }
            return result;
        }
        public TaskResult<ICollection<StoryDetail>> GetStoriesByGroup(int GroupId, DataRequest request)
        {
            TaskResult<ICollection<StoryDetail>> result = null;
            HttpClient client = GetHTTPClient();
            var wrappedRequest= new RequestWrapper { Id = GroupId, request = request };
            var content = new ObjectContent(typeof(RequestWrapper), wrappedRequest, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/Story/GetStoriesByGroup", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<ICollection<StoryDetail>>>().Result;

            }
            else
            {
                result = new TaskResult<ICollection<StoryDetail>> { state = StatusState.CancelState, Data = null };
            }
            return result;
        }
        public TaskResult<StoryDetail> GetById(int  id)
        {
            TaskResult<StoryDetail> result = null;
            try
            {
                Story story=  _storyRepository.GetById(id);
                if(story!=null)
                {
                    result = new TaskResult<StoryDetail> { Data = new StoryDetail { Content = story.Content, Description = story.Description, GroupDetails = story.Groups.Select(g => new GroupDetail { Id=g.Id,Name=g.Name}),Id=story.Id,PostedOn=story.DateCreated.Value,Title=story.Title,UserId=story.UserId }, state = StatusState.DoneState };
                }
                else
                {
                    result = new TaskResult<StoryDetail> { Data = null, state = StatusState.DoneState };
                }

            }
            catch (Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<StoryDetail> { Data = null, state = StatusState.CancelState };
            }
            return result;
        }
 public TaskResult<IEnumerable<int>> GetUserGroups(int userId)
 {
     TaskResult<IEnumerable<int>> result = null;
     try
     {
         var data = _groupRepository.GetGroupsByUserId(userId).Select(a => a.Id);
         result = new TaskResult<IEnumerable<int>> { state = StatusState.DoneState,Data=data };
     }
     catch (Exception e)
     {
         Logger.Instance.LogException(e);
         result = new TaskResult<IEnumerable<int>> {Data=null, state = StatusState.CancelState };
     }
     return result;
 }
        TaskResult<bool> IStoryDataService.PostStory(StoryDetail story)
        {
            TaskResult<bool> result;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(StoryDetail), story, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/Story/PostStory", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<bool>>().Result;

            }
            else
            {
                result = new TaskResult<bool> { state = StatusState.CancelState, Data = false };
            }
            return result;
        }
        TaskResult<bool> IGroupDataService.CreateGroup(GroupDetail group)
        {
            TaskResult<bool> result = null;
            HttpClient client = GetHTTPClient();
            var content = new ObjectContent(typeof(GroupDetail), group, new JsonMediaTypeFormatter());
            HttpResponseMessage response = client.PostAsync("api/Group/CreateGroup", content).Result;

            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsAsync<TaskResult<bool>>().Result;

            }
            else
            {
                result = new TaskResult<bool> { state = StatusState.CancelState, Data = false };
            }
            return result;
        }
        public TaskResult<bool> SaveStory(StoryDetail story)
        {
            TaskResult<bool> result = null;
            try
            {

                if (story.Id == null)//new
                {
                    Story storyEntity = new Story { Content = story.Content, DateCreated = DateTime.Now, Description = story.Description, Title = story.Title, UserId = story.UserId.Value };
                    _storyRepository.Insert(storyEntity);
                    //_storyRepository.SaveChanges();
                    _storyRepository.AddStoryToGroups(storyEntity, story.GroupDetails.Select(a => a.Id.Value).AsEnumerable());
                    _storyRepository.SaveChanges();
                }
                else
                {
                    Story _story = _storyRepository.GetSingle(a => a.Id == story.Id);
                    _story.Description = story.Description;
                    _story.Title = story.Title;
                    _story.Content = story.Content;
                    _story.DateModified = DateTime.Now;
                    IEnumerable<Group> groups = _story.Groups.ToList();
                    foreach(var gr in groups)
                    {
                        _story.Groups.Remove(gr);
                    }
                    _storyRepository.Update(_story);
                    _storyRepository.SaveChanges();

                    _storyRepository.AddStoryToGroups(_story, story.GroupDetails.Select(a => a.Id.Value).AsEnumerable());
                    _storyRepository.SaveChanges();
                }
                result = new TaskResult<bool> { Data = true, state = StatusState.DoneState };
            }
            catch(Exception e)
            {
                result = new TaskResult<bool> { Data = false, state = StatusState.CancelState };
            }
            return result;
        }
 public TaskResult<bool> JoinGroup(JoinDetail jdetail)
 {
     TaskResult<bool> result = null;
     try
     {
         _groupRepository.JoinGroup(jdetail);
         _groupRepository.SaveChanges();
         result = new TaskResult<bool> { Data = true, state = StatusState.DoneState };
     }
     catch (Exception e)
     {
         Logger.Instance.LogException(e);
         result = new TaskResult<bool> { Data = false, state = StatusState.CancelState };
     }
     return result;
 }
 public TaskResult<bool> SaveGroup(GroupDetail group)
 {
     TaskResult<bool> result= null;
     try
     {
         if(group.Id==null)//insert
         {
             _groupRepository.Insert(new Group { DateCreated = DateTime.Now, Description = group.Description, Name = group.Name });
             _groupRepository.SaveChanges();
             result= new TaskResult<bool>{Data=true,state=StatusState.DoneState};
         }
     }
     catch(Exception e)
     {
         Logger.Instance.LogException(e);
         result= new TaskResult<bool>{Data=false,state=StatusState.CancelState};
     }
     return result;
 }
        public TaskResult<UserDetail> Validate(UserDetail user)
        {
            TaskResult<UserDetail> result;
            try
            {
                var _user = _userRepository.GetSingle((x) => x.UserName == user.USerName && x.Password == user.Password);
                result = new TaskResult<UserDetail> { state = StatusState.DoneState, Data = new UserDetail { Id=_user.Id,USerName=_user.UserName} };
            }
            catch(Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<UserDetail> { state = StatusState.CancelState, Data = null };
            }

            return result;
        }
        public TaskResult<bool> SaveUser(UserDetail user)
        {
            TaskResult<bool> result = null ;
            try
            {
                if (user.Id == null)//create
                {
                    _userRepository.Insert(new User { DateCreated = DateTime.Now, Password = user.Password, UserName = user.USerName });
                    _userRepository.SaveChanges();
                    result = new TaskResult<bool> { state = StatusState.DoneState, Data = true };
                }
                else//update
                {

                }
            }
            catch(Exception e)
            {
                Logger.Instance.LogException(e);
                result = new TaskResult<bool> { state = StatusState.DoneState, Data = false };
            }
            return result;
        }