OSDN Git Service

da20e6324a85345fbcef193595c21df51aa75b1b
[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     
13     case getMemberBasic = "/kcsapi/api_get_member/basic"
14     case port = "/kcsapi/api_port/port"
15 }
16
17 fileprivate func dataKeys(_ apiResponse: APIResponse) -> [String] {
18     
19     guard let basicApi = BasicAPI(rawValue: apiResponse.api)
20         else { return ["api_data"] }
21     
22     switch basicApi {
23     case .port: return ["api_data", "api_basic"]
24         
25     default: return ["api_data"]
26     }
27 }
28
29 final class BasicMapper: JSONMapper {
30     
31     typealias ObjectType = Basic
32     
33     let apiResponse: APIResponse
34     let configuration: MappingConfiguration<Basic>
35     
36     required init(_ apiResponse: APIResponse) {
37         
38         self.apiResponse = apiResponse
39         self.configuration = MappingConfiguration(entity: Basic.entity,
40                                                   dataKeys: dataKeys(apiResponse),
41                                                   editorStore: ServerDataStore.oneTimeEditor())
42     }
43     
44     func commit() {
45         
46         let store = ServerDataStore.oneTimeEditor()
47         
48         guard let basic = store.basic() ?? store.createBasic()
49             else { return print("Can not Get Basic") }
50         
51         registerElement(data, to: basic)
52     }
53 }