@@ -13,6 +13,7 @@ class Model{
1313 static let invalidTocken = 498
1414 private static var isValidTocken: ((Int)->())? = {
1515 responce in
16+ print("\(responce)")
1617 if responce == invalidTocken {
1718 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
1819 }
@@ -22,12 +23,10 @@ class Model{
2223
2324 static let url = URL(string:"\(baseUrl)/authentication/login")!
2425 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")!
2826 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
2927 static let urlForUsers = URL(string:"\(baseUrl)/users")!
3028 static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29+ static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
3130
3231
3332 struct QueryPosts: Codable{
@@ -41,20 +40,23 @@ class Model{
4140 var id: Int
4241 var user: Model.Users?
4342 var updated: String
43+ var tags: [String]
4444
45− init(body: [Attachments], authorId: Int, id: Int, date: String) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String,tags: [String]) {
4646 self.body = body
4747 self.authorId = authorId
4848 self.id = id
4949 self.updated = date
50+ self.tags = tags
5051 }
5152
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]) {
5354 self.body = body
5455 self.authorId = authorId
5556 self.id = id
5657 self.user = user
5758 self.updated = date
59+ self.tags = tags
5860 }
5961
6062 func convertDateFormatter() -> String
@@ -62,17 +64,33 @@ class Model{
6264
6365 let dateFormatter = DateFormatter()
6466 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")
6668 let date = dateFormatter.date(from: updated)
6769
6870 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
7072 let timeStamp = dateFormatter.string(from: date!)
7173
7274 return timeStamp
7375 }
7476 }
7577
78+ struct CreatedPost: Codable {
79+ var body = [Attachments]()
80+ var tags = [String]()
81+
82+ enum CodingKeys: String, CodingKey {
83+ case body
84+ case tags
85+ }
86+
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)
91+ }
92+ }
93+
7694 struct Users: Codable{
7795
7896 var middleName: String
@@ -88,6 +106,15 @@ class Model{
88106 init(markdown: String) {
89107 self.markdown = markdown
90108 }
109+
110+ enum CodingKeys: String, CodingKey {
111+ case markdown
112+ }
113+
114+ func encode(to encoder: Encoder) throws {
115+ var container = encoder.container(keyedBy: CodingKeys.self)
116+ try container.encode(markdown, forKey: .markdown)
117+ }
91118 }
92119
93120 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -155,50 +182,17 @@ class Model{
155182 }
156183 }
157184
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)
161−
162− request.httpMethod = "POST"
163−
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")
167−
168− AF.request(request).responseJSON {
169− (response) in
170−
171− if response.response?.statusCode == 200 {
172− print("success in creating draft")
173− } else {
174− print("\(response.response?.statusCode ?? 0)")
175− }
176−
177− //let decoder = JSONDecoder()
178− guard let json = response.data else { return }
179−
180− guard let id = String(data: json, encoding: String.Encoding.utf8) else {return}
181−
182− print("with id \(id)")
183− completion(Int(id) ?? 0)
184− }
185− }
186−
187185
188− static func createAndPublish(string: String){
189− let jsonUpd = """
190− [
191− {
192− "markdown": "\(string)"
193− }
194− ]
195− """
186+ static func createAndPublish(body: [Attachments], tags: [String]){
187+ let jsonUpd = CreatedPost(body: body, tags: tags)
188+
196189 var request = URLRequest(url: createAndPublishURL)
197190
198191 request.httpMethod = "POST"
199192
200193 // insert json data to the request
201− request.httpBody = jsonUpd.data(using: .utf8)
194+ request.httpBody = try? JSONEncoder().encode(jsonUpd)
195+
202196 request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
203197 AF.request(request).response {
204198 (response) in
@@ -210,7 +204,7 @@ class Model{
210204
211205 static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
212206 let json = [
213− "userId" : id,
207+ "request" : id,
214208 "limit": 10
215209 ]
216210
@@ -271,4 +265,36 @@ class Model{
271265 completion(Model.idUser)
272266 }
273267 }
268+
269+ // get channel (with id): in responce -- PostQuery
270+ static func getChannels(with channelId: Int, completion: @escaping (([Posts])->())){
271+ let json = [
272+ "request" : channelId,
273+ "limit": 10
274+ ]
275+
276+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
277+
278+ var request = URLRequest(url: urlForPostsForUser)
279+ request.httpMethod = "POST"
280+
281+ // insert json data to the request
282+ request.httpBody = jsonData
283+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
284+
285+ AF.request(request).responseJSON {
286+ (response) in
287+
288+ if response.response?.statusCode == 200 {
289+ print("success")
290+ }
291+
292+ let decoder = JSONDecoder()
293+ guard let json = response.data else { return }
294+
295+ guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
296+
297+ completion(newPost.posts)
298+ }
299+ }
274300}