changeset 212:f276b175dcaf

delete resources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2012 10:13:49 +0100
parents b7aea293b965
children 4ce7fdcc8879
files Core/RestApi/RestApi.h OrthancServer/OrthancRestApi.cpp OrthancServer/OrthancRestApi2.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 5 files changed, 58 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/Core/RestApi/RestApi.h	Thu Nov 29 09:56:46 2012 +0100
+++ b/Core/RestApi/RestApi.h	Thu Nov 29 10:13:49 2012 +0100
@@ -63,16 +63,6 @@
         return *context_;
       }
     
-      const HttpHandler::Arguments& GetHttpHeaders() const
-      {
-        return *httpHeaders_;
-      }
-
-      const RestApiPath::Components& GetUriComponents() const
-      {
-        return *uriComponents_;
-      }
-
       const UriComponents& GetTrailing() const
       {
         return *trailing_;
@@ -83,6 +73,12 @@
       {
         return HttpHandler::GetArgument(*uriComponents_, name, defaultValue);
       }
+
+      std::string GetHttpHeader(const std::string& name,
+                                const std::string& defaultValue)
+      {
+        return HttpHandler::GetArgument(*httpHeaders_, name, defaultValue);
+      }
     };
 
  
--- a/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 09:56:46 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Thu Nov 29 10:13:49 2012 +0100
@@ -349,14 +349,14 @@
       // This is not a UUID, assume this is a DICOM instance
       c.Store(postData);
     }
-    else if (index_.GetSeries(found, postData))
+    else if (index_.LookupResource(found, postData, ResourceType_Series))
     {
       // The UUID corresponds to a series
       for (Json::Value::ArrayIndex i = 0; i < found["Instances"].size(); i++)
       {
         std::string uuid = found["Instances"][i].asString();
         Json::Value instance(Json::objectValue);
-        if (index_.GetInstance(instance, uuid))
+        if (index_.LookupResource(instance, uuid, ResourceType_Instance))
         {
           std::string content;
           storage_.ReadFile(content, instance["FileUuid"].asString());
@@ -368,7 +368,7 @@
         }
       }
     }
-    else if (index_.GetInstance(found, postData))
+    else if (index_.LookupResource(found, postData, ResourceType_Instance))
     {
       // The UUID corresponds to an instance
       std::string content;
@@ -449,22 +449,22 @@
       {
         if (uri[0] == "patients")
         {
-          existingResource = index_.GetPatient(result, uri[1]);
+          existingResource = index_.LookupResource(result, uri[1], ResourceType_Patient);
           assert(!existingResource || result["Type"] == "Patient");
         }
         else if (uri[0] == "studies")
         {
-          existingResource = index_.GetStudy(result, uri[1]);
+          existingResource = index_.LookupResource(result, uri[1], ResourceType_Study);
           assert(!existingResource || result["Type"] == "Study");
         }
         else if (uri[0] == "series")
         {
-          existingResource = index_.GetSeries(result, uri[1]);
+          existingResource = index_.LookupResource(result, uri[1], ResourceType_Series);
           assert(!existingResource || result["Type"] == "Series");
         }
         else if (uri[0] == "instances")
         {
-          existingResource = index_.GetInstance(result, uri[1]);
+          existingResource = index_.LookupResource(result, uri[1], ResourceType_Instance);
           assert(!existingResource || result["Type"] == "Instance");
         }
       }
@@ -472,19 +472,19 @@
       {
         if (uri[0] == "patients")
         {
-          existingResource = index_.DeletePatient(result, uri[1]);
+          existingResource = index_.DeleteResource(result, uri[1], ResourceType_Patient);
         }
         else if (uri[0] == "studies")
         {
-          existingResource = index_.DeleteStudy(result, uri[1]);
+          existingResource = index_.DeleteResource(result, uri[1], ResourceType_Study);
         }
         else if (uri[0] == "series")
         {
-          existingResource = index_.DeleteSeries(result, uri[1]);
+          existingResource = index_.DeleteResource(result, uri[1], ResourceType_Series);
         }
         else if (uri[0] == "instances")
         {
-          existingResource = index_.DeleteInstance(result, uri[1]);
+          existingResource = index_.DeleteResource(result, uri[1], ResourceType_Instance);
         }
 
         if (existingResource)
@@ -547,7 +547,7 @@
              uri[2] == "frames")
     {
       Json::Value instance(Json::objectValue);
-      existingResource = index_.GetInstance(instance, uri[1]);
+      existingResource = index_.LookupResource(instance, uri[1], ResourceType_Instance);
 
       if (existingResource)
       {
--- a/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 09:56:46 2012 +0100
+++ b/OrthancServer/OrthancRestApi2.cpp	Thu Nov 29 10:13:49 2012 +0100
@@ -80,6 +80,30 @@
     call.GetOutput().AnswerJson(result);
   }
 
+  template <enum ResourceType resourceType>
+  static void GetSingleResource(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    Json::Value result;
+    if (context.GetIndex().LookupResource(result, call.GetUriComponent("id", ""), resourceType))
+    {
+      call.GetOutput().AnswerJson(result);
+    }
+  }
+
+  template <enum ResourceType resourceType>
+  static void DeleteSingleResource(RestApi::DeleteCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    Json::Value result;
+    if (context.GetIndex().DeleteResource(result, call.GetUriComponent("id", ""), resourceType))
+    {
+      call.GetOutput().AnswerJson(result);
+    }
+  }
+
 
   // Changes API --------------------------------------------------------------
  
@@ -153,5 +177,14 @@
     Register("/patients", ListResources<ResourceType_Patient>);
     Register("/series", ListResources<ResourceType_Series>);
     Register("/studies", ListResources<ResourceType_Study>);
+
+    Register("/instances/{id}", DeleteSingleResource<ResourceType_Instance>);
+    Register("/instances/{id}", GetSingleResource<ResourceType_Instance>);
+    Register("/patients/{id}", DeleteSingleResource<ResourceType_Patient>);
+    Register("/patients/{id}", GetSingleResource<ResourceType_Patient>);
+    Register("/series/{id}", DeleteSingleResource<ResourceType_Series>);
+    Register("/series/{id}", GetSingleResource<ResourceType_Series>);
+    Register("/studies/{id}", DeleteSingleResource<ResourceType_Study>);
+    Register("/studies/{id}", GetSingleResource<ResourceType_Study>);
   }
 }
