2017-07-04 00:04:31 +02:00
|
|
|
//
|
|
|
|
// Article+Database.swift
|
|
|
|
// Database
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/3/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSDatabase
|
|
|
|
import Data
|
|
|
|
|
|
|
|
extension Article {
|
|
|
|
|
2017-07-08 02:49:16 +02:00
|
|
|
convenience init?(row: FMResultSet, account: Account) {
|
2017-07-04 00:04:31 +02:00
|
|
|
|
2017-07-08 02:49:16 +02:00
|
|
|
guard let feedID = row.string(forColumn: DatabaseKey.feedID) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let uniqueID = row.string(forColumn: DatabaseKey.uniqueID) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let articleID = row.string(forColumn: DatabaseKey.articleID)!
|
|
|
|
let title = row.string(forColumn: DatabaseKey.title)
|
|
|
|
let contentHTML = row.string(forColumn: DatabaseKey.contentHTML)
|
|
|
|
let contentText = row.string(forColumn: DatabaseKey.contentText)
|
|
|
|
let url = row.string(forColumn: DatabaseKey.url)
|
|
|
|
let externalURL = row.string(forColumn: DatabaseKey.externalURL)
|
|
|
|
let summary = row.string(forColumn: DatabaseKey.summary)
|
|
|
|
let imageURL = row.string(forColumn: DatabaseKey.imageURL)
|
|
|
|
let bannerImageURL = row.string(forColumn: DatabaseKey.bannerImageURL)
|
|
|
|
let datePublished = row.date(forColumn: DatabaseKey.datePublished)
|
|
|
|
let dateModified = row.date(forColumn: DatabaseKey.dateModified)
|
2017-08-22 16:45:09 +02:00
|
|
|
let accountInfo: [String: Any]? = nil // TODO
|
|
|
|
|
|
|
|
// authors, tags, and attachments are fetched from related tables, after init.
|
2017-08-21 22:31:14 +02:00
|
|
|
let authors: [Author]? = nil
|
|
|
|
let tags: Set<String>? = nil
|
|
|
|
let attachments: [Attachment]? = nil
|
2017-07-08 02:49:16 +02:00
|
|
|
|
2017-08-21 22:31:14 +02:00
|
|
|
self.init(account: account, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo)
|
2017-07-04 00:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func databaseDictionary() -> NSDictionary {
|
|
|
|
|
2017-08-21 07:43:46 +02:00
|
|
|
let d = NSMutableDictionary()
|
2017-07-04 00:04:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
return d.copy() as! NSDictionary
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 06:16:13 +02:00
|
|
|
|
2017-08-21 02:46:15 +02:00
|
|
|
extension Article: DatabaseObject {
|
|
|
|
|
2017-08-21 07:43:46 +02:00
|
|
|
public var databaseID: String {
|
2017-08-21 02:46:15 +02:00
|
|
|
get {
|
|
|
|
return articleID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 06:16:13 +02:00
|
|
|
extension Set where Element == Article {
|
|
|
|
|
|
|
|
func withNilProperty<T>(_ keyPath: KeyPath<Article,T?>) -> Set<Article> {
|
|
|
|
|
|
|
|
return Set(filter{ $0[keyPath: keyPath] == nil })
|
|
|
|
}
|
|
|
|
|
|
|
|
func articleIDs() -> Set<String> {
|
|
|
|
|
2017-08-21 22:31:14 +02:00
|
|
|
return Set<String>(map { $0.databaseID })
|
2017-08-07 06:16:13 +02:00
|
|
|
}
|
2017-08-27 00:37:15 +02:00
|
|
|
|
|
|
|
func eachHasAStatus() -> Bool {
|
|
|
|
|
|
|
|
for article in self {
|
|
|
|
if article.status == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func missingStatuses() -> Set<Article> {
|
|
|
|
|
|
|
|
return withNilProperty(\Article.status)
|
|
|
|
}
|
2017-08-29 22:32:36 +02:00
|
|
|
|
|
|
|
func statuses() -> Set<ArticleStatus> {
|
|
|
|
|
|
|
|
return Set<ArticleStatus>(self.flatMap { $0.status })
|
|
|
|
}
|
2017-08-07 06:16:13 +02:00
|
|
|
}
|