-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathBlog+Post.swift
111 lines (90 loc) · 3.8 KB
/
Blog+Post.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Foundation
// MARK: - Lookup posts
extension Blog {
/// Lookup a post in the blog.
///
/// - Parameter postID: The ID associated with the post.
/// - Returns: The `AbstractPost` associated with the given post ID.
@objc(lookupPostWithID:inContext:)
func lookupPost(withID postID: NSNumber, in context: NSManagedObjectContext) -> AbstractPost? {
lookupPost(withID: postID.int64Value, in: context)
}
/// Lookup a post in the blog.
///
/// - Parameter postID: The ID associated with the post.
/// - Returns: The `AbstractPost` associated with the given post ID.
func lookupPost(withID postID: Int, in context: NSManagedObjectContext) -> AbstractPost? {
lookupPost(withID: Int64(postID), in: context)
}
/// Lookup a post in the blog.
///
/// - Parameter postID: The ID associated with the post.
/// - Returns: The `AbstractPost` associated with the given post ID.
func lookupPost(withID postID: Int64, in context: NSManagedObjectContext) -> AbstractPost? {
let request = NSFetchRequest<AbstractPost>(entityName: NSStringFromClass(AbstractPost.self))
request.predicate = NSPredicate(format: "blog = %@ AND original = NULL AND postID = %ld", self, postID)
return (try? context.fetch(request))?.first
}
}
// MARK: - Create posts
extension Blog {
/// Create a post in the blog.
@objc
func createPost() -> Post {
guard let context = managedObjectContext else {
fatalError("The `Blog` instance is not associated with an `NSManagedObjectContext`")
}
let post = NSEntityDescription.insertNewObject(forEntityName: NSStringFromClass(Post.self), into: context) as! Post
post.blog = self
post.remoteStatus = .sync
if let categoryID = settings?.defaultCategoryID,
categoryID.intValue != PostCategoryUncategorized,
let category = PostCategoryService(managedObjectContext: context).find(withBlogObjectID: objectID, andCategoryID: categoryID) {
post.addCategoriesObject(category)
}
post.postFormat = settings?.defaultPostFormat
post.postType = Post.typeDefaultIdentifier
if let userID = userID, let author = getAuthorWith(id: userID) {
post.authorID = author.userID
post.author = author.displayName
}
try? context.obtainPermanentIDs(for: [post])
precondition(!post.objectID.isTemporaryID, "The new post for this blog must have a permanent ObjectID")
return post
}
/// Create a draft post in the blog.
func createDraftPost() -> Post {
let post = createPost()
markAsDraft(post)
return post
}
/// Create a page in the blog.
@objc
func createPage() -> Page {
guard let context = managedObjectContext else {
fatalError("The `Blog` instance is not associated with a `NSManagedObjectContext`")
}
let page = NSEntityDescription.insertNewObject(forEntityName: NSStringFromClass(Page.self), into: context) as! Page
page.blog = self
page.date_created_gmt = Date()
page.remoteStatus = .sync
if let userID = userID, let author = getAuthorWith(id: userID) {
page.authorID = author.userID
page.author = author.displayName
}
try? context.obtainPermanentIDs(for: [page])
precondition(!page.objectID.isTemporaryID, "The new page for this blog must have a permanent ObjectID")
return page
}
/// Create a draft page in the blog.
func createDraftPage() -> Page {
let page = createPage()
markAsDraft(page)
return page
}
private func markAsDraft(_ post: AbstractPost) {
post.remoteStatus = .local
post.dateModified = Date()
post.status = .draft
}
}