git.iko.soy/dashared/GDproject
on master help sign in
working with denis: channels can be created, updated and deleted.
commit333da2671a728122528df2b5747ba5d86143d611 authorDarya Rednikina <rednikina.com@yandex.ru> dateWed 13 Mar 2019, 15:12 (8 years ago) parent8cecd53
4 files changed+62−26
GDproject/Controller/ News and channels/ChannelController.swift+9−1 GDproject/Controller/ News and channels/ChannelListController.swift+13−9 GDproject/Controller/ News and channels/NewsController.swift−1 GDproject/Simple model/Model.swift+40−15
GDproject/Controller/ News and channels/ChannelController.swift+9−1
@@ -58,9 +58,17 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
5858 textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
5959 }
6060
61+ // TODO: update channel
6162 override func viewWillDisappear(_ animated: Bool) {
62− Model.createChannel(with: channel!)
63+ if let _ = channel?.id {
64+ print("update")
65+ Model.updateChannel(with: channel!)
66+ } else {
67+ print("create")
68+ Model.createChannel(with: channel!)
69+ }
6370 }
71+
6472 @objc func changedText(_ textField: UITextField){
6573 navigationItem.title = textField.text
6674 channel?.name = textField.text!
GDproject/Controller/ News and channels/ChannelListController.swift+13−9
@@ -25,6 +25,12 @@ class ChannelListController: UITableViewController, DataDelegate {
2525 var myProtocol: DataDelegate?
2626 var displayingChannel: Model.Channels?
2727
28+ var toReload: Bool = false {
29+ didSet{
30+ tableView.reloadData()
31+ }
32+ }
33+
2834 var isFiltering: Bool {
2935 return searchController.isActive && !searchBarIsEmpty()
3036 }
@@ -70,6 +76,7 @@ class ChannelListController: UITableViewController, DataDelegate {
7076 searchController.isActive = false
7177 Model.channelsList { [weak self] (channels) in
7278 self?.dataSource = [ChannelListController.generalChannel] + channels
79+ self?.toReload = true
7380 }
7481 }
7582
@@ -85,14 +92,7 @@ class ChannelListController: UITableViewController, DataDelegate {
8592
8693 static let generalChannel = Model.Channels(people: [], name: "General", id: -1, tags: [])
8794
88− var dataSource : [Model.Channels] = [
89− Model.Channels(people: [], name: "General", id: -1, tags: [])
90− ]
91− {
92− didSet{
93− self.tableView.reloadData()
94− }
95− }
95+ var dataSource : [Model.Channels] = [ ChannelListController.generalChannel ]
9696
9797 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
9898 if isFiltering {
@@ -143,6 +143,11 @@ class ChannelListController: UITableViewController, DataDelegate {
143143
144144
145145 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+
146151 if (self!.dataSource[indexPath.row].id == self!.displayingChannel?.id ?? -1)
147152 {
148153 self?.myProtocol?.passData(for: 0, channel: self!.dataSource[0])
@@ -154,7 +159,6 @@ class ChannelListController: UITableViewController, DataDelegate {
154159 }
155160 deleteButton.backgroundColor = .red
156161
157−
158162 return [editButton, deleteButton]
159163 }
160164
GDproject/Controller/ News and channels/NewsController.swift−1
@@ -41,7 +41,6 @@ class NewsController: UITableViewController, UISearchControllerDelegate, NewPost
4141 })
4242
4343 news.dataSourse = newPosts
44− print("and heeeere in dict")
4544 tableView.reloadData()
4645 }
4746 }
GDproject/Simple model/Model.swift+40−15
@@ -13,9 +13,7 @@ class Model{
1313
1414 static let invalidTocken = 498
1515
16− private static var isValidTocken: ((Int)->())? = {
17− responce in
18− print("\(responce)")
16+ private static var isValidTocken: ((Int)->())? = { responce in
1917 if responce == invalidTocken {
2018 DataStorage.standard.setIsLoggedIn(value: false, with: 0)
2119 }
@@ -24,15 +22,16 @@ class Model{
2422 private static let baseUrl = "https://valera-denis.herokuapp.com"
2523 static let decoder = JSONDecoder()
2624
27− static let url = URL(string:"\(baseUrl)/authentication/login")!
28− static let url1 = URL(string:"\(baseUrl)/posts/last")!
29− static let urlForPostsForUser = URL(string:"\(baseUrl)/posts/forUser")!
30− static let urlForUsers = URL(string:"\(baseUrl)/users")!
31− static let createAndPublishURL = URL(string:"\(baseUrl)/posts/publish")!
25+ static let authenticationURL = URL(string:"\(baseUrl)/authentication/login")!
26+ static let postsLastURL = URL(string:"\(baseUrl)/posts/last")!
27+ static let postsForUserURL = URL(string:"\(baseUrl)/posts/forUser")!
28+ static let postsPublishURL = URL(string:"\(baseUrl)/posts/publish")!
29+ static let usersURL = URL(string:"\(baseUrl)/users")!
3230 static let channelsGetURL = URL(string: "\(baseUrl)/channels/get")!
3331 static let channelsUpdateURL = URL(string: "\(baseUrl)/channels/update")!
3432 static let channelsListURL = URL(string: "\(baseUrl)/channels")!
3533 static let channelsCreateURL = URL(string: "\(baseUrl)/channels/create")!
34+ static let channelsDeleteURL = URL(string: "\(baseUrl)/channels/delete")!
3635
3736
3837 struct QueryPosts: Codable{
@@ -154,10 +153,9 @@ class Model{
154153 var container = encoder.container(keyedBy: CodingKeys.self)
155154 try container.encode(people, forKey: .people)
156155 try container.encode(name, forKey: .name)
157− // try container.encode(id, forKey: .id)
156+ try container.encode(id, forKey: .id)
158157 try container.encode(tags, forKey: .tags)
159158 }
160−
161159 }
162160
163161 static func authenticate(with id: Int, completion: @escaping ((Bool)->())) {
@@ -165,7 +163,7 @@ class Model{
165163 let json: [String:Any] = ["authenticationId" : id]
166164 let jsonData = try? JSONSerialization.data(withJSONObject: json)
167165
168− var request = URLRequest(url: url)
166+ var request = URLRequest(url: authenticationURL)
169167 request.httpMethod = "POST"
170168
171169 // insert json data to the request
@@ -201,7 +199,7 @@ class Model{
201199
202200 static func getLast(completion: @escaping (((users:[Int: Users], posts:[Posts]))->())){
203201 let jsonString = "30"
204− var request = URLRequest(url: url1)
202+ var request = URLRequest(url: postsLastURL)
205203
206204 request.httpMethod = "POST"
207205
@@ -227,7 +225,7 @@ class Model{
227225
228226 static func createAndPublish(body: [Attachments], tags: [String]){
229227 let jsonUpd = CreatedPost(body: body, tags: tags)
230− var request = URLRequest(url: createAndPublishURL)
228+ var request = URLRequest(url: postsPublishURL)
231229
232230 request.httpMethod = "POST"
233231
@@ -251,7 +249,7 @@ class Model{
251249
252250 let jsonData = try? JSONSerialization.data(withJSONObject: json)
253251
254− var request = URLRequest(url: urlForPostsForUser)
252+ var request = URLRequest(url: postsForUserURL)
255253 request.httpMethod = "POST"
256254
257255 // insert json data to the request
@@ -276,7 +274,7 @@ class Model{
276274 static func getUsers(for ids: [Int], completion: @escaping (([Int:Users])->())){
277275 let json = "\(Set(ids))"
278276 print(json)
279− var request = URLRequest(url: urlForUsers)
277+ var request = URLRequest(url: usersURL)
280278
281279 request.httpMethod = "POST"
282280
@@ -364,4 +362,31 @@ class Model{
364362 completion(channelsList)
365363 }
366364 }
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+ }
367392}