@@ -10,46 +10,61 @@ import Foundation
10import Alamofire
10import Alamofire
12class Model{
12class Model{
13+ static let invalidTocken = 498
14+ private static var isValidTocken: ((Int)->())? = {
16+ if responce == invalidTocken {
17+ DataStorage.standard.setIsLoggedIn(value: false, with: 0)
14 private static let baseUrl = "https://valera-denis.herokuapp.com"
21 private static let baseUrl = "https://valera-denis.herokuapp.com"
16− static let url = URL(string:"\(baseUrl)/authenticate")!
23+ static let url = URL(string:"\(baseUrl)/authentication/login")!
17 static let url1 = URL(string:"\(baseUrl)/posts/last")!
24 static let url1 = URL(string:"\(baseUrl)/posts/last")!
18 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
25 static let urlForDrafts = URL(string:"\(baseUrl)/drafts/create")!
19 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
26 static let urlForPublish = URL(string:"\(baseUrl)/drafts/publish")!
20 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
27 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
21 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
28 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
22 static let urlForUsers = URL(string:"\(baseUrl)/users")!
29 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")!
33+ struct QueryPosts: Codable{
34+ var users: [Int: Users]
25 struct Posts: Codable {
38 struct Posts: Codable {
26 var body: [Attachments]
39 var body: [Attachments]
27 var authorId: Int
40 var authorId: Int
28 var id: Int
41 var id: Int
29 var user: Model.Users?
42 var user: Model.Users?
31− init(body: [Attachments], authorId: Int, id: Int) {
45+ init(body: [Attachments], authorId: Int, id: Int, date: String) {
32 self.body = body
46 self.body = body
33 self.authorId = authorId
47 self.authorId = authorId
34 self.id = id
48 self.id = id
37− init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
52+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users, date: String) {
38 self.body = body
53 self.body = body
39 self.authorId = authorId
54 self.authorId = authorId
40 self.id = id
55 self.id = id
41 self.user = user
56 self.user = user
42− }
57+ self.updated = date
44− mutating func setUser(with: Users){
49 struct Users: Codable{
61 struct Users: Codable{
63+ var middleName: String
50− var secondName: String
64+ var lastName: String
51 var firstName: String
65 var firstName: String
52 var id: Int
66 var id: Int
55 struct Attachments: Codable {
70 struct Attachments: Codable {
@@ -74,8 +89,15 @@ class Model{
75 AF.request(request).response { (responce) in
90 AF.request(request).response { (responce) in
77− guard let realResponse = responce.response, realResponse.statusCode == 204 else {
92+ guard let realResponse = responce.response, realResponse.statusCode == 200 else
78− print("Not a 204 response")
94+ print("Not a 200 response")
99+ guard let json = responce.data else { return }
100+ guard let personId = Int(String(data: json, encoding: String.Encoding.utf8)!) else {
79 completion(false)
101 completion(false)
@@ -86,13 +108,13 @@ class Model{
87 HTTPCookieStorage.shared.setCookie(cookies[0])
109 HTTPCookieStorage.shared.setCookie(cookies[0])
89− DataStorage.standard.setIsLoggedIn(value: true, with: id)
111+ DataStorage.standard.setIsLoggedIn(value: true, with: personId)
90 completion(true)
112 completion(true)
95− static func getLast(completion: @escaping ((Channel)->())){
117+ static func getLast(completion: @escaping (((users:[Int: Users], channel:Channel))->())){
96 let jsonString = "30"
118 let jsonString = "30"
97 var request = URLRequest(url: url1)
119 var request = URLRequest(url: url1)
@@ -105,18 +127,15 @@ class Model{
105 AF.request(request).responseJSON {
127 AF.request(request).responseJSON {
106 (response) in
128 (response) in
108− if response.response?.statusCode == 200 {
111− print(response.response?.statusCode)
130+ isValidTocken?(response.response?.statusCode ?? 498)
114 let decoder = JSONDecoder()
132 let decoder = JSONDecoder()
115 guard let json = response.data else { return }
133 guard let json = response.data else { return }
117− guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
135+ guard let newQueery = try? decoder.decode(QueryPosts.self, from: json) else { print("no")
119− completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
138+ completion((newQueery.users, Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newQueery.posts)))
@@ -168,11 +187,7 @@ class Model{
168 AF.request(request).response {
187 AF.request(request).response {
169 (response) in
188 (response) in
171− if response.response?.statusCode == 204 {
190+ isValidTocken?(response.response?.statusCode ?? 498)
172− print("post created")
174− print("failure in creating and publishing post")
@@ -202,9 +217,9 @@ class Model{
202 let decoder = JSONDecoder()
217 let decoder = JSONDecoder()
203 guard let json = response.data else { return }
218 guard let json = response.data else { return }
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 }
207− completion(newPost)
222+ completion(newPost.posts)