@@ -15,17 +15,37 @@ class Model{
1515 static var urlForDrafts = URL(string:"https://valera-denis.herokuapp.com/drafts/create")!
1616 static var urlForPublish = URL(string:"https://valera-denis.herokuapp.com/drafts/publish")!
1717 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")!
1820
1921 struct Posts: Codable {
2022 var body: [Attachments]
2123 var authorId: Int
2224 var id: Int
25+ var user: Model.Users?
2326
2427 init(body: [Attachments], authorId: Int, id: Int) {
2528 self.body = body
2629 self.authorId = authorId
2730 self.id = id
2831 }
32+
33+ init(body: [Attachments], authorId: Int, id: Int, user: Model.Users) {
34+ self.body = body
35+ self.authorId = authorId
36+ self.id = id
37+ self.user = user
38+ }
39+
40+ mutating func setUser(with: Users){
41+ self.user = with
42+ }
43+ }
44+
45+ struct Users: Codable{
46+ var secondName: String
47+ var firstName: String
48+ var id: Int
2949 }
3050
3151 struct Attachments: Codable {
@@ -36,9 +56,9 @@ class Model{
3656 }
3757 }
3858
39− static func authenticate(){
59+ static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
4060
41− let json: [String:Any] = ["authenticationId" : 9]
61+ let json: [String:Any] = ["authenticationId" : id]
4262 let jsonData = try? JSONSerialization.data(withJSONObject: json)
4363
4464 var request = URLRequest(url: url)
@@ -52,6 +72,7 @@ class Model{
5272
5373 guard let realResponse = responce.response, realResponse.statusCode == 204 else {
5474 print("Not a 204 response")
75+ completion(false)
5576 return
5677 }
5778
@@ -59,11 +80,10 @@ class Model{
5980
6081 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
6182
83+ HTTPCookieStorage.shared.setCookie(cookies[0])
6284
63− for cookie in cookies {
64− HTTPCookieStorage.shared.setCookie(cookie)
65− DataStorage.standard.cookie = cookie
66− }
85+ DataStorage.standard.setIsLoggedIn(value: true)
86+ completion(true)
6787 }
6888 }
6989
@@ -83,6 +103,8 @@ class Model{
83103
84104 if response.response?.statusCode == 200 {
85105 print("success")
106+ } else {
107+ print(response.response?.statusCode)
86108 }
87109
88110 let decoder = JSONDecoder()
@@ -162,7 +184,6 @@ class Model{
162184 "id": \(post.id)
163185 }
164186 """
165− print(jsonUpd)
166187 var request = URLRequest(url: urlForUpdate)
167188
168189 request.httpMethod = "POST"
@@ -181,4 +202,68 @@ class Model{
181202 }
182203 }
183204 }
205+
206+
207+ static func getPostsForUser(with id: Int, completion: @escaping (([Posts])->())){
208+ let json = [
209+ "userId" : id,
210+ "limit": 10
211+ ]
212+
213+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
214+
215+ var request = URLRequest(url: urlForPostsForUser)
216+ request.httpMethod = "POST"
217+
218+ // insert json data to the request
219+ request.httpBody = jsonData
220+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
221+
222+ AF.request(request).responseJSON {
223+ (response) in
224+
225+ if response.response?.statusCode == 200 {
226+ print("success")
227+ }
228+
229+ let decoder = JSONDecoder()
230+ guard let json = response.data else { return }
231+
232+ guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
233+
234+ completion(newPost)
235+ }
236+ }
237+
238+ static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
239+ let json = "\(Set(ids))"
240+ print(json)
241+ var request = URLRequest(url: urlForUsers)
242+
243+ request.httpMethod = "POST"
244+
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")
248+
249+ AF.request(request).responseJSON {
250+ (response) in
251+
252+ if response.response?.statusCode == 200 {
253+ print("successddd")
254+ }
255+
256+ let decoder = JSONDecoder()
257+ guard let json = response.data else { return }
258+
259+ guard let users = try? decoder.decode([Users].self, from: json) else { return }
260+
261+ var dict: [Int:Users] = [:]
262+ users.forEach({ (user) in
263+ dict[user.id] = user
264+ })
265+
266+ completion(dict)
267+ }
268+ }
184269}