Use optional unwrapping and wfPost properties in publish API/handler

This commit is contained in:
Angelo Stavrow 2020-08-26 12:55:49 -04:00
parent 072e60c918
commit a97bd5abb7
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -66,30 +66,17 @@ extension WriteFreelyModel {
func publish(post: Post) {
guard let loggedInClient = client else { return }
if post.wfPost == nil {
// This is a new local draft.
post.wfPost = WFPost(
body: post.body,
title: post.title,
createdDate: post.createdDate
)
loggedInClient.createPost(
post: post.wfPost!, in: post.collection.wfCollection?.alias, completion: publishHandler
if let existingPostId = post.wfPost.postId {
// This is an existing post.
loggedInClient.updatePost(
postId: existingPostId,
updatedPost: post.wfPost,
completion: publishHandler
)
} else {
// This is an existing post.
// 1. Update Post.wfPost properties from (redundant) Post properties
// FIXME: https://github.com/writeas/writefreely-swiftui-multiplatform/issues/27
guard var publishedPost = post.wfPost else { return }
publishedPost.title = post.title
publishedPost.body = post.body
publishedPost.createdDate = post.createdDate
// 2. Update the post on the server and call the handler
loggedInClient.updatePost(
postId: publishedPost.postId!,
updatedPost: publishedPost,
completion: publishHandler
// This is a new local draft.
loggedInClient.createPost(
post: post.wfPost, in: post.collection.wfCollection?.alias, completion: publishHandler
)
}
}
@ -212,7 +199,7 @@ private extension WriteFreelyModel {
do {
let wfPost = try result.get()
let foundPostIndex = store.posts.firstIndex(where: {
$0.title == wfPost.title && $0.body == wfPost.body
$0.wfPost.title == wfPost.title && $0.wfPost.body == wfPost.body
})
guard let index = foundPostIndex else { return }
DispatchQueue.main.async {