public HttpResponseMessage Put(int id, ScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
            }

            var scope = config.Scopes.All.SingleOrDefault(x => x.ID == id);
            if (scope == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            var query =
                (from a in this.config.Applications.All
                 from s in a.Scopes
                 where s.ID == id
                 select a);
            var app = query.Single();
            
            if (app.Scopes.Any(x=>x.Name == model.Name && x.ID != id))
            {
                ModelState.AddModelError("", "That Scope name is already in use.");
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
            }

            scope.DisplayName = model.DisplayName;
            scope.Description = model.Description;
            scope.Emphasize = model.Emphasize;

            config.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.NoContent);
        }
        public HttpResponseMessage Post(int id, ScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            var app = config.Applications.All.SingleOrDefault(x => x.ID == id);
            if (app == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            var scope = new Scope();
            scope.Name = model.Name;
            scope.Description = model.Description;
            scope.Emphasize = model.Emphasize;

            app.Scopes.Add(scope);
            config.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK, new {
                    scope.ID,
                    scope.Name,
                    scope.Description,
                    scope.Emphasize
                });
        }
        public HttpResponseMessage Put(int id, ScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            var scope = config.Scopes.All.SingleOrDefault(x => x.ID == id);
            if (scope == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            scope.Description = model.Description;
            scope.Emphasize = model.Emphasize;

            config.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.NoContent);
        }
        public HttpResponseMessage Post(int id, ScopeModel model)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
            }

            var app = _config.Applications.All.SingleOrDefault(x => x.ID == id);
            if (app == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            if (app.Scopes.Any(x => x.Name == model.Name))
            {
                ModelState.AddModelError("", "That Scope name is already in use.");
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.GetErrors());
            }

            var scope = new Scope
                {
                    Name = model.Name,
                    DisplayName = model.DisplayName,
                    Description = model.Description,
                    Emphasize = model.Emphasize
                };

            app.Scopes.Add(scope);
            _config.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK, new {
                    scope.ID,
                    scope.Name,
                    scope.DisplayName,
                    scope.Description,
                    scope.Emphasize
                });
        }