@@ -13,6 +13,7 @@ class Model{
13 static let invalidTocken = 498
13 static let invalidTocken = 498
14 private static var isValidTocken: ((Int)->())? = {
14 private static var isValidTocken: ((Int)->())? = {
15 responce in
15 responce in
16 if responce == invalidTocken {
17 if responce == invalidTocken {
17 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -22,12 +23,10 @@ class Model{
23 static let url = URL(string:"\(baseUrl)/authentication/login")!
24 static let url = URL(string:"\(baseUrl)/authentication/login")!
24 static let url1 = URL(string:"\(baseUrl)/posts/last")!
25 static let url1 = URL(string:"\(baseUrl)/posts/last")!
25− static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
26− static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
27− static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
28 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
26 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
29 static let urlForUsers = URL(string:"\(baseUrl)/users")!
27 static let urlForUsers = URL(string:"\(baseUrl)/users")!
30 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
28 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29+ static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
33 struct QueryPosts: Codable{
32 struct QueryPosts: Codable{
@@ -41,20 +40,23 @@ class Model{
41 var id: Int
40 var id: Int
42 var user: Model.Users?
41 var user: Model.Users?
43 var updated: String
42 var updated: String
45− init(body: [Attachments], authorId: Int, id: Int, date: String) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String,tags: [String]) {
46 self.body = body
46 self.body = body
47 self.authorId = authorId
47 self.authorId = authorId
48 self.id = id
48 self.id = id
49 self.updated = date
49 self.updated = date
52− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
53+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String, tags: [String]) {
53 self.body = body
54 self.body = body
54 self.authorId = authorId
55 self.authorId = authorId
55 self.id = id
56 self.id = id
56 self.user = user
57 self.user = user
57 self.updated = date
58 self.updated = date
60 func convertDateFormatter() -> String
62 func convertDateFormatter() -> String
@@ -62,17 +64,33 @@ class Model{
63 let dateFormatter = DateFormatter()
65 let dateFormatter = DateFormatter()
64 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
66 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"//this your string date format
65− dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
67+ dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
66 let date = dateFormatter.date(from: updated)
68 let date = dateFormatter.date(from: updated)
68 dateFormatter.dateFormat = "MMM d, yyyy HH:mm" ///this is what you want to convert format
70 dateFormatter.dateFormat = "MMM d, yyyy HH:mm" ///this is what you want to convert format
69− dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
71+ dateFormatter.timeZone = NSTimeZone.local
70 let timeStamp = dateFormatter.string(from: date!)
72 let timeStamp = dateFormatter.string(from: date!)
72 return timeStamp
74 return timeStamp
78+ struct CreatedPost: Codable {
79+ var body = [Attachments]()
80+ var tags = [String]()
82+ enum CodingKeys: String, CodingKey {
87+ func encode(to encoder: Encoder) throws {
88+ var container = encoder.container(keyedBy: CodingKeys.self)
89+ try container.encode(body, forKey: .body)
90+ try container.encode(tags, forKey: .tags)
76 struct Users: Codable{
94 struct Users: Codable{
78 var middleName: String
96 var middleName: String
@@ -88,6 +106,15 @@ class Model{
88 init(markdown: String) {
106 init(markdown: String) {
89 self.markdown = markdown
107 self.markdown = markdown
110+ enum CodingKeys: String, CodingKey {
114+ func encode(to encoder: Encoder) throws {
115+ var container = encoder.container(keyedBy: CodingKeys.self)
116+ try container.encode(markdown, forKey: .markdown)
93 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
120 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -155,50 +182,17 @@ class Model{
158− static func createDraft(completion: @escaping ((Int)->())){
159− let jsonString = "[{ \"markdown\": \"# This is a markdown title\nThis is body.\" }]"
160− var request = URLRequest(url: urlForDrafts)
162− request.httpMethod = "POST"
164− // insert json data to the request
165− request.httpBody = jsonString.data(using: .utf8)
166− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
168− AF.request(request).responseJSON {
171− if response.response?.statusCode == 200 {
172− print("success in creating draft")
174− print("\(response.response?.statusCode ?? 0)")
177− //let decoder = JSONDecoder()
178− guard let json = response.data else { return }
180− guard let id = String(data: json, encoding: String.Encoding.utf8) else {return}
182− print("with id \(id)")
183− completion(Int(id) ?? 0)
188− static func createAndPublish(string: String){
186+ static func createAndPublish(body: [Attachments], tags: [String]){
189− let jsonUpd = """
187+ let jsonUpd = CreatedPost(body: body, tags: tags)
192− "markdown": "\(string)"
196 var request = URLRequest(url: createAndPublishURL)
189 var request = URLRequest(url: createAndPublishURL)
198 request.httpMethod = "POST"
191 request.httpMethod = "POST"
200 // insert json data to the request
193 // insert json data to the request
201− request.httpBody = jsonUpd.data(using: .utf8)
194+ request.httpBody = try? JSONEncoder().encode(jsonUpd)
202 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
196 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
203 AF.request(request).response {
197 AF.request(request).response {
204 (response) in
198 (response) in
@@ -210,7 +204,7 @@ class Model{
211 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
205 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
212 let json = [
206 let json = [
213− "userId" : id,
207+ "request" : id,
214 "limit": 10
208 "limit": 10
@@ -271,4 +265,36 @@ class Model{
271 completion(Model.idUser)
265 completion(Model.idUser)
269+ // get channel (with id): in responce -- PostQuery
270+ static func getChannels(with channelId: Int, completion: @escaping (([Posts])->())){
272+ "request" : channelId,
276+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
278+ var request = URLRequest(url: urlForPostsForUser)
279+ request.httpMethod = "POST"
281+ // insert json data to the request
282+ request.httpBody = jsonData
283+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
285+ AF.request(request).responseJSON {
288+ if response.response?.statusCode == 200 {
292+ let decoder = JSONDecoder()
293+ guard let json = response.data else { return }
295+ guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
297+ completion(newPost.posts)