@@ -10,6 +10,22 @@ import Foundation
1010import UIKit
1111import Alamofire
1212
13+enum ResultR: Int {
14+ case internalServerError = 500
15+ case exceededContent = 470
16+ case longContent = 471
17+ case incorrectContent = 472
18+ case impossibleContent = 473
19+ case invalidTocken = 498
20+ case alreadyRegistered = 409
21+ case invalidCode = 403
22+ case badAccess = 400
23+ case tooMuchToAdd = 406
24+ case notFound = 404
25+ case success1 = 204
26+ case success = 200
27+}
28+
1329class Model{
1430
1531 static let invalidTocken = 498
@@ -17,7 +33,7 @@ class Model{
1733 static var hashTagTree: CompletionTree?
1834
1935 private static var isValidTocken: ((Int)->())? = { responce in
20−
36+ print(responce)
2137 if responce == invalidTocken {
2238
2339 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
@@ -33,6 +49,7 @@ class Model{
3349 static let decoder = JSONDecoder()
3450
3551 static let authMeURL = URL(string: "\(baseUrl)/authentication/me")!
52+ static let deactivateURL = URL(string: "\(baseUrl)/deactivateAll")!
3653 static let registerMeURL = URL(string: "\(baseUrl)/authentication/register")!
3754 static let authVerifyURL = URL(string: "\(baseUrl)/authentication/verify")!
3855 static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
@@ -42,6 +59,7 @@ class Model{
4259 static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
4360 static let usersURL = URL(string:"\(baseUrl)/users")!
4461 static let usersAllURL = URL(string:"\(baseUrl)/users/all")!
62+ static let usersUpdateURL = URL(string:"\(baseUrl)/users/update")!
4563 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
4664 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
4765 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
@@ -251,15 +269,13 @@ class Model{
251269 }
252270
253271 if code == 204 {
254− let fields = responce.response?.allHeaderFields as? [String :String]
255272
273+ let fields = responce.response?.allHeaderFields as? [String :String]
256274 let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: responce.response!.url!)
257−
258275 HTTPCookieStorage.shared.setCookie(cookies[0])
259276
260277 completion()
261278 }
262−
263279 }
264280 }
265281
@@ -470,7 +486,7 @@ class Model{
470486
471487 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
472488 let json = "\(Set(ids))"
473− print(json)
489+
474490 var request = URLRequest(url: usersURL)
475491
476492 request.httpMethod = "POST"
@@ -534,7 +550,7 @@ class Model{
534550 }
535551
536552
537− static func createChannel(with channel: Channels) {
553+ static func createChannel(with channel: Channels, completion: @escaping ((ResultR)->())) {
538554
539555 var request = URLRequest(url: channelsCreateURL)
540556 request.httpMethod = "POST"
@@ -545,9 +561,13 @@ class Model{
545561 (response) in
546562
547563 isValidTocken?(response.response?.statusCode ?? 498)
548− if let code = response.response?.statusCode, code == 498 {
564+
565+ if let code = response.response?.statusCode, let result = ResultR(rawValue: code) {
566+ completion(result)
549567 return
550568 }
569+
570+ completion(ResultR.internalServerError)
551571 }
552572
553573 }
@@ -709,8 +729,6 @@ class Model{
709729 guard let json = response.data else { return }
710730
711731 guard let newQueery = try? decoder.decode(QueryPosts<Posts>.self, from: json) else { return }
712−
713− print("\(anonymousChannel) \(newQueery)")
714732 completion((newQueery.users, newQueery.response))
715733 }
716734 }
@@ -1081,4 +1099,40 @@ class Model{
10811099 completion(faculties)
10821100 }
10831101 }
1102+
1103+ static func userUpdate(with newUser: NewRegistration, completion: @escaping ((Bool)->())) {
1104+ var request = URLRequest(url: usersUpdateURL)
1105+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
1106+ request.httpMethod = "POST"
1107+ request.httpBody = try? JSONEncoder().encode(newUser)
1108+
1109+ AF.request(request).response { (response) in
1110+ isValidTocken?(response.response?.statusCode ?? 498)
1111+
1112+ if let code = response.response?.statusCode, code == 498 {
1113+ return
1114+ }
1115+
1116+ if let code = response.response?.statusCode, code != 204{
1117+ completion (false)
1118+ } else {
1119+ completion(true)
1120+ }
1121+ }
1122+ }
1123+
1124+ static func deactivateAll(completion: @escaping (()->())){
1125+ var request = URLRequest(url: deactivateURL)
1126+ request.httpMethod = "DELETE"
1127+
1128+ AF.request(request).response { (responce) in
1129+ isValidTocken?(responce.response?.statusCode ?? 498)
1130+
1131+ if let code = responce.response?.statusCode, code == 498 {
1132+ return
1133+ }
1134+
1135+ completion()
1136+ }
1137+ }
10841138}