@@ -13,9 +13,7 @@ class Model{
14 static let invalidTocken = 498
14 static let invalidTocken = 498
16− private static var isValidTocken: ((Int)->())? = {
16+ private static var isValidTocken: ((Int)->())? = { responce in
19 if responce == invalidTocken {
17 if responce == invalidTocken {
20 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -24,15 +22,16 @@ class Model{
24 private static let baseUrl = "https://valera-denis.herokuapp.com"
22 private static let baseUrl = "https://valera-denis.herokuapp.com"
25 static let decoder = JSONDecoder()
23 static let decoder = JSONDecoder()
27− static let url = URL(string:"\(baseUrl)/authentication/login")!
25+ static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
28− static let url1 = URL(string:"\(baseUrl)/posts/last")!
26+ static let postsLastURL = URL(string:"\(baseUrl)/posts/last")!
29− static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
27+ static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
28+ static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
30− static let urlForUsers = URL(string:"\(baseUrl)/users")!
29+ static let usersURL = URL(string:"\(baseUrl)/users")!
31− static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
32 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
30 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
33 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
31 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
34 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
32 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
35 static let channelsCreateURL = URL(string: "\(baseUrl)/channels/create")!
33 static let channelsCreateURL = URL(string: "\(baseUrl)/channels/create")!
34+ static let channelsDeleteURL = URL(string: "\(baseUrl)/channels/delete")!
38 struct QueryPosts: Codable{
37 struct QueryPosts: Codable{
@@ -154,10 +153,9 @@ class Model{
154 var container = encoder.container(keyedBy: CodingKeys.self)
153 var container = encoder.container(keyedBy: CodingKeys.self)
155 try container.encode(people, forKey: .people)
154 try container.encode(people, forKey: .people)
156 try container.encode(name, forKey: .name)
155 try container.encode(name, forKey: .name)
157− // try container.encode(id, forKey: .id)
156+ try container.encode(id, forKey: .id)
158 try container.encode(tags, forKey: .tags)
157 try container.encode(tags, forKey: .tags)
163 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
161 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -165,7 +163,7 @@ class Model{
165 let json: [String:Any] = ["authenticationId" : id]
163 let json: [String:Any] = ["authenticationId" : id]
166 let jsonData = try? JSONSerialization.data(withJSONObject: json)
164 let jsonData = try? JSONSerialization.data(withJSONObject: json)
168− var request = URLRequest(url: url)
166+ var request = URLRequest(url: authenticationURL)
169 request.httpMethod = "POST"
167 request.httpMethod = "POST"
171 // insert json data to the request
169 // insert json data to the request
@@ -201,7 +199,7 @@ class Model{
202 static func getLast(completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
200 static func getLast(completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
203 let jsonString = "30"
201 let jsonString = "30"
204− var request = URLRequest(url: url1)
202+ var request = URLRequest(url: postsLastURL)
206 request.httpMethod = "POST"
204 request.httpMethod = "POST"
@@ -227,7 +225,7 @@ class Model{
228 static func createAndPublish(body: [Attachments], tags: [String]){
226 static func createAndPublish(body: [Attachments], tags: [String]){
229 let jsonUpd = CreatedPost(body: body, tags: tags)
227 let jsonUpd = CreatedPost(body: body, tags: tags)
230− var request = URLRequest(url: createAndPublishURL)
228+ var request = URLRequest(url: postsPublishURL)
232 request.httpMethod = "POST"
230 request.httpMethod = "POST"
@@ -251,7 +249,7 @@ class Model{
252 let jsonData = try? JSONSerialization.data(withJSONObject: json)
250 let jsonData = try? JSONSerialization.data(withJSONObject: json)
254− var request = URLRequest(url: urlForPostsForUser)
252+ var request = URLRequest(url: postsForUserURL)
255 request.httpMethod = "POST"
253 request.httpMethod = "POST"
257 // insert json data to the request
255 // insert json data to the request
@@ -276,7 +274,7 @@ class Model{
276 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
274 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
277 let json = "\(Set(ids))"
275 let json = "\(Set(ids))"
278 print(json)
276 print(json)
279− var request = URLRequest(url: urlForUsers)
277+ var request = URLRequest(url: usersURL)
281 request.httpMethod = "POST"
279 request.httpMethod = "POST"
@@ -364,4 +362,31 @@ class Model{
364 completion(channelsList)
362 completion(channelsList)
366+ static func channelsDelete(by id: Int, completion: @escaping(()->())){
367+ var request = URLRequest(url: channelsDeleteURL)
368+ request.httpMethod = "POST"
369+ request.httpBody = "\(id)".data(using: .utf8)
371+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
373+ AF.request(request).response { (response) in
374+ isValidTocken?(response.response?.statusCode ?? 498)
379+ static func updateChannel(with channel: Channels) {
381+ var request = URLRequest(url: channelsUpdateURL)
382+ request.httpMethod = "POST"
383+ request.httpBody = try? JSONEncoder().encode(channel)
385+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
386+ AF.request(request).response {
389+ isValidTocken?(response.response?.statusCode ?? 498)