OSDN Git Service

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