2017-07-11 05:54:00 +02:00
|
|
|
//
|
|
|
|
// Author+Database.swift
|
|
|
|
// Database
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/8/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-07-12 22:25:10 +02:00
|
|
|
import Data
|
|
|
|
import RSDatabase
|
2017-09-05 02:10:02 +02:00
|
|
|
import RSParser
|
2017-07-11 05:54:00 +02:00
|
|
|
|
2017-09-13 22:29:52 +02:00
|
|
|
// MARK: - DatabaseObject
|
|
|
|
|
2017-09-14 02:40:25 +02:00
|
|
|
extension Author {
|
2017-09-09 21:57:24 +02:00
|
|
|
|
2017-09-13 07:27:36 +02:00
|
|
|
init?(row: FMResultSet) {
|
2017-09-14 02:40:25 +02:00
|
|
|
|
2017-09-13 07:27:36 +02:00
|
|
|
let authorID = row.string(forColumn: DatabaseKey.authorID)
|
2017-09-09 21:57:24 +02:00
|
|
|
let name = row.string(forColumn: DatabaseKey.name)
|
|
|
|
let url = row.string(forColumn: DatabaseKey.url)
|
|
|
|
let avatarURL = row.string(forColumn: DatabaseKey.avatarURL)
|
|
|
|
let emailAddress = row.string(forColumn: DatabaseKey.emailAddress)
|
|
|
|
|
|
|
|
self.init(authorID: authorID, name: name, url: url, avatarURL: avatarURL, emailAddress: emailAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
init?(parsedAuthor: ParsedAuthor) {
|
|
|
|
|
|
|
|
self.init(authorID: nil, name: parsedAuthor.name, url: parsedAuthor.url, avatarURL: parsedAuthor.avatarURL, emailAddress: parsedAuthor.emailAddress)
|
|
|
|
}
|
2017-09-14 06:41:01 +02:00
|
|
|
|
2017-12-02 23:20:58 +01:00
|
|
|
public static func authorsWithParsedAuthors(_ parsedAuthors: Set<ParsedAuthor>?) -> Set<Author>? {
|
2017-09-14 06:41:01 +02:00
|
|
|
|
|
|
|
guard let parsedAuthors = parsedAuthors else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
let authors = Set(parsedAuthors.compactMap { Author(parsedAuthor: $0) })
|
2017-09-14 06:41:01 +02:00
|
|
|
return authors.isEmpty ? nil: authors
|
|
|
|
}
|
2017-09-09 21:57:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 02:40:25 +02:00
|
|
|
extension Author: DatabaseObject {
|
|
|
|
|
|
|
|
public var databaseID: String {
|
2018-02-14 22:14:25 +01:00
|
|
|
return authorID
|
2017-09-14 02:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public func databaseDictionary() -> NSDictionary? {
|
|
|
|
|
2017-09-14 06:41:01 +02:00
|
|
|
let d = NSMutableDictionary()
|
2017-09-14 02:40:25 +02:00
|
|
|
|
2017-09-14 06:41:01 +02:00
|
|
|
d[DatabaseKey.authorID] = authorID
|
2017-09-13 22:29:52 +02:00
|
|
|
|
2017-09-14 06:41:01 +02:00
|
|
|
if let name = name {
|
|
|
|
d[DatabaseKey.name] = name
|
|
|
|
}
|
|
|
|
if let url = url {
|
|
|
|
d[DatabaseKey.url] = url
|
|
|
|
}
|
|
|
|
if let avatarURL = avatarURL {
|
|
|
|
d[DatabaseKey.avatarURL] = avatarURL
|
|
|
|
}
|
|
|
|
if let emailAddress = emailAddress {
|
|
|
|
d[DatabaseKey.emailAddress] = emailAddress
|
|
|
|
}
|
2017-09-13 22:29:52 +02:00
|
|
|
|
2017-09-14 06:41:01 +02:00
|
|
|
return (d.copy() as! NSDictionary)
|
2017-09-13 22:29:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|