Skip to content

Commit

Permalink
Fix search posts model
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Jul 5, 2024
1 parent bcc9dab commit 45d0170
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 277 deletions.
3 changes: 1 addition & 2 deletions Sources/WPSwift/Models/Params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ extension [String: Any] {
"search": term,
"page": page,
"per_page": perPage,
"_embed": "",
"_fields":"id,date_gmt,modified_gmt,status,title,content.rendered,excerpt,author,featured_media,comment_status,categories,tags,link,_links.author,_links.wp:featuredmedia"
"_fields":"id,title,url"
]
}

Expand Down
130 changes: 6 additions & 124 deletions Sources/WPSwift/Models/Post.swift
Original file line number Diff line number Diff line change
@@ -1,144 +1,26 @@
//
// Post.swift
// SimplePost.swift
//
//
// Created by Ulaş Sancak on 6.10.2023.
// Created by Ulaş Sancak on 5.07.2023.
//

import Foundation

public struct Post: Decodable {
public struct SimplePost: Decodable {
public let id: Int
public let date: Date?
public let modified: Date?
public let status: String
public let title: RenderedContent
public let content: RenderedContent
public let excerpt: RenderedContent
public let author: Int
public let featured_media: Int
public let comment_status: String
public let categories: [Int]
public let tags: [Int]
public let title: String
public let link: String
public let embeddedContent: EmbeddedContent

private enum CodingKeys: String, CodingKey {
case id
case date = "date_gmt"
case modified = "modified_gmt"
case status
case title
case content
case excerpt
case author
case featured_media
case comment_status
case categories
case tags
case link
case embeddedContent = "_embedded"
case link = "url"
}

public init(id: Int, date: Date? = nil, modified: Date? = nil, status: String, title: RenderedContent, content: RenderedContent, excerpt: RenderedContent, author: Int, featured_media: Int, comment_status: String, categories: [Int], tags: [Int], link: String, embeddedContent: EmbeddedContent) {
public init(id: Int, title: String, link: String) {
self.id = id
self.date = date
self.modified = modified
self.status = status
self.title = title
self.content = content
self.excerpt = excerpt
self.author = author
self.featured_media = featured_media
self.comment_status = comment_status
self.categories = categories
self.tags = tags
self.link = link
self.embeddedContent = embeddedContent
}
}

public struct PostToCreate: Encodable {
public let title: RenderedContent
public let content: RenderedContent
public let format: String?
public let sticky: Bool?
public let excerpt: RenderedContent?
public let status: String?
public let password: String?
public let comment_status: String?
public let ping_status: String?
public let categories: [Int]
public let tags: [Int]

public init(title: RenderedContent, content: RenderedContent, format: String? = nil, sticky: Bool? = nil, excerpt: RenderedContent? = nil, status: String? = nil, password: String? = nil, comment_status: String? = nil, ping_status: String? = nil, categories: [Int], tags: [Int]) {
self.title = title
self.content = content
self.format = format
self.sticky = sticky
self.excerpt = excerpt
self.status = status
self.password = password
self.comment_status = comment_status
self.ping_status = ping_status
self.categories = categories
self.tags = tags
}
}

public struct PostToUpdate: Encodable {
public let id: Int
public let title: RenderedContent
public let content: RenderedContent
public let format: String?
public let sticky: Bool?
public let excerpt: RenderedContent?
public let status: String?
public let password: String?
public let comment_status: String?
public let ping_status: String?
public let categories: [Int]
public let tags: [Int]

public init(id: Int, title: RenderedContent, content: RenderedContent, format: String? = nil, sticky: Bool? = nil, excerpt: RenderedContent? = nil, status: String? = nil, password: String? = nil, comment_status: String? = nil, ping_status: String? = nil, categories: [Int], tags: [Int]) {
self.id = id
self.title = title
self.content = content
self.format = format
self.sticky = sticky
self.excerpt = excerpt
self.status = status
self.password = password
self.comment_status = comment_status
self.ping_status = ping_status
self.categories = categories
self.tags = tags
}
}

public struct RenderedContent: Codable {
public let rendered: String
public let protected: Bool

public init(rendered: String, protected: Bool = false) {
self.rendered = rendered
self.protected = protected
}

enum CodingKeys: CodingKey {
case rendered
case protected
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.rendered = try container.decode(String.self, forKey: .rendered)
self.protected = try container.decodeIfPresent(Bool.self, forKey: .protected) ?? false
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.rendered, forKey: .rendered)
try container.encode(self.protected, forKey: .protected)
}
}
144 changes: 144 additions & 0 deletions Sources/WPSwift/Models/SimplePost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
//
// Post.swift
//
//
// Created by Ulaş Sancak on 6.10.2023.
//

import Foundation

public struct Post: Decodable {
public let id: Int
public let date: Date?
public let modified: Date?
public let status: String?
public let title: RenderedContent
public let content: RenderedContent
public let excerpt: RenderedContent
public let author: Int
public let featured_media: Int
public let comment_status: String
public let categories: [Int]
public let tags: [Int]
public let link: String
public let embeddedContent: EmbeddedContent

private enum CodingKeys: String, CodingKey {
case id
case date = "date_gmt"
case modified = "modified_gmt"
case status
case title
case content
case excerpt
case author
case featured_media
case comment_status
case categories
case tags
case link
case embeddedContent = "_embedded"
}

public init(id: Int, date: Date? = nil, modified: Date? = nil, status: String? = nil, title: RenderedContent, content: RenderedContent, excerpt: RenderedContent, author: Int, featured_media: Int, comment_status: String, categories: [Int], tags: [Int], link: String, embeddedContent: EmbeddedContent) {
self.id = id
self.date = date
self.modified = modified
self.status = status
self.title = title
self.content = content
self.excerpt = excerpt
self.author = author
self.featured_media = featured_media
self.comment_status = comment_status
self.categories = categories
self.tags = tags
self.link = link
self.embeddedContent = embeddedContent
}
}

public struct PostToCreate: Encodable {
public let title: RenderedContent
public let content: RenderedContent
public let format: String?
public let sticky: Bool?
public let excerpt: RenderedContent?
public let status: String?
public let password: String?
public let comment_status: String?
public let ping_status: String?
public let categories: [Int]
public let tags: [Int]

public init(title: RenderedContent, content: RenderedContent, format: String? = nil, sticky: Bool? = nil, excerpt: RenderedContent? = nil, status: String? = nil, password: String? = nil, comment_status: String? = nil, ping_status: String? = nil, categories: [Int], tags: [Int]) {
self.title = title
self.content = content
self.format = format
self.sticky = sticky
self.excerpt = excerpt
self.status = status
self.password = password
self.comment_status = comment_status
self.ping_status = ping_status
self.categories = categories
self.tags = tags
}
}

public struct PostToUpdate: Encodable {
public let id: Int
public let title: RenderedContent
public let content: RenderedContent
public let format: String?
public let sticky: Bool?
public let excerpt: RenderedContent?
public let status: String?
public let password: String?
public let comment_status: String?
public let ping_status: String?
public let categories: [Int]
public let tags: [Int]

public init(id: Int, title: RenderedContent, content: RenderedContent, format: String? = nil, sticky: Bool? = nil, excerpt: RenderedContent? = nil, status: String? = nil, password: String? = nil, comment_status: String? = nil, ping_status: String? = nil, categories: [Int], tags: [Int]) {
self.id = id
self.title = title
self.content = content
self.format = format
self.sticky = sticky
self.excerpt = excerpt
self.status = status
self.password = password
self.comment_status = comment_status
self.ping_status = ping_status
self.categories = categories
self.tags = tags
}
}

public struct RenderedContent: Codable {
public let rendered: String
public let protected: Bool

public init(rendered: String, protected: Bool = false) {
self.rendered = rendered
self.protected = protected
}

enum CodingKeys: CodingKey {
case rendered
case protected
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.rendered = try container.decode(String.self, forKey: .rendered)
self.protected = try container.decodeIfPresent(Bool.self, forKey: .protected) ?? false
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.rendered, forKey: .rendered)
try container.encode(self.protected, forKey: .protected)
}
}
2 changes: 1 addition & 1 deletion Sources/WPSwift/Repositories/PostsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct PostsRepository {
try .init(.init(endpoint: WPEndpoint.Posts.posts.path, parameters: .createParamsForPosts(page: page, perPage: perPage, order: order, categories: categories, categoriesToExclude: categoriesToExclude, tags: tags, tagsToExclude: tagsToExclude)))
}

public func getSearchPostsClient(term: String, page: Int = 1, perPage: Int = 10) throws -> WPClient<EmptyModel, [Post]> {
public func getSearchPostsClient(term: String, page: Int = 1, perPage: Int = 10) throws -> WPClient<EmptyModel, [SimplePost]> {
try .init(.init(endpoint: WPEndpoint.Posts.search.path, parameters: .createParamsForSearchPosts(term: term, page: page, perPage: perPage)))
}

Expand Down
Loading

0 comments on commit 45d0170

Please sign in to comment.