From d814d65b794e49750f2ad2dfa03c33ea182d0713 Mon Sep 17 00:00:00 2001 From: Hansangjin98 Date: Sun, 23 May 2021 05:38:39 +0900 Subject: [PATCH] =?UTF-8?q?=ED=85=8C=EC=9D=B4=EB=B8=94=EB=B7=B0=20?= =?UTF-8?q?=EC=84=9C=EB=B2=84=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SOPKATHON_Kick.xcodeproj/project.pbxproj | 44 ++++++++++++++ SOPKATHON_Kick/Common/API/APIConstants.swift | 14 +++++ SOPKATHON_Kick/Common/API/DataModel.swift | 41 +++++++++++++ SOPKATHON_Kick/Common/API/NetworkResult.swift | 16 ++++++ .../Common/API/PopularWritingService.swift | 57 +++++++++++++++++++ .../Common/API/RecentWritingService.swift | 57 +++++++++++++++++++ .../Common/Struct/countDataModel.swift | 29 ++++++++++ .../Common/Struct/dateDataModel.swift | 21 +++++++ .../Sources/Home/HomeViewController.swift | 42 +++++++++++--- 9 files changed, 312 insertions(+), 9 deletions(-) create mode 100644 SOPKATHON_Kick/Common/API/APIConstants.swift create mode 100644 SOPKATHON_Kick/Common/API/DataModel.swift create mode 100644 SOPKATHON_Kick/Common/API/NetworkResult.swift create mode 100644 SOPKATHON_Kick/Common/API/PopularWritingService.swift create mode 100644 SOPKATHON_Kick/Common/API/RecentWritingService.swift create mode 100644 SOPKATHON_Kick/Common/Struct/countDataModel.swift create mode 100644 SOPKATHON_Kick/Common/Struct/dateDataModel.swift diff --git a/SOPKATHON_Kick.xcodeproj/project.pbxproj b/SOPKATHON_Kick.xcodeproj/project.pbxproj index 41d7fab..c1dca03 100644 --- a/SOPKATHON_Kick.xcodeproj/project.pbxproj +++ b/SOPKATHON_Kick.xcodeproj/project.pbxproj @@ -21,6 +21,13 @@ 403917842659451100BF2C6F /* MyPage.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 403917832659451100BF2C6F /* MyPage.storyboard */; }; 403917872659462100BF2C6F /* MainTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 403917862659462100BF2C6F /* MainTabBarController.swift */; }; 4039178E265947D200BF2C6F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 403917712659434900BF2C6F /* Main.storyboard */; }; + 4040303226598F5600709B7F /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4040303126598F5600709B7F /* APIConstants.swift */; }; + 4040303526598FC500709B7F /* DataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4040303426598FC500709B7F /* DataModel.swift */; }; + 404030382659900D00709B7F /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 404030372659900D00709B7F /* NetworkResult.swift */; }; + 4040303B2659903000709B7F /* RecentWritingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4040303A2659903000709B7F /* RecentWritingService.swift */; }; + 404030402659928100709B7F /* dateDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4040303F2659928100709B7F /* dateDataModel.swift */; }; + 40403045265999DE00709B7F /* countDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40403044265999DE00709B7F /* countDataModel.swift */; }; + 4040304826599C9B00709B7F /* PopularWritingService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4040304726599C9B00709B7F /* PopularWritingService.swift */; }; 4058242726593B5B00845A48 /* Login.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4058242626593B5B00845A48 /* Login.storyboard */; }; 4058242B26593BAB00845A48 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4058242A26593BAB00845A48 /* LoginViewController.swift */; }; 40F598E22659568F005DD065 /* HomeCollectionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40F598E12659568F005DD065 /* HomeCollectionViewModel.swift */; }; @@ -48,6 +55,13 @@ 403917802659450A00BF2C6F /* MyPageViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageViewController.swift; sourceTree = ""; }; 403917832659451100BF2C6F /* MyPage.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = MyPage.storyboard; sourceTree = ""; }; 403917862659462100BF2C6F /* MainTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabBarController.swift; sourceTree = ""; }; + 4040303126598F5600709B7F /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = ""; }; + 4040303426598FC500709B7F /* DataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataModel.swift; sourceTree = ""; }; + 404030372659900D00709B7F /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = ""; }; + 4040303A2659903000709B7F /* RecentWritingService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentWritingService.swift; sourceTree = ""; }; + 4040303F2659928100709B7F /* dateDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = dateDataModel.swift; sourceTree = ""; }; + 40403044265999DE00709B7F /* countDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = countDataModel.swift; sourceTree = ""; }; + 4040304726599C9B00709B7F /* PopularWritingService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopularWritingService.swift; sourceTree = ""; }; 4058242626593B5B00845A48 /* Login.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Login.storyboard; sourceTree = ""; }; 4058242A26593BAB00845A48 /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = ""; }; 40F598E12659568F005DD065 /* HomeCollectionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeCollectionViewModel.swift; sourceTree = ""; }; @@ -102,6 +116,27 @@ path = SOPKATHON_Kick; sourceTree = ""; }; + 4040303026598F4500709B7F /* API */ = { + isa = PBXGroup; + children = ( + 4040303126598F5600709B7F /* APIConstants.swift */, + 4040303426598FC500709B7F /* DataModel.swift */, + 404030372659900D00709B7F /* NetworkResult.swift */, + 4040303A2659903000709B7F /* RecentWritingService.swift */, + 4040304726599C9B00709B7F /* PopularWritingService.swift */, + ); + path = API; + sourceTree = ""; + }; + 4040303E2659924500709B7F /* Struct */ = { + isa = PBXGroup; + children = ( + 4040303F2659928100709B7F /* dateDataModel.swift */, + 40403044265999DE00709B7F /* countDataModel.swift */, + ); + path = Struct; + sourceTree = ""; + }; 4058241D265939C700845A48 /* Application */ = { isa = PBXGroup; children = ( @@ -218,6 +253,8 @@ 40F598E626595E65005DD065 /* Common */ = { isa = PBXGroup; children = ( + 4040303E2659924500709B7F /* Struct */, + 4040303026598F4500709B7F /* API */, 40F598E726595E6C005DD065 /* Extension */, ); path = Common; @@ -370,13 +407,20 @@ 403917752659447300BF2C6F /* WritingViewController.swift in Sources */, 4058242B26593BAB00845A48 /* LoginViewController.swift in Sources */, 40F598F626596D7B005DD065 /* HomeTableViewCell.swift in Sources */, + 404030402659928100709B7F /* dateDataModel.swift in Sources */, 403917872659462100BF2C6F /* MainTabBarController.swift in Sources */, 40F598EF26596594005DD065 /* HomeTableViewModel.swift in Sources */, + 4040303B2659903000709B7F /* RecentWritingService.swift in Sources */, + 4040303526598FC500709B7F /* DataModel.swift in Sources */, 403917812659450A00BF2C6F /* MyPageViewController.swift in Sources */, + 404030382659900D00709B7F /* NetworkResult.swift in Sources */, 400CFA0A265932CF0080CB1B /* AppDelegate.swift in Sources */, 40F598E22659568F005DD065 /* HomeCollectionViewModel.swift in Sources */, + 4040303226598F5600709B7F /* APIConstants.swift in Sources */, 400CFA0C265932CF0080CB1B /* SceneDelegate.swift in Sources */, + 4040304826599C9B00709B7F /* PopularWritingService.swift in Sources */, 4039176A2659416F00BF2C6F /* HomeViewController.swift in Sources */, + 40403045265999DE00709B7F /* countDataModel.swift in Sources */, 4039177E265944D500BF2C6F /* KingViewController.swift in Sources */, 40F598E926595E86005DD065 /* UIColor.swift in Sources */, 40F598F326596C59005DD065 /* HomeCollectionViewCell.swift in Sources */, diff --git a/SOPKATHON_Kick/Common/API/APIConstants.swift b/SOPKATHON_Kick/Common/API/APIConstants.swift new file mode 100644 index 0000000..9fd6b8a --- /dev/null +++ b/SOPKATHON_Kick/Common/API/APIConstants.swift @@ -0,0 +1,14 @@ +// +// APIConstants.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation +struct APIConstants{ + + // MARK:- Base URL + static let dateURL = "http://13.209.70.37:5000/api/post/date" + static let countURL = "http://13.209.70.37:5000/api/post/count" +} diff --git a/SOPKATHON_Kick/Common/API/DataModel.swift b/SOPKATHON_Kick/Common/API/DataModel.swift new file mode 100644 index 0000000..de10429 --- /dev/null +++ b/SOPKATHON_Kick/Common/API/DataModel.swift @@ -0,0 +1,41 @@ +// +// DataModel.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation + +// MARK: - LoginDataModel +struct DataModel: Codable { + let success: Bool + let message: String + let data: UserData? + + enum CodingKeys: String, CodingKey { + case success + case message + case data + } + + init(from decoder : Decoder) throws + { + let values = try decoder.container(keyedBy: CodingKeys.self) + success = (try? values.decode(Bool.self, forKey: .success)) ?? false + message = (try? values.decode(String.self, forKey: .message)) ?? "" + data = (try? values.decode(UserData.self, forKey: .data)) ?? nil + } +} + +// MARK: - UserData +struct UserData: Codable { + let userID: Int + let userNickname, token: String + + enum CodingKeys: String, CodingKey { + case userID = "UserId" + case userNickname = "user_nickname" + case token + } +} diff --git a/SOPKATHON_Kick/Common/API/NetworkResult.swift b/SOPKATHON_Kick/Common/API/NetworkResult.swift new file mode 100644 index 0000000..df0d42c --- /dev/null +++ b/SOPKATHON_Kick/Common/API/NetworkResult.swift @@ -0,0 +1,16 @@ +// +// NetworkResult.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation + +enum NetworkResult { + case success(T) + case requestErr(T) + case pathErr + case serverErr + case networkFail +} diff --git a/SOPKATHON_Kick/Common/API/PopularWritingService.swift b/SOPKATHON_Kick/Common/API/PopularWritingService.swift new file mode 100644 index 0000000..180afcd --- /dev/null +++ b/SOPKATHON_Kick/Common/API/PopularWritingService.swift @@ -0,0 +1,57 @@ +// +// PopularWritingService.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation +import Alamofire + +struct PopularWriting{ + static let shared = PopularWriting() + + + + func getData( + completion : @escaping (NetworkResult) -> Void) + { + + let url = "http://13.209.70.37:5000/api/post/count" + let header : HTTPHeaders = ["Content-Type": "application/json"] + let dataRequest = AF.request(url, method: .get, encoding: JSONEncoding.default) + + dataRequest.responseData { dataResponse in + + dump(dataResponse) + + switch dataResponse.result { + case .success: + guard let statusCode = dataResponse.response?.statusCode else {return} + guard let value = dataResponse.value else {return} + let networkResult = self.judgeStatus(by: statusCode, value) + completion(networkResult) + + case .failure: completion(.pathErr) + + } + } + + } + + private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { + + let decoder = JSONDecoder() + + guard let decodedData = try? decoder.decode(Date.self, from: data) + else { return .pathErr} + + switch statusCode { + + case 200: return .success(decodedData) + case 400: return .requestErr(decodedData) + case 500: return .serverErr + default: return .networkFail + } + } +} diff --git a/SOPKATHON_Kick/Common/API/RecentWritingService.swift b/SOPKATHON_Kick/Common/API/RecentWritingService.swift new file mode 100644 index 0000000..03c4260 --- /dev/null +++ b/SOPKATHON_Kick/Common/API/RecentWritingService.swift @@ -0,0 +1,57 @@ +// +// NetworkService.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation +import Alamofire + +struct RecentWriting{ + static let shared = RecentWriting() + + + + func getData( + completion : @escaping (NetworkResult) -> Void) + { + + let url = "http://13.209.70.37:5000/api/post/date" + let header : HTTPHeaders = ["Content-Type": "application/json"] + let dataRequest = AF.request(url, method: .get, encoding: JSONEncoding.default) + + dataRequest.responseData { dataResponse in + + dump(dataResponse) + + switch dataResponse.result { + case .success: + guard let statusCode = dataResponse.response?.statusCode else {return} + guard let value = dataResponse.value else {return} + let networkResult = self.judgeStatus(by: statusCode, value) + completion(networkResult) + + case .failure: completion(.pathErr) + + } + } + + } + + private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult { + + let decoder = JSONDecoder() + + guard let decodedData = try? decoder.decode(Date.self, from: data) + else { return .pathErr} + + switch statusCode { + + case 200: return .success(decodedData) + case 400: return .requestErr(decodedData) + case 500: return .serverErr + default: return .networkFail + } + } +} diff --git a/SOPKATHON_Kick/Common/Struct/countDataModel.swift b/SOPKATHON_Kick/Common/Struct/countDataModel.swift new file mode 100644 index 0000000..4e46a32 --- /dev/null +++ b/SOPKATHON_Kick/Common/Struct/countDataModel.swift @@ -0,0 +1,29 @@ +// +// countDataModel.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation + +import Foundation + +// MARK: - WelcomeElement +import Foundation + +// MARK: - WelcomeElement +struct CountElement: Codable { + let id: String + let kickCount: Int + let timestamp, contents, title: String + + enum CodingKeys: String, CodingKey { + case id = "_id" + case kickCount = "kick_count" + case timestamp, contents, title + } +} + +typealias Welcome = [CountElement] + diff --git a/SOPKATHON_Kick/Common/Struct/dateDataModel.swift b/SOPKATHON_Kick/Common/Struct/dateDataModel.swift new file mode 100644 index 0000000..fc4c568 --- /dev/null +++ b/SOPKATHON_Kick/Common/Struct/dateDataModel.swift @@ -0,0 +1,21 @@ +// +// dateDataModel.swift +// SOPKATHON_Kick +// +// Created by 한상진 on 2021/05/23. +// + +import Foundation + +struct DateElement: Codable { + let id: String + let kickCount: Int + let timestamp, contents, title: String + + enum CodingKeys: String, CodingKey { + case id = "_id" + case kickCount = "kick_count" + case timestamp, contents, title + } +} +typealias Date = [DateElement] diff --git a/SOPKATHON_Kick/Sources/Home/HomeViewController.swift b/SOPKATHON_Kick/Sources/Home/HomeViewController.swift index 3728928..b46f5a4 100644 --- a/SOPKATHON_Kick/Sources/Home/HomeViewController.swift +++ b/SOPKATHON_Kick/Sources/Home/HomeViewController.swift @@ -16,7 +16,8 @@ class HomeViewController: UIViewController { @IBOutlet weak var homePageControl: UIPageControl! var homeCollectionViewList: [HomeCollectionViewModel] = [] var homeTableViewList: [HomeTableViewModel] = [] - + var dateList : [DateElement] = [] +// var countList : [CountElement] = [] override func viewDidLoad() { super.viewDidLoad() @@ -26,6 +27,30 @@ class HomeViewController: UIViewController { homeCollectionViewSet() setScrollHeight() pageCtrlSet() + getDate() +// getCount() + } + + func getDate() + { + RecentWriting.shared.getData { (result) in + switch(result) + { + case .success(let dateObject): + if let date = dateObject as? [DateElement]{ + self.dateList = date + self.homeTableView.reloadData() + } + case .requestErr(_): + return + case .pathErr: + return + case .serverErr: + return + case .networkFail: + return + } + } } func homeTableViewSet() { @@ -43,7 +68,6 @@ class HomeViewController: UIViewController { HomeTableViewModel(title: "테스트", contents: "테ㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔ테스트", click: 32, minute: 15, like: 302), HomeTableViewModel(title: "테스트", contents: "테ㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔ테스트", click: 32, minute: 15, like: 302), HomeTableViewModel(title: "테스트", contents: "테ㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔㅔ테스트", click: 32, minute: 15, like: 302) - ]) } @@ -62,7 +86,7 @@ class HomeViewController: UIViewController { func setScrollHeight() { - scrollViewHeightConstraint.constant = CGFloat(homeTableViewList.count * 59) + scrollViewHeightConstraint.constant = CGFloat(homeTableViewList.count * 70) homeTableView.reloadData() } @@ -74,7 +98,7 @@ class HomeViewController: UIViewController { extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return homeCollectionViewList.count + return dateList.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { @@ -91,17 +115,17 @@ extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSour extension HomeViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return homeTableViewList.count + return dateList.count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 91 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? HomeTableViewCell else { return UITableViewCell() } - cell.title.text = homeTableViewList[indexPath.row].title - cell.content.text = homeTableViewList[indexPath.row].contents - cell.clickAndTime.text = "조회 " + "\(homeTableViewList[indexPath.row].click)" + "•" + "\(homeTableViewList[indexPath.row].minute)" + " 전" - cell.like.text = "\(homeTableViewList[indexPath.row].like)" + cell.title.text = dateList[indexPath.row].title + cell.content.text = dateList[indexPath.row].contents + cell.clickAndTime.text = "\(dateList[indexPath.row].timestamp)" + " 전" + cell.like.text = "\(dateList[indexPath.row].kickCount)" return cell } }