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

✨ [FEAT] 커뮤니티 메인 API 연결 #461

Merged
merged 16 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
5e54d54
✅ Chore: 게시글 전체 리스트 조회 Response Model 생성 (#445)
hwangJi-dev Sep 8, 2022
82b173c
✨ Feat: PostList 조회 API, Service 구현 (#445)
hwangJi-dev Sep 10, 2022
c598d09
✅ Chore: PostListResModel 수정 및 API 코드 수정 (#445)
hwangJi-dev Sep 12, 2022
0087058
✅ Chore: API와의 연관성을 위해 기존 CommunityType을 제거하고 PostFilterType으로 변경 (#445)
hwangJi-dev Sep 12, 2022
01351fc
✅ Chore: 기존 CommunityPostList Model을 PostListResModel로 변경 (#445)
hwangJi-dev Sep 12, 2022
a57fef0
✨ Feat: CommunityMainReactor - 커뮤니티 메인 리스트 API 요청 기능 구현 (#445)
hwangJi-dev Sep 12, 2022
504dddd
✨ Feat: 커뮤니티 메인 리스트 API 요청 기능과 뷰 바인딩 (#445)
hwangJi-dev Sep 12, 2022
7e5e837
✅ Chore: 게시글 전체 리스트 조회 Response Model 생성 (#445)
hwangJi-dev Sep 8, 2022
dbb8fd7
✨ Feat: PostList 조회 API, Service 구현 (#445)
hwangJi-dev Sep 10, 2022
3b988df
✅ Chore: PostListResModel 수정 및 API 코드 수정 (#445)
hwangJi-dev Sep 12, 2022
9ebf96f
✅ Chore: API와의 연관성을 위해 기존 CommunityType을 제거하고 PostFilterType으로 변경 (#445)
hwangJi-dev Sep 12, 2022
2ed47d0
✅ Chore: 기존 CommunityPostList Model을 PostListResModel로 변경 (#445)
hwangJi-dev Sep 12, 2022
d40e733
✨ Feat: CommunityMainReactor - 커뮤니티 메인 리스트 API 요청 기능 구현 (#445)
hwangJi-dev Sep 12, 2022
4aa07cc
✨ Feat: 커뮤니티 메인 리스트 API 요청 기능과 뷰 바인딩 (#445)
hwangJi-dev Sep 12, 2022
5adf0e3
✅ Chore: 게시글 리스트 데이터 Model 변경에 따른 코드 수정 (#445)
hwangJi-dev Sep 12, 2022
0e510c5
Merge remote-tracking branch 'origin/feature/#445-communityMain-conne…
hwangJi-dev Sep 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions NadoSunbae-iOS/NadoSunbae-iOS/Global/Struct/CommunityType.swift

This file was deleted.

33 changes: 33 additions & 0 deletions NadoSunbae-iOS/NadoSunbae-iOS/Global/Struct/PostFilterType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// PostFilterType.swift
// NadoSunbae
//
// Created by hwangJi on 2022/09/08.
//

import Foundation

enum PostFilterType: CaseIterable {
case community
case general
case questionToEveryone // 전체에게 질문 (커뮤니티 질문)
case information
case questionToPerson // 1:1 질문
}

extension PostFilterType {
var name: String {
switch self {
case .community:
return "전체"
case .general:
return "자유"
case .questionToEveryone:
return "질문"
case .information:
return "정보"
case .questionToPerson:
return "1:1 질문"
}
}
}
87 changes: 24 additions & 63 deletions NadoSunbae-iOS/NadoSunbae-iOS/Network/APIManagers/PublicAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Moya

class PublicAPI: BaseAPI {
static let shared = PublicAPI()
var publicProvider = MoyaProvider<PublicService>()
var publicProvider = MoyaProvider<PublicService>(plugins: [NetworkLoggerPlugin()])

private override init() {}

Expand All @@ -23,6 +23,7 @@ class PublicAPI: BaseAPI {
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data, [MajorListData].self)

completion(networkResult)

case .failure(let err):
Expand All @@ -39,8 +40,9 @@ class PublicAPI: BaseAPI {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data, RequestBlockUnblockUserModel.self)

completion(self.requestBlockUnBlockUserJudgeData(status: statusCode, data: data))
completion(networkResult)

case .failure(let err):
print(err)
Expand All @@ -56,8 +58,9 @@ class PublicAPI: BaseAPI {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data, String.self)

completion(self.requestReportJudgeData(status: statusCode, data: data))
completion(networkResult)

case .failure(let err):
print(err)
Expand All @@ -73,73 +76,31 @@ class PublicAPI: BaseAPI {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data, AppLinkResponseModel.self)

completion(self.getAppLinkJudgeData(status: statusCode, data: data))
completion(networkResult)

case .failure(let err):
print(err)
}
}
}
}

// MARK: - JudgeData
extension PublicAPI {

/// requestBlockUnBlockUser
private func requestBlockUnBlockUserJudgeData(status: Int, data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<RequestBlockUnblockUserModel>.self, from: data) else { return .pathErr }

switch status {
case 200...204:
return .success(decodedData.data ?? "None-Data")
case 401:
return .requestErr(false)
case 400, 402...409:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}

/// requestReport
private func requestReportJudgeData(status: Int, data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<String>.self, from: data) else { return .pathErr }

switch status {
case 200...204:
return .success(decodedData.data ?? "None-Data")
case 401:
return .requestErr(false)
case 409:
return .requestErr(409)
case 400, 402...408:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}

/// getAppLink
private func getAppLinkJudgeData(status: Int, data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<AppLinkResponseModel>.self, from: data) else { return .pathErr }

switch status {
case 200...204:
return .success(decodedData.data ?? "None-Data")
case 400...409:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
/// [GET] 전체 게시글 리스트 조회 - 커뮤니티, 1:1 질문 전체 글 리스트
func getPostList(univID: Int, majorID: Int, filter: PostFilterType, sort: String, search: String, completion: @escaping (NetworkResult<Any>) -> (Void)) {
publicProvider.request(.getPostList(univID: univID, majorID: majorID, filter: filter, sort: sort, search: search)) { result in
switch result {

case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data, [PostListResModel].self)

completion(networkResult)

case .failure(let err):
print(err)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// PostListResModel.swift
// NadoSunbae
//
// Created by hwangJi on 2022/09/08.
//

import Foundation

/// 커뮤니티 전체, 자유, 질문, 정보, 1:1 질문 전체 Post List 조회 Response Data Model
struct PostListResModel: Codable {
let postID: Int
let type, title, content, createdAt: String
let majorName: String
let writer: CommunityWriter
let commentCount: Int
let like: Like

enum CodingKeys: String, CodingKey {
case postID = "postId"
case type, title, content, createdAt, majorName, writer, commentCount, like
}
}

struct CommunityWriter: Codable {
let writerID: Int
let nickname: String

enum CodingKeys: String, CodingKey {
case writerID = "id"
case nickname
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum PublicService {
case requestBlockUnBlockUser(blockUserID: Int)
case requestReport(reportedTargetID: Int, reportedTargetTypeID: Int, reason: String)
case getAppLink
case getPostList(univID: Int, majorID: Int, filter: PostFilterType, sort: String, search: String)
}

extension PublicService: TargetType {
Expand All @@ -31,12 +32,14 @@ extension PublicService: TargetType {
return "/report"
case .getAppLink:
return "/app/link"
case .getPostList(let univID, _, _, _, _):
return "/post/university/\(univID)"
}
}

var method: Moya.Method {
switch self {
case .getMajorList, .getAppLink:
case .getMajorList, .getAppLink, .getPostList:
return .get
case .requestBlockUnBlockUser, .requestReport:
return .post
Expand All @@ -59,12 +62,20 @@ extension PublicService: TargetType {
return .requestParameters(parameters: body, encoding: JSONEncoding.prettyPrinted)
case .getAppLink:
return .requestPlain
case .getPostList(_, let majorID, let filter, let sort, let search):
let query: [String: Any] = [
"majorId": majorID,
"filter": filter,
"sort": sort,
"search": search
]
return .requestParameters(parameters: query, encoding: URLEncoding.queryString)
}
}

var headers: [String: String]? {
switch self {
case .requestBlockUnBlockUser, .requestReport:
case .requestBlockUnBlockUser, .requestReport, .getPostList:
let accessToken = UserToken.shared.accessToken ?? ""
return ["accessToken": accessToken]
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extension CommunityTVC {

// MARK: - Custom Methods
extension CommunityTVC {
func setCommunityData(data: CommunityPostList) {
categoryLabel.text = data.category
func setCommunityData(data: PostListResModel) {
categoryLabel.text = data.type
questionTitleLabel.text = data.title
questionContentLabel.text = data.content
nicknameLabel.text = data.writer.nickname
Expand Down
Loading