|
| 1 | +// |
| 2 | +// SearchClient+Dictionaries.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Vladislav Fitc on 21/01/2021. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +public extension SearchClient { |
| 11 | + |
| 12 | + // MARK: - Save dictionary entries |
| 13 | + |
| 14 | + /** |
| 15 | + Save dictionary entries |
| 16 | + |
| 17 | + - Parameter dictionary: Target dictionary |
| 18 | + - Parameter dictionaryEntries: List of dictionary entries |
| 19 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 20 | + - Parameter completion: Result completion |
| 21 | + - Returns: Launched asynchronous operation |
| 22 | + */ |
| 23 | + @discardableResult func saveDictionaryEntries<D: CustomDictionary>(to dictionary: D.Type, |
| 24 | + dictionaryEntries: [D.Entry], |
| 25 | + requestOptions: RequestOptions? = nil, |
| 26 | + completion: @escaping ResultCallback<WaitableWrapper<DictionaryRevision>>) -> Operation where D.Entry: Encodable { |
| 27 | + let requests = dictionaryEntries.map(DictionaryRequest.add) |
| 28 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 29 | + requests: requests, |
| 30 | + clearExistingDictionaryEntries: false, |
| 31 | + requestOptions: requestOptions) |
| 32 | + return execute(command, completion: completion) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + Save dictionary entries |
| 37 | + |
| 38 | + - Parameter dictionary: Target dictionary |
| 39 | + - Parameter dictionaryEntries: List of dictionary entries |
| 40 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 41 | + - Returns: Response object |
| 42 | + */ |
| 43 | + @discardableResult func saveDictionaryEntries<D: CustomDictionary>(to dictionary: D.Type, |
| 44 | + dictionaryEntries: [D.Entry], |
| 45 | + requestOptions: RequestOptions? = nil) throws -> WaitableWrapper<DictionaryRevision> where D.Entry: Encodable { |
| 46 | + let requests = dictionaryEntries.map(DictionaryRequest.add) |
| 47 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 48 | + requests: requests, |
| 49 | + clearExistingDictionaryEntries: false, |
| 50 | + requestOptions: requestOptions) |
| 51 | + return try execute(command) |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - Replace dictionary entries |
| 55 | + |
| 56 | + /** |
| 57 | + Replace dictionary entries |
| 58 | + |
| 59 | + - Parameter dictionary: Target dictionary |
| 60 | + - Parameter dictionaryEntries: List of dictionary entries |
| 61 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 62 | + - Parameter completion: Result completion |
| 63 | + - Returns: Launched asynchronous operation |
| 64 | + */ |
| 65 | + @discardableResult func replaceDictionaryEntries<D: CustomDictionary>(in dictionary: D.Type, |
| 66 | + with dictionaryEntries: [D.Entry], |
| 67 | + requestOptions: RequestOptions? = nil, |
| 68 | + completion: @escaping ResultCallback<WaitableWrapper<DictionaryRevision>>) -> Operation where D.Entry: Encodable { |
| 69 | + let requests = dictionaryEntries.map(DictionaryRequest.add) |
| 70 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 71 | + requests: requests, |
| 72 | + clearExistingDictionaryEntries: true, |
| 73 | + requestOptions: requestOptions) |
| 74 | + return execute(command, completion: completion) |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + Replace dictionary entries |
| 79 | + |
| 80 | + - Parameter dictionary: Target dictionary |
| 81 | + - Parameter dictionaryEntries: List of dictionary entries |
| 82 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 83 | + - Returns: Response object |
| 84 | + */ |
| 85 | + @discardableResult func replaceDictionaryEntries<D: CustomDictionary>(in dictionary: D.Type, |
| 86 | + with dictionaryEntries: [D.Entry], |
| 87 | + requestOptions: RequestOptions? = nil) throws -> WaitableWrapper<DictionaryRevision> where D.Entry: Encodable { |
| 88 | + let requests = dictionaryEntries.map(DictionaryRequest.add) |
| 89 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 90 | + requests: requests, |
| 91 | + clearExistingDictionaryEntries: true, |
| 92 | + requestOptions: requestOptions) |
| 93 | + return try execute(command) |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: - Delete dictionary entries |
| 97 | + |
| 98 | + /** |
| 99 | + Delete dictionary entries |
| 100 | + |
| 101 | + - Parameter dictionary: Target dictionary |
| 102 | + - Parameter objectIDs: IDs of the objects to delete |
| 103 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 104 | + - Parameter completion: Result completion |
| 105 | + - Returns: Launched asynchronous operation |
| 106 | + */ |
| 107 | + |
| 108 | + @discardableResult func deleteDictionaryEntries<D: CustomDictionary>(from dictionary: D.Type, |
| 109 | + withObjectIDs objectIDs: [ObjectID], |
| 110 | + requestOptions: RequestOptions? = nil, |
| 111 | + completion: @escaping ResultCallback<WaitableWrapper<DictionaryRevision>>) -> Operation where D.Entry: Encodable { |
| 112 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 113 | + requests: objectIDs.map(DictionaryRequest<D.Entry>.deleteEntry(withObjectID:)), |
| 114 | + clearExistingDictionaryEntries: false, |
| 115 | + requestOptions: requestOptions) |
| 116 | + return execute(command, completion: completion) |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + Delete dictionary entries |
| 121 | + |
| 122 | + - Parameter dictionary: Target dictionary |
| 123 | + - Parameter objectIDs: IDs of the objects to delete |
| 124 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 125 | + - Returns: Response object |
| 126 | + */ |
| 127 | + @discardableResult func deleteDictionaryEntries<D: CustomDictionary>(from dictionary: D.Type, |
| 128 | + withObjectIDs objectIDs: [ObjectID], |
| 129 | + requestOptions: RequestOptions? = nil) throws -> WaitableWrapper<DictionaryRevision> where D.Entry: Encodable { |
| 130 | + let command = Command.Dictionaries.Batch(dictionary: D.self, |
| 131 | + requests: objectIDs.map(DictionaryRequest<D.Entry>.deleteEntry(withObjectID:)), |
| 132 | + clearExistingDictionaryEntries: false, |
| 133 | + requestOptions: requestOptions) |
| 134 | + return try execute(command) |
| 135 | + } |
| 136 | + |
| 137 | + // MARK: - Clear dictionary entries |
| 138 | + |
| 139 | + /** |
| 140 | + Clear dictionary entries |
| 141 | + |
| 142 | + - Parameter dictionary: Target dictionary |
| 143 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 144 | + - Parameter completion: Result completion |
| 145 | + - Returns: Launched asynchronous operation |
| 146 | + */ |
| 147 | + |
| 148 | + @discardableResult func clearDictionaryEntries<D: CustomDictionary>(dictionary: D.Type, |
| 149 | + requestOptions: RequestOptions? = nil, |
| 150 | + completion: @escaping ResultCallback<WaitableWrapper<DictionaryRevision>>) -> Operation where D.Entry: Encodable { |
| 151 | + return replaceDictionaryEntries(in: D.self, with: [], requestOptions: requestOptions, completion: completion) |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + Clear dictionary entries |
| 156 | + |
| 157 | + - Parameter dictionary: Target dictionary |
| 158 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 159 | + - Returns: Response object |
| 160 | + */ |
| 161 | + @discardableResult func clearDictionaryEntries<D: CustomDictionary>(dictionary: D.Type, |
| 162 | + requestOptions: RequestOptions? = nil) throws -> WaitableWrapper<DictionaryRevision> where D.Entry: Encodable { |
| 163 | + return try replaceDictionaryEntries(in: D.self, with: [], requestOptions: requestOptions) |
| 164 | + } |
| 165 | + |
| 166 | + // MARK: - Search dictionary entries |
| 167 | + |
| 168 | + /** |
| 169 | + Search dictionary entries |
| 170 | + |
| 171 | + - Parameter dictionary: Target dictionary |
| 172 | + - Parameter query: Search query |
| 173 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 174 | + - Parameter completion: Result completion |
| 175 | + - Returns: Launched asynchronous operation |
| 176 | + */ |
| 177 | + @discardableResult func searchDictionaryEntries<D: CustomDictionary>(in dictionary: D.Type, |
| 178 | + query: DictionaryQuery, |
| 179 | + requestOptions: RequestOptions? = nil, |
| 180 | + completion: @escaping ResultCallback<DictionarySearchResponse<D.Entry>>) -> Operation where D.Entry: Decodable { |
| 181 | + let command = Command.Dictionaries.Search(dictionaryName: D.name, |
| 182 | + query: query, |
| 183 | + requestOptions: requestOptions) |
| 184 | + return execute(command, completion: completion) |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + Search dictionary entries |
| 189 | + |
| 190 | + - Parameter dictionary: Target dictionary |
| 191 | + - Parameter query: Search query |
| 192 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 193 | + - Returns: Response object |
| 194 | + */ |
| 195 | + @discardableResult func searchDictionaryEntries<D: CustomDictionary>(in dictionary: D.Type, |
| 196 | + query: DictionaryQuery, |
| 197 | + requestOptions: RequestOptions? = nil) throws -> DictionarySearchResponse<D.Entry> where D.Entry: Decodable { |
| 198 | + let command = Command.Dictionaries.Search(dictionaryName: D.name, |
| 199 | + query: query, |
| 200 | + requestOptions: requestOptions) |
| 201 | + return try execute(command) |
| 202 | + } |
| 203 | + |
| 204 | + // MARK: - Set dictionary settings |
| 205 | + |
| 206 | + /** |
| 207 | + Set dictionary settings |
| 208 | + |
| 209 | + - Parameter settings: Dictionaries settings |
| 210 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 211 | + - Parameter completion: Result completion |
| 212 | + - Returns: Launched asynchronous operation |
| 213 | + */ |
| 214 | + @discardableResult func setDictionarySettings(_ settings: DictionarySettings, |
| 215 | + requestOptions: RequestOptions? = nil, |
| 216 | + completion: @escaping ResultCallback<WaitableWrapper<AppRevision>>) -> Operation { |
| 217 | + let command = Command.Dictionaries.SetSettings(settings: settings, |
| 218 | + requestOptions: requestOptions) |
| 219 | + return execute(command, completion: completion) |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + Set dictionary settings |
| 224 | + |
| 225 | + - Parameter settings: Dictionaries settings |
| 226 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 227 | + - Returns: Response object |
| 228 | + */ |
| 229 | + @discardableResult func setDictionarySettings(_ settings: DictionarySettings, |
| 230 | + requestOptions: RequestOptions? = nil) throws -> WaitableWrapper<AppRevision> { |
| 231 | + let command = Command.Dictionaries.SetSettings(settings: settings, |
| 232 | + requestOptions: requestOptions) |
| 233 | + return try execute(command) |
| 234 | + } |
| 235 | + |
| 236 | + // MARK: - Get dictionary settings |
| 237 | + |
| 238 | + /** |
| 239 | + Get dictionary settings |
| 240 | + |
| 241 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 242 | + - Parameter completion: Result completion |
| 243 | + - Returns: Launched asynchronous operation |
| 244 | + */ |
| 245 | + @discardableResult func getDictionarySettings(requestOptions: RequestOptions? = nil, |
| 246 | + completion: @escaping ResultCallback<DictionarySettings>) -> Operation { |
| 247 | + let command = Command.Dictionaries.GetSettings(requestOptions: requestOptions) |
| 248 | + return execute(command, completion: completion) |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + Get dictionary settings |
| 253 | + |
| 254 | + - Parameter requestOptions: Configure request locally with RequestOptions |
| 255 | + - Returns: Response object |
| 256 | + */ |
| 257 | + @discardableResult func getDictionarySettings(requestOptions: RequestOptions? = nil) throws -> DictionarySettings { |
| 258 | + let command = Command.Dictionaries.GetSettings(requestOptions: requestOptions) |
| 259 | + return try execute(command) |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments