// PUT /api/applications/5
        public HttpResponseMessage Put(String id, ApplicationModels.UpdateApplicationRequest request)
        {
            DomainServices.ApplicationService applicationService = new DomainServices.ApplicationService(_ctx);

            Guid apiKey;

            Guid.TryParse(id, out apiKey);

            if (apiKey == null)
            {
                _logger.Log(LogLevel.Error, String.Format("Exception Updating Application.  ApiKey {0} could not be parsed as a GUID.", id));

                var message = new HttpResponseMessage(HttpStatusCode.BadRequest);
                message.ReasonPhrase = "Invalid ApiKey.  Cannot be parsed to GUID.";

                return(message);
            }

            Application application = null;

            try
            {
                application = applicationService.GetApplication(id);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled Exception Deleting Application.  Application with the API Key {0} not found. {0}", id, ex.Message));

                var message = new HttpResponseMessage(HttpStatusCode.NotFound);
                message.ReasonPhrase = "Application Resource Not Found.";

                return(message);
            }


            try
            {
                application.IsActive        = request.isActive;
                application.ApplicationName = request.name;
                application.Url             = request.url;

                applicationService.UpdateApplication(application);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled Exception updating application {0}. {1}", id, ex.Message));

                var message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                message.ReasonPhrase = ex.Message;

                return(message);
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
        // GET /api/applications/5
        public HttpResponseMessage<ApplicationModels.ApplicationResponse> Get(String id)
        {
            DomainServices.ApplicationService applicationService = new DomainServices.ApplicationService(_ctx);

            Guid apiKey;

            Guid.TryParse(id, out apiKey);

            if (apiKey == null)
            {
                _logger.Log(LogLevel.Error, String.Format("Unable to parse Guid {0}", id));

                return new HttpResponseMessage<ApplicationModels.ApplicationResponse>(HttpStatusCode.BadRequest);
            }

            Application application;

            try
            {
                application = applicationService.GetApplication(apiKey.ToString());
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled exception getting application {0}. {1}", id, ex.Message));

                var response = new HttpResponseMessage<ApplicationModels.ApplicationResponse>(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ex.Message;

                return response;
            }

            if (application == null)
            {
                var response = new HttpResponseMessage<ApplicationModels.ApplicationResponse>(HttpStatusCode.BadRequest);
                response.ReasonPhrase = "Invalid Application";

                return response;
            }

            //TODO: check to make sure passed in id is the calling application

            return new HttpResponseMessage<ApplicationModels.ApplicationResponse>(new ApplicationModels.ApplicationResponse()
            {
                apiKey = application.ApiKey.ToString(),
                id = application.ApiKey.ToString(),
                isActive = application.IsActive,
                url = application.Url
            }, HttpStatusCode.OK);
        }
        // GET /api/applications/5
        public HttpResponseMessage <ApplicationModels.ApplicationResponse> Get(String id)
        {
            DomainServices.ApplicationService applicationService = new DomainServices.ApplicationService(_ctx);

            Guid apiKey;

            Guid.TryParse(id, out apiKey);

            if (apiKey == null)
            {
                _logger.Log(LogLevel.Error, String.Format("Unable to parse Guid {0}", id));

                return(new HttpResponseMessage <ApplicationModels.ApplicationResponse>(HttpStatusCode.BadRequest));
            }

            Application application;

            try
            {
                application = applicationService.GetApplication(apiKey.ToString());
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled exception getting application {0}. {1}", id, ex.Message));

                var response = new HttpResponseMessage <ApplicationModels.ApplicationResponse>(HttpStatusCode.InternalServerError);
                response.ReasonPhrase = ex.Message;

                return(response);
            }

            if (application == null)
            {
                var response = new HttpResponseMessage <ApplicationModels.ApplicationResponse>(HttpStatusCode.BadRequest);
                response.ReasonPhrase = "Invalid Application";

                return(response);
            }

            //TODO: check to make sure passed in id is the calling application

            return(new HttpResponseMessage <ApplicationModels.ApplicationResponse>(new ApplicationModels.ApplicationResponse()
            {
                apiKey = application.ApiKey.ToString(),
                id = application.ApiKey.ToString(),
                isActive = application.IsActive,
                url = application.Url
            }, HttpStatusCode.OK));
        }
        // PUT /api/applications/5
        public HttpResponseMessage Put(String id, ApplicationModels.UpdateApplicationRequest request)
        {
            DomainServices.ApplicationService applicationService = new DomainServices.ApplicationService(_ctx);

            Guid apiKey;

            Guid.TryParse(id, out apiKey);

            if(apiKey == null)
            {
                _logger.Log(LogLevel.Error, String.Format("Exception Updating Application.  ApiKey {0} could not be parsed as a GUID.", id));

                var message = new HttpResponseMessage(HttpStatusCode.BadRequest);
                message.ReasonPhrase = "Invalid ApiKey.  Cannot be parsed to GUID.";

                return message;
            }

            Application application = null;

            try
            {
                application = applicationService.GetApplication(id);
            }
            catch(Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled Exception Deleting Application.  Application with the API Key {0} not found. {0}", id, ex.Message));

                var message = new HttpResponseMessage(HttpStatusCode.NotFound);
                message.ReasonPhrase = "Application Resource Not Found.";

                return message;
            }

            try
            {
                application.IsActive = request.isActive;
                application.ApplicationName = request.name;
                application.Url = request.url;

                applicationService.UpdateApplication(application);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, String.Format("Unhandled Exception updating application {0}. {1}", id, ex.Message));

                var message = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                message.ReasonPhrase = ex.Message;

                return message;
            }

            return new HttpResponseMessage(HttpStatusCode.OK);
        }