@@ -11,15 +11,174 @@ import Alamofire
1111
1212class Model{
1313 static var url = URL(string:"https://valera-denis.herokuapp.com/authenticate")!
14+ static var url1 = URL(string:"https://valera-denis.herokuapp.com/posts/last")!
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")!
17+ static var urlForUpdate = URL(string:"https://valera-denis.herokuapp.com/drafts/update")!
18+
19+ struct Posts: Codable {
20+ var body: [Attachments]
21+ var authorId: Int
22+ var id: Int
23+
24+ init(body: [Attachments], authorId: Int, id: Int) {
25+ self.body = body
26+ self.authorId = authorId
27+ self.id = id
28+ }
29+ }
30+
31+ struct Attachments: Codable {
32+ var markdown: String
33+
34+ init(markdown: String) {
35+ self.markdown = markdown
36+ }
37+ }
1438
1539 static func authenticate(){
1640
17− let params: Parameters = ["authenticationId" : 9]
18− AF.request(url, method: .post, parameters: params).response{
19− (responce) in
41+ let json: [String:Any] = ["authenticationId" : 9]
42+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
43+
44+ var request = URLRequest(url: url)
45+ request.httpMethod = "POST"
46+
47+ // insert json data to the request
48+ request.httpBody = jsonData
49+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
50+
51+ AF.request(request).response { (responce) in
52+
53+ guard let realResponse = responce.response, realResponse.statusCode == 204 else {
54+ print("Not a 204 response")
55+ return
56+ }
57+
58+ let fields = realResponse.allHeaderFields as? [String :String]
59+
60+ let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
61+
62+
63+ for cookie in cookies {
64+ HTTPCookieStorage.shared.setCookie(cookie)
65+ DataStorage.standard.cookie = cookie
66+ }
67+ }
68+ }
69+
70+
71+ static func getLast(completion: @escaping ((Channel)->())){
72+ let jsonString = "100"
73+ var request = URLRequest(url: url1)
74+
75+ request.httpMethod = "POST"
76+
77+ // insert json data to the request
78+ request.httpBody = jsonString.data(using: .utf8)
79+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
80+
81+ AF.request(request).responseJSON {
82+ (response) in
83+
84+ if response.response?.statusCode == 200 {
85+ print("success")
86+ }
87+
88+ let decoder = JSONDecoder()
89+ guard let json = response.data else { return }
90+
91+ guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
2092
21− print(responce.response?.allHeaderFields)
93+ completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
2294 }
95+ }
96+
97+ static func createDraft(completion: @escaping ((Int)->())){
98+ let jsonString = "[{ \"markdown\": \"# This is a markdown title\nThis is body.\" }]"
99+ var request = URLRequest(url: urlForDrafts)
100+
101+ request.httpMethod = "POST"
102+
103+ // insert json data to the request
104+ request.httpBody = jsonString.data(using: .utf8)
105+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
106+
107+ AF.request(request).responseJSON {
108+ (response) in
109+
110+ if response.response?.statusCode == 200 {
111+ print("success in creating draft")
112+ } else {
113+ print("\(response.response?.statusCode ?? 0)")
114+ }
115+
116+ //let decoder = JSONDecoder()
117+ guard let json = response.data else { return }
118+
119+ guard let id = String(data: json, encoding: String.Encoding.utf8) else {return}
120+
121+ print("with id \(id)")
122+ completion(Int(id) ?? 0)
123+ }
124+ }
125+
126+ static var seeMe: Int = 0 {
127+ didSet{
128+ publish()
129+ }
130+ }
131+
132+ static func publish(){
133+ let jsonUpd = "\(seeMe)"
134+ var request = URLRequest(url: urlForPublish)
23135
136+ request.httpMethod = "POST"
137+
138+ // insert json data to the request
139+ request.httpBody = jsonUpd.data(using: .utf8)
140+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
141+ AF.request(request).response {
142+ (response) in
143+
144+ if response.response?.statusCode == 204 {
145+ print("successfuly published \(jsonUpd)")
146+ }
147+ }
148+ }
149+
150+ static func update(post: Posts){
151+ let transformed = post.body.reduce("") { (res, att) -> String in
152+ res + att.markdown
153+ }
154+
155+ let jsonUpd = """
156+ {
157+ "body": [
158+ {
159+ "markdown": "\(transformed)"
160+ }
161+ ],
162+ "id": \(post.id)
163+ }
164+ """
165+ print(jsonUpd)
166+ var request = URLRequest(url: urlForUpdate)
167+
168+ request.httpMethod = "POST"
169+
170+ // insert json data to the request
171+ request.httpBody = jsonUpd.data(using: .utf8)
172+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
173+ AF.request(request).response {
174+ (response) in
175+
176+ if response.response?.statusCode == 204 {
177+ print("successfuly updated \(post.id) with \(jsonUpd)")
178+ seeMe = post.id
179+ } else {
180+ print(response.response?.statusCode)
181+ }
182+ }
24183 }
25184}