--- a/OrthancServer/ServerIndex.cpp	Thu Nov 29 09:56:46 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Thu Nov 29 10:13:49 2012 +0100
@@ -123,7 +123,7 @@
   }
 
 
-  bool ServerIndex::DeleteInternal(Json::Value& target,
+  bool ServerIndex::DeleteResource(Json::Value& target,
                                    const std::string& uuid,
                                    ResourceType expectedType)
   {
--- a/OrthancServer/ServerIndex.h	Thu Nov 29 09:56:46 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Thu Nov 29 10:13:49 2012 +0100
@@ -62,14 +62,6 @@
     void MainDicomTagsToJson(Json::Value& result,
                              int64_t resourceId);
 
-    bool DeleteInternal(Json::Value& target,
-                        const std::string& uuid,
-                        ResourceType expectedType);
-
-    bool LookupResource(Json::Value& result,
-                        const std::string& publicId,
-                        ResourceType expectedType);
-
     SeriesStatus GetSeriesStatus(int id);
 
   public:
@@ -95,31 +87,9 @@
 
     uint64_t GetTotalUncompressedSize();
 
-    bool GetInstance(Json::Value& result,
-                     const std::string& instanceUuid)
-    {
-      return LookupResource(result, instanceUuid, ResourceType_Instance);
-    }
-
-    bool GetSeries(Json::Value& result,
-                   const std::string& seriesUuid)
-    {
-      return LookupResource(result, seriesUuid, ResourceType_Series);
-    }
-
-
-    bool GetStudy(Json::Value& result,
-                  const std::string& studyUuid)
-    {
-      return LookupResource(result, studyUuid, ResourceType_Study);
-    }
-
-
-    bool GetPatient(Json::Value& result,
-                    const std::string& patientUuid)
-    {
-      return LookupResource(result, patientUuid, ResourceType_Patient);
-    }
+    bool LookupResource(Json::Value& result,
+                        const std::string& publicId,
+                        ResourceType expectedType);
 
     bool GetFile(std::string& fileUuid,
                  CompressionType& compressionType,
@@ -129,29 +99,9 @@
     void GetAllUuids(Json::Value& target,
                      ResourceType resourceType);
 
-    bool DeletePatient(Json::Value& target,
-                       const std::string& patientUuid)
-    {
-      return DeleteInternal(target, patientUuid, ResourceType_Patient);
-    }
-
-    bool DeleteStudy(Json::Value& target,
-                     const std::string& studyUuid)
-    {
-      return DeleteInternal(target, studyUuid, ResourceType_Study);
-    }
-
-    bool DeleteSeries(Json::Value& target,
-                      const std::string& seriesUuid)
-    {
-      return DeleteInternal(target, seriesUuid, ResourceType_Series);
-    }
-
-    bool DeleteInstance(Json::Value& target,
-                        const std::string& instanceUuid)
-    {
-      return DeleteInternal(target, instanceUuid, ResourceType_Instance);
-    }
+    bool DeleteResource(Json::Value& target,
+                        const std::string& uuid,
+                        ResourceType expectedType);
 
     bool GetChanges(Json::Value& target,
                     int64_t since,