Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] #42 - 네트워크 레이어 리팩토링 및 예시코드 첨부 #94

Merged
merged 5 commits into from
Jul 16, 2022

Conversation

L-j-h-c
Copy link
Member

@L-j-h-c L-j-h-c commented Jul 16, 2022

🔥 Pull requests

⛳️ 작업한 브랜치

👷 작업한 내용

  • 네트워크 레이어 리팩토링

🚨 참고 사항

사용법 첨부할게요~~!

폴더링

image

노션 API 명세서를 보시면 Route 속성이 있습니다. 이 Route 속성을 기준으로 Services와 Routers 파일을 세팅합니다!

image

API마다 잘 확인해보시고 열거형에 추가한 다음에 Router -> Service -> VC 순으로 추가해주시면 됩니다.

Router

enum AuthRouter {
    case requestSignUp(email: String, name: String, pw: String)
    case requestSignIn(email: String, pw: String)
    case requestPostData(postId: Int)
}

extension AuthRouter: BaseRouter {
    
    var path: String {
        switch self {
        case .requestSignUp:
            return "/auth/signup"
        case .requestSignIn:
            return "/auth/signin"
        case .requestPostData(let postId):
            return "/post/\(postId)"
        }
    }
    
    var method: HTTPMethod {
        switch self {
        case .requestSignUp, .requestSignIn:
            return .post
        default:
            return .get
        }
    }
    
    var parameters: RequestParams {
        switch self {
        case .requestSignUp(let email, let name, let pw):
            let body: [String: Any] = [
                "email": email,
                "name": name,
                "password": pw
            ]
            return .requestBody(body)
        case .requestSignIn(let email, let pw):
            let body: [String: Any] = [
                "email": email,
                "password": pw
            ]
            return .requestBody(body)
        default:
            return .requestPlain
        }
    }
}
enum HomeRouter {
    case getPostDetail(postId: String)
    case changeSellStatus(postId: String, onSale: String)
    case changeLikeStatus(postId: String)
    case createPostWrite(imageCount: Int, title: String, category: String, price: Int, contents: String, isPriceSuggestion: Bool)
    case getPostList
}

extension HomeRouter: BaseRouter {
    
    var path: String {
        switch self {
        case .getPostDetail(let postId):
            return "/feed/\(postId)"
        case .changeSellStatus:
            return "/feed/on-sale"
        case .changeLikeStatus(let postId):
            return "/feed/like/\(postId)"
        case .createPostWrite:
            return "/feed"
        case .getPostList:
            return "/feed"
        }
    }
    
    var method: HTTPMethod {
        switch self {
        case .getPostDetail:
            return .get
        case .changeSellStatus:
            return .put
        case .changeLikeStatus:
            return .put
        case .createPostWrite:
            return .post
        case .getPostList:
            return .get
        }
    }
    
    var parameters: RequestParams {
        switch self {
        case .getPostDetail:
            return .requestPlain
        case .changeSellStatus(let postId, let onSale):
            let body: [String: Any] =
            ["id": postId,
             "onSale": onSale ]
            return .requestBody(body)
        case .changeLikeStatus:
            return .requestPlain
        case .createPostWrite(let imageCount, let title, let category, let price, let contents, let isPriceSuggestion):
            let body: [String: Any] =
            ["imageCount": imageCount,
             "title": title,
             "category": category,
             "price": price,
             "contents": contents,
             "isPriceSuggestion": isPriceSuggestion]
            return .requestBody(body)
            
        case .getPostList:
            return .requestPlain
        }
    }
}

Service

import Foundation

import Alamofire

class AuthService: BaseService {
    static let shared = AuthService()
    
    private override init() {}
}

extension AuthService {
    func requestSignIn(email: String, pw: String, completion: @escaping (NetworkResult<Any>) -> Void) {
        requestObject(AuthRouter.requestSignIn(email: email, pw: pw), type: SignIn.self, decodingMode: .message, completion: completion)
    }
}

뷰컨에서 호출

// MARK: Network
extension PostDetailVC {
    func getPostDetail(postId: String) {
        HomeService.shared.getPostDetail(postId: postId) { networkResult in
            switch networkResult {
            case .success(let data):
                if let data = data as? PostDetail {
                    self.postDetailModel = data
                    self.bottomView.setData(data: data)
                }
            default:
                break;
            }
        }
    }
    
    func changeSellStatus(onSale: String) {
        HomeService.shared.changeSellStatus(postId: postId ?? "", onSale: onSale) { networkResult in
            switch networkResult {
            case .success(let message):
                print(message)
            default:
                break;
            }
        }
    }
    
    func changeLikesStatus() {
        HomeService.shared.changeLikeStatus(postId: postId ?? "") { networkResult in
            switch networkResult {
            case .success(let message):
                print(message)
            default:
                break;
            }
        }
    }
}

📟 관련 이슈

@L-j-h-c L-j-h-c added Feat 새로운 기능 구현 🧑🏻‍💻Duno labels Jul 16, 2022
@L-j-h-c L-j-h-c self-assigned this Jul 16, 2022
Copy link
Contributor

@0inn 0inn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

야무지다.. 우리 리드 이주노다 .. 서버 가보자 .. 고생하셨습니다 ~!

Copy link
Member

@0lynny 0lynny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굿굿~!!

Copy link
Member

@yungu0010 yungu0010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서버 통신도 주노와 함께라면 든든해~

@L-j-h-c L-j-h-c merged commit a0cbbbf into Health-Food-Me:develop Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧑🏻‍💻Duno Feat 새로운 기능 구현
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Refactor] 네트워크 레이어 리팩토링
4 participants