OSDN Git Service

Doutaku を 1.0 にアップデート
[kcd/KCD.git] / KCD / BasicMapper.swift
index 90b4441..087f8ab 100644 (file)
@@ -8,41 +8,51 @@
 
 import Cocoa
 
-fileprivate enum BasicAPI: String {
-    case getMemberBasic = "/kcsapi/api_get_member/basic"
-    case port = "/kcsapi/api_port/port"
-}
-
-fileprivate func dataKey(_ apiResponse: APIResponse) -> String {
-    guard let basicApi = BasicAPI(rawValue: apiResponse.api)
-        else { return "api_data" }
-    switch basicApi {
-    case .port: return "api_data.api_basic"
-    default: return "api_data"
-    }
-}
-
-class BasicMapper: JSONMapper {
-    typealias ObjectType = Basic
-    
+final class BasicMapper: JSONMapper {
+        
     let apiResponse: APIResponse
     let configuration: MappingConfiguration<Basic>
     
     required init(_ apiResponse: APIResponse) {
+        
         self.apiResponse = apiResponse
-        self.configuration = MappingConfiguration(entity: Basic.entity,
-                                                  dataKey: dataKey(apiResponse),
+        self.configuration = MappingConfiguration(entity: Basic.self,
+                                                  dataKeys: BasicMapper.dataKeys(apiResponse),
                                                   editorStore: ServerDataStore.oneTimeEditor())
     }
     
+    private class func dataKeys(_ apiResponse: APIResponse) -> [String] {
+        
+        switch apiResponse.api.endpoint {
+            
+        case .port: return ["api_data", "api_basic"]
+            
+        case .basic: return ["api_data"]
+            
+        default:
+            
+            Logger.shared.log("Missing API: \(apiResponse.api)")
+            
+            return ["api_data"]
+            
+        }
+    }
+    
     func commit() {
-        let j = apiResponse.json as NSDictionary
-        guard let data = j.value(forKeyPath: configuration.dataKey) as? [String: Any]
-            else { return print("json is wrong") }
         
-        let store = ServerDataStore.oneTimeEditor()
-        guard let basic = store.basic() ?? store.createBasic()
-            else { return print("Can not Get Basic") }
+        configuration.editorStore.async(execute: commintInContext)
+    }
+    
+    private func commintInContext() {
+        
+        guard let store = configuration.editorStore as? ServerDataStore,
+            let basic = store.basic() ?? store.createBasic() else {
+                
+                Logger.shared.log("Can not Get Basic")
+                
+                return
+        }
+        
         registerElement(data, to: basic)
     }
 }