git.iko.soy/dashared/GDproject
on master help sign in
working with denis: channels can be created, updated and deleted.
333da26Darya Rednikina
GDproject/Controller/ News and channels/ChannelController.swift+9−1
@@ -58,9 +58,17 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
58 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
58 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
59 }
59 }
60
60
61+ // TODO: update channel
61 override func viewWillDisappear(_ animated: Bool) {
62 override func viewWillDisappear(_ animated: Bool) {
63+ if let _ = channel?.id {
64+ print("update")
65+ Model.updateChannel(with: channel!)
66+ } else {
67+ print("create")
62− Model.createChannel(with: channel!)
68+ Model.createChannel(with: channel!)
69+ }
63 }
70 }
71+
64 @objc func changedText(_ textField: UITextField){
72 @objc func changedText(_ textField: UITextField){
65 navigationItem.title = textField.text
73 navigationItem.title = textField.text
66 channel?.name = textField.text!
74 channel?.name = textField.text!
GDproject/Controller/ News and channels/ChannelListController.swift+13−9
@@ -25,6 +25,12 @@ class ChannelListController: UITableViewController, DataDelegate {
25 var myProtocol: DataDelegate?
25 var myProtocol: DataDelegate?
26 var displayingChannel: Model.Channels?
26 var displayingChannel: Model.Channels?
27
27
28+ var toReload: Bool = false {
29+ didSet{
30+ tableView.reloadData()
31+ }
32+ }
33+
28 var isFiltering: Bool {
34 var isFiltering: Bool {
29 return searchController.isActive && !searchBarIsEmpty()
35 return searchController.isActive && !searchBarIsEmpty()
30 }
36 }
@@ -70,6 +76,7 @@ class ChannelListController: UITableViewController, DataDelegate {
70 searchController.isActive = false
76 searchController.isActive = false
71 Model.channelsList { [weak self] (channels) in
77 Model.channelsList { [weak self] (channels) in
72 self?.dataSource = [ChannelListController.generalChannel] + channels
78 self?.dataSource = [ChannelListController.generalChannel] + channels
79+ self?.toReload = true
73 }
80 }
74 }
81 }
75
82
@@ -85,14 +92,7 @@ class ChannelListController: UITableViewController, DataDelegate {
85
92
86 static let generalChannel = Model.Channels(people: [], name: "General", id: -1, tags: [])
93 static let generalChannel = Model.Channels(people: [], name: "General", id: -1, tags: [])
87
94
88− var dataSource : [Model.Channels] = [
95+ var dataSource : [Model.Channels] = [ ChannelListController.generalChannel ]
89− Model.Channels(people: [], name: "General", id: -1, tags: [])
90− ]
91− {
92− didSet{
93− self.tableView.reloadData()
94− }
95− }
96
96
97 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
97 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
98 if isFiltering {
98 if isFiltering {
@@ -143,6 +143,11 @@ class ChannelListController: UITableViewController, DataDelegate {
143
143
144
144
145 let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { [weak self] (action, indexPath) in
145 let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { [weak self] (action, indexPath) in
146+
147+ Model.channelsDelete(by: self!.dataSource[indexPath.row].id!, completion: {
148+ print("хз что тут делать при успехе")
149+ })
150+
146 if (self!.dataSource[indexPath.row].id == self!.displayingChannel?.id ?? -1)
151 if (self!.dataSource[indexPath.row].id == self!.displayingChannel?.id ?? -1)
147 {
152 {
148 self?.myProtocol?.passData(for: 0, channel: self!.dataSource[0])
153 self?.myProtocol?.passData(for: 0, channel: self!.dataSource[0])
@@ -154,7 +159,6 @@ class ChannelListController: UITableViewController, DataDelegate {
154 }
159 }
155 deleteButton.backgroundColor = .red
160 deleteButton.backgroundColor = .red
156
161
157−
158 return [editButton, deleteButton]
162 return [editButton, deleteButton]
159 }
163 }
160
164
GDproject/Controller/ News and channels/NewsController.swift−1
@@ -41,7 +41,6 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
41 })
41 })
42
42
43 news.dataSourse = newPosts
43 news.dataSourse = newPosts
44− print("and heeeere in dict")
45 tableView.reloadData()
44 tableView.reloadData()
46 }
45 }
47 }
46 }
GDproject/Simple model/Model.swift+40−15
@@ -13,9 +13,7 @@ class Model{
13
13
14 static let invalidTocken = 498
14 static let invalidTocken = 498
15
15
16− private static var isValidTocken: ((Int)->())? = {
16+ private static var isValidTocken: ((Int)->())? = { responce in
17− responce in
18− print("\(responce)")
19 if responce == invalidTocken {
17 if responce == invalidTocken {
20 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
18 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
21 }
19 }
@@ -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()
26
24
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")!
36
35
37
36
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)
159 }
158 }
160−
161 }
159 }
162
160
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)
167
165
168− var request = URLRequest(url: url)
166+ var request = URLRequest(url: authenticationURL)
169 request.httpMethod = "POST"
167 request.httpMethod = "POST"
170
168
171 // insert json data to the request
169 // insert json data to the request
@@ -201,7 +199,7 @@ class Model{
201
199
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)
205
203
206 request.httpMethod = "POST"
204 request.httpMethod = "POST"
207
205
@@ -227,7 +225,7 @@ class Model{
227
225
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)
231
229
232 request.httpMethod = "POST"
230 request.httpMethod = "POST"
233
231
@@ -251,7 +249,7 @@ class Model{
251
249
252 let jsonData = try? JSONSerialization.data(withJSONObject: json)
250 let jsonData = try? JSONSerialization.data(withJSONObject: json)
253
251
254− var request = URLRequest(url: urlForPostsForUser)
252+ var request = URLRequest(url: postsForUserURL)
255 request.httpMethod = "POST"
253 request.httpMethod = "POST"
256
254
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)
280
278
281 request.httpMethod = "POST"
279 request.httpMethod = "POST"
282
280
@@ -364,4 +362,31 @@ class Model{
364 completion(channelsList)
362 completion(channelsList)
365 }
363 }
366 }
364 }
365+
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)
370+ print("\(id)")
371+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
372+
373+ AF.request(request).response { (response) in
374+ isValidTocken?(response.response?.statusCode ?? 498)
375+ }
376+ completion()
377+ }
378+
379+ static func updateChannel(with channel: Channels) {
380+
381+ var request = URLRequest(url: channelsUpdateURL)
382+ request.httpMethod = "POST"
383+ request.httpBody = try? JSONEncoder().encode(channel)
384+
385+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
386+ AF.request(request).response {
387+ (response) in
388+
389+ isValidTocken?(response.response?.statusCode ?? 498)
390+ }
391+ }
367}
392}