@@ -15,17 +15,37 @@ class Model{
15 static var urlForDrafts = URL(string:"https://valera-denis.herokuapp.com/drafts/create")!
15 static var urlForDrafts = URL(string:"https://valera-denis.herokuapp.com/drafts/create")!
16 static var urlForPublish = URL(string:"https://valera-denis.herokuapp.com/drafts/publish")!
16 static var urlForPublish = URL(string:"https://valera-denis.herokuapp.com/drafts/publish")!
17 static var urlForUpdate = URL(string:"https://valera-denis.herokuapp.com/drafts/update")!
17 static var urlForUpdate = URL(string:"https://valera-denis.herokuapp.com/drafts/update")!
18+ static var urlForPostsForUser = URL(string:"https://valera-denis.herokuapp.com/posts/forUser")!
19+ static var urlForUsers = URL(string:"https://valera-denis.herokuapp.com/users")!
19 struct Posts: Codable {
21 struct Posts: Codable {
20 var body: [Attachments]
22 var body: [Attachments]
21 var authorId: Int
23 var authorId: Int
22 var id: Int
24 var id: Int
25+ var user: Model.Users?
24 init(body: [Attachments], authorId: Int, id: Int) {
27 init(body: [Attachments], authorId: Int, id: Int) {
25 self.body = body
28 self.body = body
26 self.authorId = authorId
29 self.authorId = authorId
27 self.id = id
30 self.id = id
33+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
35+ self.authorId = authorId
40+ mutating func setUser(with: Users){
45+ struct Users: Codable{
46+ var secondName: String
47+ var firstName: String
31 struct Attachments: Codable {
51 struct Attachments: Codable {
@@ -36,9 +56,9 @@ class Model{
39− static func authenticate(){
59+ static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
41− let json: [String:Any] = ["authenticationId" : 9]
61+ let json: [String:Any] = ["authenticationId" : id]
42 let jsonData = try? JSONSerialization.data(withJSONObject: json)
62 let jsonData = try? JSONSerialization.data(withJSONObject: json)
44 var request = URLRequest(url: url)
64 var request = URLRequest(url: url)
@@ -52,6 +72,7 @@ class Model{
53 guard let realResponse = responce.response, realResponse.statusCode == 204 else {
73 guard let realResponse = responce.response, realResponse.statusCode == 204 else {
54 print("Not a 204 response")
74 print("Not a 204 response")
@@ -59,11 +80,10 @@ class Model{
60 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
81 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
83+ HTTPCookieStorage.shared.setCookie(cookies[0])
63− for cookie in cookies {
64− HTTPCookieStorage.shared.setCookie(cookie)
65− DataStorage.standard.cookie = cookie
85+ DataStorage.standard.setIsLoggedIn(value: true)
66− }
86+ completion(true)
@@ -83,6 +103,8 @@ class Model{
84 if response.response?.statusCode == 200 {
104 if response.response?.statusCode == 200 {
85 print("success")
105 print("success")
107+ print(response.response?.statusCode)
88 let decoder = JSONDecoder()
110 let decoder = JSONDecoder()
@@ -162,7 +184,6 @@ class Model{
162 "id": \(post.id)
184 "id": \(post.id)
166 var request = URLRequest(url: urlForUpdate)
187 var request = URLRequest(url: urlForUpdate)
168 request.httpMethod = "POST"
189 request.httpMethod = "POST"
@@ -181,4 +202,68 @@ class Model{
207+ static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
213+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
215+ var request = URLRequest(url: urlForPostsForUser)
216+ request.httpMethod = "POST"
218+ // insert json data to the request
219+ request.httpBody = jsonData
220+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
222+ AF.request(request).responseJSON {
225+ if response.response?.statusCode == 200 {
229+ let decoder = JSONDecoder()
230+ guard let json = response.data else { return }
232+ guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
238+ static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
239+ let json = "\(Set(ids))"
241+ var request = URLRequest(url: urlForUsers)
243+ request.httpMethod = "POST"
245+ // insert json data to the request
246+ request.httpBody = json.data(using: .utf8)
247+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
249+ AF.request(request).responseJSON {
252+ if response.response?.statusCode == 200 {
256+ let decoder = JSONDecoder()
257+ guard let json = response.data else { return }
259+ guard let users = try? decoder.decode([Users].self, from: json) else { return }
261+ var dict: [Int:Users] = [:]
262+ users.forEach({ (user) in
263+ dict[user.id] = user