Skip to content

Commit

Permalink
Merge pull request #32 from BE-SOPT-Collaboration-Seminar-Coinone/fea…
Browse files Browse the repository at this point in the history
…tuer/#27

🎨 Network 통신 준비 완료
  • Loading branch information
hansolnoh95 authored Jun 3, 2021
2 parents de318f9 + 8a8f551 commit e4c354c
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Coinone-iOS/Coinone-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
CC272E712651872400637BCE /* NotoSansKR-Light.otf in Resources */ = {isa = PBXBuildFile; fileRef = CC272E6B2651872400637BCE /* NotoSansKR-Light.otf */; };
CC272E722651872400637BCE /* NotoSansKR-Black.otf in Resources */ = {isa = PBXBuildFile; fileRef = CC272E6C2651872400637BCE /* NotoSansKR-Black.otf */; };
CC272E732651872400637BCE /* NotoSansKR-Bold.otf in Resources */ = {isa = PBXBuildFile; fileRef = CC272E6D2651872400637BCE /* NotoSansKR-Bold.otf */; };
CC33700C2667C3C4004F067E /* SortedCoinModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC33700B2667C3C4004F067E /* SortedCoinModel.swift */; };
CC33700F2667C549004F067E /* APIConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC33700E2667C549004F067E /* APIConstants.swift */; };
CC3370112667C552004F067E /* NetworkResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3370102667C552004F067E /* NetworkResult.swift */; };
CC3370132667C5D4004F067E /* CoinFilterService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3370122667C5D4004F067E /* CoinFilterService.swift */; };
CC6608BE265450B0004DD7D2 /* PopupHsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC6608BD265450B0004DD7D2 /* PopupHsViewController.swift */; };
CC770CF8265396CF004095A6 /* UILabel+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC770CF7265396CF004095A6 /* UILabel+.swift */; };
CC770CFC265397BD004095A6 /* UIFont+.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC770CFB265397BD004095A6 /* UIFont+.swift */; };
Expand Down Expand Up @@ -74,6 +78,10 @@
CC272E6B2651872400637BCE /* NotoSansKR-Light.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSansKR-Light.otf"; sourceTree = "<group>"; };
CC272E6C2651872400637BCE /* NotoSansKR-Black.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSansKR-Black.otf"; sourceTree = "<group>"; };
CC272E6D2651872400637BCE /* NotoSansKR-Bold.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "NotoSansKR-Bold.otf"; sourceTree = "<group>"; };
CC33700B2667C3C4004F067E /* SortedCoinModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortedCoinModel.swift; sourceTree = "<group>"; };
CC33700E2667C549004F067E /* APIConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIConstants.swift; sourceTree = "<group>"; };
CC3370102667C552004F067E /* NetworkResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkResult.swift; sourceTree = "<group>"; };
CC3370122667C5D4004F067E /* CoinFilterService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoinFilterService.swift; sourceTree = "<group>"; };
CC6608BD265450B0004DD7D2 /* PopupHsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopupHsViewController.swift; sourceTree = "<group>"; };
CC770CF7265396CF004095A6 /* UILabel+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+.swift"; sourceTree = "<group>"; };
CC770CFB265397BD004095A6 /* UIFont+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -143,6 +151,7 @@
CC770D0426542DDF004095A6 /* CoinModel.swift */,
E8EC22B5265448200027E8A8 /* MyListDataModel.swift */,
CC0A1A9F26564FDA000B65B9 /* CoinListModel.swift */,
CC33700B2667C3C4004F067E /* SortedCoinModel.swift */,
);
path = Models;
sourceTree = "<group>";
Expand All @@ -160,6 +169,14 @@
path = Font;
sourceTree = "<group>";
};
CC33700D2667C51F004F067E /* NetworkServices */ = {
isa = PBXGroup;
children = (
CC3370122667C5D4004F067E /* CoinFilterService.swift */,
);
path = NetworkServices;
sourceTree = "<group>";
};
CC770CFF26540937004095A6 /* Protocol */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -210,6 +227,7 @@
CCAA9A1D264FAA2A008E50BF /* Resource */ = {
isa = PBXGroup;
children = (
CC33700D2667C51F004F067E /* NetworkServices */,
CC770CFF26540937004095A6 /* Protocol */,
CC272E672651871300637BCE /* Font */,
CCA0ECC5264FA7A4001CC563 /* AppDelegate.swift */,
Expand All @@ -218,6 +236,8 @@
CCAA9A1F264FAA7B008E50BF /* Storyboards */,
CCA0ECCE264FA7A6001CC563 /* Assets.xcassets */,
CCA0ECD3264FA7A6001CC563 /* Info.plist */,
CC33700E2667C549004F067E /* APIConstants.swift */,
CC3370102667C552004F067E /* NetworkResult.swift */,
);
path = Resource;
sourceTree = "<group>";
Expand Down Expand Up @@ -409,13 +429,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CC3370132667C5D4004F067E /* CoinFilterService.swift in Sources */,
CC0A1A9C265642E9000B65B9 /* TitleCollectionReusableView.swift in Sources */,
CCAA9A34264FAF68008E50BF /* UIView+.swift in Sources */,
CC6608BE265450B0004DD7D2 /* PopupHsViewController.swift in Sources */,
CC0E232D2652241D00B982A0 /* StockModel.swift in Sources */,
CCAA9A2B264FACDD008E50BF /* UIColor+.swift in Sources */,
CC770CF8265396CF004095A6 /* UILabel+.swift in Sources */,
CC0E232B265223FE00B982A0 /* StockTVC.swift in Sources */,
CC33700F2667C549004F067E /* APIConstants.swift in Sources */,
CC0A1A992656425A000B65B9 /* CoinListTableViewCell.swift in Sources */,
CC770CFE26540903004095A6 /* TopMenuCollectionViewCell.swift in Sources */,
CC770D03265426FA004095A6 /* FavoriteTableViewCell.swift in Sources */,
Expand All @@ -429,13 +451,15 @@
E8EC22B2265445210027E8A8 /* MyTableViewCell.swift in Sources */,
CC0E23272652232700B982A0 /* MyViewController.swift in Sources */,
CCAA9A2E264FAEB9008E50BF /* CALayer+.swift in Sources */,
CC3370112667C552004F067E /* NetworkResult.swift in Sources */,
CC272E6226517F0500637BCE /* HomeViewController.swift in Sources */,
CC0A1A9E2656434B000B65B9 /* CoinListCollectionReusableView.swift in Sources */,
CC0E232F2652244F00B982A0 /* GeoraesoVC.swift in Sources */,
CC770D0126540943004095A6 /* Identifiable.swift in Sources */,
CCA0ECC8264FA7A4001CC563 /* SceneDelegate.swift in Sources */,
CC770D0526542DDF004095A6 /* CoinModel.swift in Sources */,
CC272E6526517F1000637BCE /* TransactionViewController.swift in Sources */,
CC33700C2667C3C4004F067E /* SortedCoinModel.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
49 changes: 49 additions & 0 deletions Coinone-iOS/Coinone-iOS/Resource/APIConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// APIConstants.swift
// Coinone-iOS
//
// Created by 노한솔 on 2021/06/02.
//

import Foundation

struct APIConstants {

static let baseURL = "http://3.37.86.93:5000"

static let addCoinURL = baseURL + "/api/add-coin"
static let addUserURL = baseURL + "/api/add-user/"
static let myCoinURL = baseURL + "/api/my-coin/"
static let coinListURL = baseURL + "/api/coin-list/"
}

struct APIIndex {

var index: Index

enum Index {
case id(String)
case userId(String)
case sort(String, String)

func getIndex() -> String {
switch self {
case .id(let index):
return "/\(index)"
case .userId(let index):
return "/\(index)"
case .sort(let sort, let ascending):
return "/\(sort)/\(ascending)"
}
}
}
}

// add-user
// let url = APIConstants.addUserURL + APIIndex.init(index: .userID(parameterName)).index.getIndex()

// my-coin
// let url = APIConstants.myCoinURL + APIIndex.init(index: .id(parameterName)).index.getIndex()

// coin-list
// let url = APIConstants.coinListURL + APIIndex.init(index: .sort(parameter1, parameter2)).index.getIndex()
16 changes: 16 additions & 0 deletions Coinone-iOS/Coinone-iOS/Resource/NetworkResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NetworkResult.swift
// Coinone-iOS
//
// Created by 노한솔 on 2021/06/02.
//

import Foundation

enum NetworkResult<T> {
case success
case requestErr
case pathErr
case serverErr
case networkFail
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// CoinFilterService.swift
// Coinone-iOS
//
// Created by 노한솔 on 2021/06/02.
//

import Alamofire
import Foundation

struct CoinFilterService {
static let shared = CoinFilterService()

func sortCoin(sort: String,
ascending: String,
completion: @escaping (NetworkResult<Any>) -> Void) {
let header: HTTPHeaders = ["Content-Type": "application/json"]
let url = APIConstants.coinListURL + APIIndex.init(index: .sort(sort, ascending)).index.getIndex()
let dataRequest = AF.request(url,
method: .get,
encoding: JSONEncoding.default,
headers: header)

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(SortedCoin.self, from: data) else {return .pathErr}
switch statusCode {
case 200: return .success
case 400: return .requestErr
case 500: return .serverErr
default: return .networkFail
}
}
}

30 changes: 30 additions & 0 deletions Coinone-iOS/Coinone-iOS/Source/Models/SortedCoinModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// SortedCoinModel.swift
// Coinone-iOS
//
// Created by 노한솔 on 2021/06/02.
//

import Foundation

// MARK: - SortedCoin
struct SortedCoin: Codable {
let success: Bool
let data: [SortedData]
}

// MARK: - SortedData
struct SortedData: Codable {
let id: String
let coinLogoImage: String
let coinEnglishTitle, coinKoreanTitle, coinCurrentPrice, riseOrDescent: String
let degree, percentage, coinTotalPrice: String
let graphImage: String
let v: Int

enum CodingKeys: String, CodingKey {
case id = "_id"
case coinLogoImage, coinEnglishTitle, coinKoreanTitle, coinCurrentPrice, riseOrDescent, degree, percentage, coinTotalPrice, graphImage
case v = "__v"
}
}

0 comments on commit e4c354c

Please sign in to comment.