OSDN Git Service

UAをVersion/10.0.3 Safari/602.4.8に変更
[kcd/KCD.git] / KCD / BasicMapper.swift
1 //
2 //  BasicMapper.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 fileprivate enum BasicAPI: String {
12     case getMemberBasic = "/kcsapi/api_get_member/basic"
13     case port = "/kcsapi/api_port/port"
14 }
15
16 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
17     guard let basicApi = BasicAPI(rawValue: apiResponse.api)
18         else { return ["api_data"] }
19     switch basicApi {
20     case .port: return ["api_data", "api_basic"]
21     default: return ["api_data"]
22     }
23 }
24
25 class BasicMapper: JSONMapper {
26     typealias ObjectType = Basic
27     
28     let apiResponse: APIResponse
29     let configuration: MappingConfiguration<Basic>
30     
31     required init(_ apiResponse: APIResponse) {
32         self.apiResponse = apiResponse
33         self.configuration = MappingConfiguration(entity: Basic.entity,
34                                                   dataKeys: dataKeys(apiResponse),
35                                                   editorStore: ServerDataStore.oneTimeEditor())
36     }
37     
38     func commit() {
39         let store = ServerDataStore.oneTimeEditor()
40         guard let basic = store.basic() ?? store.createBasic()
41             else { return print("Can not Get Basic") }
42         registerElement(data, to: basic)
43     }
44 }