@@ -20,6 +20,7 @@ class Model{
2020 static let urlForUpdate = URL(string:"\(baseUrl)/drafts/update")!
2121 static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
2222 static let urlForUsers = URL(string:"\(baseUrl)/users")!
23+ static let createAndPublishURL = URL(string:"\(baseUrl)/drafts/createAndPublish")!
2324
2425 struct Posts: Codable {
2526 var body: [Attachments]
@@ -148,46 +149,16 @@ class Model{
148149 }
149150 }
150151
151− static var seeMe: Int = 0 {
152− didSet{
153− publish()
154− }
155− }
156−
157− static func publish(){
158− let jsonUpd = "\(seeMe)"
159− var request = URLRequest(url: urlForPublish)
160−
161− request.httpMethod = "POST"
162−
163− // insert json data to the request
164− request.httpBody = jsonUpd.data(using: .utf8)
165− request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
166− AF.request(request).response {
167− (response) in
168−
169− if response.response?.statusCode == 204 {
170− print("successfuly published \(jsonUpd)")
171− }
172− }
173− }
174152
175− static func update(post: Posts){
176− let transformed = post.body.reduce("") { (res, att) -> String in
177− res + att.markdown
178− }
179−
153+ static func createAndPublish(string: String){
180154 let jsonUpd = """
181− {
182− "body": [
183− {
184− "markdown": "\(transformed)"
185− }
186− ],
187− "id": \(post.id)
188− }
155+ [
156+ {
157+ "markdown": "\(string)"
158+ }
159+ ]
189160 """
190− var request = URLRequest(url: urlForUpdate)
161+ var request = URLRequest(url: createAndPublishURL)
191162
192163 request.httpMethod = "POST"
193164
@@ -198,10 +169,9 @@ class Model{
198169 (response) in
199170
200171 if response.response?.statusCode == 204 {
201− print("successfuly updated \(post.id) with \(jsonUpd)")
202− seeMe = post.id
172+ print("post created")
203173 } else {
204− print(response.response?.statusCode)
174+ print("failure in creating and publishing post")
205175 }
206176 }
207177 }