public IActionResult GetSubscription(string subscriptionID)
        {
            IActionResult result;
            Guid          subscriptionIDGuid;

            if (StringUtils.GuidTryDecode(subscriptionID, out subscriptionIDGuid))
            {
                Model.Subscription subscription = BusinessLogicFactory.Subscriptions.GetSubscription(subscriptionIDGuid);
                if (subscription == null)
                {
                    result = new NotFoundResult();
                }
                else
                {
                    ServiceModels.Subscription response = new ServiceModels.Subscription(subscription);
                    string rootUrl = Request.GetRootUrl();
                    response.AddSelfLink(string.Concat(rootUrl, "/subscriptions/", StringUtils.GuidEncode(subscription.SubscriptionID)), true, true);
                    result = Request.GetObjectResult(response);
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
        public IActionResult GetSubscriptions(string clientID, string definitionID, string instanceID)
        {
            Guid clientIDGuid = Guid.Empty;
            Guid definitionIDGuid = Guid.Empty;
            IActionResult result = null;

            if (clientID != null && !StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                result = new BadRequestResult();
            }
            else if (clientID != null && BusinessLogicFactory.Clients.GetClient(clientIDGuid) == null)
            {
                result = new NotFoundResult();
            }
            else if (definitionID != null && !StringUtils.GuidTryDecode(definitionID, out definitionIDGuid))
            {
                result = new BadRequestResult();
            }
            else if (definitionID != null && BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(User.GetOrganisationID(), definitionIDGuid) == null)
            {
                result = new NotFoundResult();
            }
            else
            {
                string rootUrl = Request.GetRootUrl();
                if (clientID != null)
                    rootUrl = string.Concat(rootUrl, "/clients/", clientID);
                if (definitionID != null)
                    rootUrl = string.Concat(rootUrl, "/objecttypes/", definitionID);
                if (instanceID != null)
                    rootUrl = string.Concat(rootUrl, "/instances/", instanceID);

                ServiceModels.Subscriptions response = new ServiceModels.Subscriptions();
                response.AddLink("add", string.Concat(rootUrl, "/subscriptions"), null);

                List<Model.Subscription> subscriptions = null;
                if (clientID == null)
                {
                    int organisationID = User.GetOrganisationID();
                    subscriptions = BusinessLogicFactory.Subscriptions.GetSubscriptions(organisationID);
                }
                else
                {
                    List<Model.Subscription> unfilteredSubscriptions = BusinessLogicFactory.Subscriptions.GetSubscriptions(clientIDGuid);

                    if (definitionID != null)
                    {
                        subscriptions = new List<Model.Subscription>();

                        foreach (Model.Subscription subscription in unfilteredSubscriptions)
                        {
                            if (definitionID == null || definitionIDGuid == subscription.ObjectDefinitionID)
                            {
                                if (instanceID == null || instanceID.Equals(subscription.ObjectID))
                                {
                                    subscriptions.Add(subscription);
                                }
                            }
                        }
                    }
                    else
                    {
                        subscriptions = unfilteredSubscriptions;
                    }
                }

                response.PageInfo = Request.GetPageInfo(subscriptions.Count);
                int endIndex = response.PageInfo.StartIndex + response.PageInfo.ItemsCount;
                for (int index = response.PageInfo.StartIndex; index < endIndex; index++)
                {
                    ServiceModels.Subscription subscription = new ServiceModels.Subscription(subscriptions[index]);
                    subscription.AddSelfLink(string.Concat(Request.GetRootUrl(), "/subscriptions/", StringUtils.GuidEncode(subscriptions[index].SubscriptionID)), true, true);
                    response.Add(subscription);
                }
                result = Request.GetObjectResult(response);
            }
            return result;
        }
 public IActionResult GetSubscription(string subscriptionID)
 {
     IActionResult result;
     Guid subscriptionIDGuid;
     if (StringUtils.GuidTryDecode(subscriptionID, out subscriptionIDGuid))
     {
         Model.Subscription subscription = BusinessLogicFactory.Subscriptions.GetSubscription(subscriptionIDGuid);
         if (subscription == null)
             result = new NotFoundResult();
         else
         {
             ServiceModels.Subscription response = new ServiceModels.Subscription(subscription);
             string rootUrl = Request.GetRootUrl();
             response.AddSelfLink(string.Concat(rootUrl, "/subscriptions/", StringUtils.GuidEncode(subscription.SubscriptionID)), true, true);
             result = Request.GetObjectResult(response);
         }
     }
     else
     {
         result = new BadRequestResult();
     }
     return result;
 }
        public IActionResult GetSubscriptions(string clientID, string definitionID, string instanceID)
        {
            Guid          clientIDGuid     = Guid.Empty;
            Guid          definitionIDGuid = Guid.Empty;
            IActionResult result           = null;

            if (clientID != null && !StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                result = new BadRequestResult();
            }
            else if (clientID != null && BusinessLogicFactory.Clients.GetClient(clientIDGuid) == null)
            {
                result = new NotFoundResult();
            }
            else if (definitionID != null && !StringUtils.GuidTryDecode(definitionID, out definitionIDGuid))
            {
                result = new BadRequestResult();
            }
            else if (definitionID != null && BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(User.GetOrganisationID(), definitionIDGuid) == null)
            {
                result = new NotFoundResult();
            }
            else
            {
                string rootUrl = Request.GetRootUrl();
                if (clientID != null)
                {
                    rootUrl = string.Concat(rootUrl, "/clients/", clientID);
                }
                if (definitionID != null)
                {
                    rootUrl = string.Concat(rootUrl, "/objecttypes/", definitionID);
                }
                if (instanceID != null)
                {
                    rootUrl = string.Concat(rootUrl, "/instances/", instanceID);
                }

                ServiceModels.Subscriptions response = new ServiceModels.Subscriptions();
                response.AddLink("add", string.Concat(rootUrl, "/subscriptions"), null);


                List <Model.Subscription> subscriptions = null;
                if (clientID == null)
                {
                    int organisationID = User.GetOrganisationID();
                    subscriptions = BusinessLogicFactory.Subscriptions.GetSubscriptions(organisationID);
                }
                else
                {
                    List <Model.Subscription> unfilteredSubscriptions = BusinessLogicFactory.Subscriptions.GetSubscriptions(clientIDGuid);

                    if (definitionID != null)
                    {
                        subscriptions = new List <Model.Subscription>();

                        foreach (Model.Subscription subscription in unfilteredSubscriptions)
                        {
                            if (definitionID == null || definitionIDGuid == subscription.ObjectDefinitionID)
                            {
                                if (instanceID == null || instanceID.Equals(subscription.ObjectID))
                                {
                                    subscriptions.Add(subscription);
                                }
                            }
                        }
                    }
                    else
                    {
                        subscriptions = unfilteredSubscriptions;
                    }
                }

                response.PageInfo = Request.GetPageInfo(subscriptions.Count);
                int endIndex = response.PageInfo.StartIndex + response.PageInfo.ItemsCount;
                for (int index = response.PageInfo.StartIndex; index < endIndex; index++)
                {
                    ServiceModels.Subscription subscription = new ServiceModels.Subscription(subscriptions[index]);
                    subscription.AddSelfLink(string.Concat(Request.GetRootUrl(), "/subscriptions/", StringUtils.GuidEncode(subscriptions[index].SubscriptionID)), true, true);
                    response.Add(subscription);
                }
                result = Request.GetObjectResult(response);
            }
            return(result);
        }