Skip to content

Commit

Permalink
[Network/#95] 학과 검색 API 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
Nya128 committed Jan 22, 2025
1 parent ff3d948 commit d72786b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// GetSchoolSearchResponseDTO.swift
// GetSearchResponseDTO.swift
// Gongbaek_iOS
//
// Created by 김나연 on 1/23/25.
Expand All @@ -10,3 +10,7 @@ import Foundation
struct GetSchoolSearchResponseDTO: Codable {
let schoolNames: [String]
}

struct GetMajorSearchResponseDTO: Codable {
let schoolMajors: [String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ import Moya

enum SignupTargetType {
case postNicknameValidation(nickname: String)
case getSchoolSearchResults(schoolName: String)
case getMajorSearchResults(schoolName: String, majorName: String)
}

extension SignupTargetType: BaseTargetType {
var headers: Parameters? {
switch self {
case .postNicknameValidation:
return APIConstants.hasContentTypeHeader
case .getSchoolSearchResults:
return APIConstants.hasContentTypeHeader
case .getMajorSearchResults:
return APIConstants.hasContentTypeHeader
}
}

var path: String {
switch self {
case .postNicknameValidation:
return "/api/v1/user/validate/nickname"
case .getSchoolSearchResults:
return "/api/v1/school/search"
case .getMajorSearchResults:
return "/api/v1/school/major/search"
}
}

var method: Moya.Method {
switch self {
case .postNicknameValidation:
return .post
case .getSchoolSearchResults:
return .get
case .getMajorSearchResults:
return .get
}
}

Expand All @@ -40,6 +54,19 @@ extension SignupTargetType: BaseTargetType {
parameters: ["nickname" : nickname],
encoding: URLEncoding.queryString
)
case .getSchoolSearchResults(let schoolName):
return .requestParameters(
parameters: ["schoolName" : schoolName],
encoding: URLEncoding.queryString
)
case .getMajorSearchResults(let schoolName, let majorName):
return .requestParameters(
parameters: [
"schoolName" : schoolName,
"majorName" : majorName,
],
encoding: URLEncoding.queryString
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ struct SchoolMajorSearchView: View {
}

private func searchResultListView() -> some View {
List(viewModel.searchResultList, id: \.self) { item in
let results = Array(Set(viewModel.searchResultList)).sorted()

return List(results, id: \.self) { item in
let isSelected = viewModel.selectedSearchResult == item

SearchListCell(
Expand Down Expand Up @@ -121,10 +123,12 @@ extension SchoolMajorSearchView {
switch state {
case .school:
viewModel.getSchoolSearchResults() { response in

// TODO: 에러대응
}
case .major:
viewModel.searchResultList = MajorNameListModel.mockData().schoolMajors
viewModel.getMajorSearchResults() { response in
// TODO: 에러대응
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,21 @@ extension SignupViewModel {
}
}
}

/// 학과 검색 API
func getMajorSearchResults(completion: @escaping (Bool) -> ()) {
Providers.SignupProvider.request(
target: .getMajorSearchResults(
schoolName: schoolName,
majorName: searchWord
),
instance: BaseResponse<GetMajorSearchResponseDTO>.self
) { response in
print(response)
if response.success {
guard let data = response.data else { return }
self.searchResultList = data.schoolMajors
}
}
}
}

0 comments on commit d72786b

Please sign in to comment.