-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
326 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NetworkResult.swift | ||
// SOPKATHON_Kick | ||
// | ||
// Created by 한상진 on 2021/05/23. | ||
// | ||
|
||
import Foundation | ||
|
||
enum NetworkResult<T> { | ||
case success(T) | ||
case requestErr(T) | ||
case pathErr | ||
case serverErr | ||
case networkFail | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Any>) -> 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<Any> { | ||
|
||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Any>) -> 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<Any> { | ||
|
||
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.