@@ -10,46 +10,61 @@ import Foundation
1010import Alamofire
1111
1212class Model{
13+ static let invalidTocken = 498
14+ private static var isValidTocken: ((Int)->())? = {
15+ responce in
16+ if responce == invalidTocken {
17+ DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18+ }
19+ }
1320
1421 private static let baseUrl = "https://valera-denis.herokuapp.com"
1522
16− static let url = URL(string:"\(baseUrl)/authenticate")!
23+ static let url = URL(string:"\(baseUrl)/authentication/login")!
1724 static let url1 = URL(string:"\(baseUrl)/posts/last")!
1825 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
1926 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
2027 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
2128 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
2229 static let urlForUsers = URL(string:"\(baseUrl)/users")!
23− static let createAndPublishURL = URL(string:"\(baseUrl)/drafts/createAndPublish")!
30+ static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
31+
32+
33+ struct QueryPosts: Codable{
34+ var users: [Int: Users]
35+ var posts: [Posts]
36+ }
2437
2538 struct Posts: Codable {
2639 var body: [Attachments]
2740 var authorId: Int
2841 var id: Int
2942 var user: Model.Users?
43+ var updated: String
3044
31− init(body: [Attachments], authorId: Int, id: Int) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String) {
3246 self.body = body
3347 self.authorId = authorId
3448 self.id = id
49+ self.updated = date
3550 }
3651
37− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
52+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
3853 self.body = body
3954 self.authorId = authorId
4055 self.id = id
4156 self.user = user
42− }
43−
44− mutating func setUser(with: Users){
45− self.user = with
57+ self.updated = date
4658 }
4759 }
4860
4961 struct Users: Codable{
50− var secondName: String
62+
63+ var middleName: String
64+ var lastName: String
5165 var firstName: String
5266 var id: Int
67+
5368 }
5469
5570 struct Attachments: Codable {
@@ -74,8 +89,15 @@ class Model{
7489
7590 AF.request(request).response { (responce) in
7691
77− guard let realResponse = responce.response, realResponse.statusCode == 204 else {
78− print("Not a 204 response")
92+ guard let realResponse = responce.response, realResponse.statusCode == 200 else
93+ {
94+ print("Not a 200 response")
95+ completion(false)
96+ return
97+ }
98+
99+ guard let json = responce.data else { return }
100+ guard let personId = Int(String(data: json, encoding: String.Encoding.utf8)!) else {
79101 completion(false)
80102 return
81103 }
@@ -86,13 +108,13 @@ class Model{
86108
87109 HTTPCookieStorage.shared.setCookie(cookies[0])
88110
89− DataStorage.standard.setIsLoggedIn(value: true, with: id)
111+ DataStorage.standard.setIsLoggedIn(value: true, with: personId)
90112 completion(true)
91113 }
92114 }
93115
94116
95− static func getLast(completion: @escaping ((Channel)->())){
117+ static func getLast(completion: @escaping (((users:[Int: Users], channel:Channel))->())){
96118 let jsonString = "30"
97119 var request = URLRequest(url: url1)
98120
@@ -105,18 +127,15 @@ class Model{
105127 AF.request(request).responseJSON {
106128 (response) in
107129
108− if response.response?.statusCode == 200 {
109− print("success")
110− } else {
111− print(response.response?.statusCode)
112− }
130+ isValidTocken?(response.response?.statusCode ?? 498)
113131
114132 let decoder = JSONDecoder()
115133 guard let json = response.data else { return }
116−
117− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
118134
119− completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
135+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
136+ return }
137+
138+ completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts)))
120139 }
121140 }
122141
@@ -168,11 +187,7 @@ class Model{
168187 AF.request(request).response {
169188 (response) in
170189
171− if response.response?.statusCode == 204 {
172− print("post created")
173− } else {
174− print("failure in creating and publishing post")
175− }
190+ isValidTocken?(response.response?.statusCode ?? 498)
176191 }
177192 }
178193
@@ -202,9 +217,9 @@ class Model{
202217 let decoder = JSONDecoder()
203218 guard let json = response.data else { return }
204219
205− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
220+ guard let newPost = try? decoder.decode(QueryPosts.self, from: json) else { return }
206221
207− completion(newPost)
222+ completion(newPost.posts)
208223 }
209224 }
210225