Skip to content

Instantly share code, notes, and snippets.

@godrm
Last active May 2, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godrm/4b42add78f196e9c283509b6300fa841 to your computer and use it in GitHub Desktop.
Save godrm/4b42add78f196e9c283509b6300fa841 to your computer and use it in GitHub Desktop.
protocol CustomerRepositoryProtocol {
}
protocol ProductRepositoryProtocol {
}
struct ConversionRate {
var fromCurrency : String
var toCurrency : String
var ratio = 0.0
}
protocol ForeignExchangeProtocol {
func conversionRates() -> [ConversionRate]
}
protocol RequestHandlerProtocol {
associatedtype command
func handle(request : command, canCancel : Bool)
}
class AddCustomerOrderCommandHandler : RequestHandlerProtocol {
typealias command = AddCustomerOrderCommand
private (set) var customerRepository : CustomerRepositoryProtocol
private (set) var productRepositoty : ProductRepositoryProtocol
private (set) var foreignExchange : ForeignExchangeProtocol
init(customerRepository : CustomerRepositoryProtocol,
productRepositoty : ProductRepositoryProtocol,
foreignExchange : ForeignExchangeProtocol) {
self.customerRepository = customerRepository
self.productRepositoty = productRepositoty
self.foreignExchange = foreignExchange
}
func handle(request: AddCustomerOrderCommand, canCancel: Bool) {
//
}
}
struct ConversionRatesCache {
static let Key = "ConversionRatesCache.Key"
var rates = [ConversionRate]()
var timestamp : Date
init(rates : [ConversionRate]) {
self.rates = rates
self.timestamp = Date()
}
}
protocol CacheStoreDelegate {
func value(for key: String) -> ConversionRatesCache?
func add(value: ConversionRatesCache, for key: String)
}
class ForeignExchangeMock : ForeignExchangeProtocol {
private (set) var cacheStore : CacheStoreDelegate
init(cacheStore : CacheStoreDelegate) {
self.cacheStore = cacheStore
}
func conversionRates() -> [ConversionRate] {
if let ratesCache = cacheStore.value(for: ConversionRatesCache.Key) {
return ratesCache.rates
}
let rates = conversionRatesFromExternal()
cacheStore.add(value: ConversionRatesCache(rates: rates), for: ConversionRatesCache.Key)
return rates
}
private func conversionRatesFromExternal() -> [ConversionRate] {
var conversionRates = [ConversionRate]()
conversionRates.append(ConversionRate(fromCurrency: "USD", toCurrency: "EUR", ratio: 0.88))
conversionRates.append(ConversionRate(fromCurrency: "EUR", toCurrency: "USD", ratio: 1.14))
return conversionRates
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment