-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
147 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// JoinModel.swift | ||
// Spon-us | ||
// | ||
// Created by 박현수 on 2/9/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct JoinContent: Codable { | ||
let id: Int | ||
let email, name: String | ||
} | ||
|
||
struct JoinModel: Codable { | ||
let statusCode, message: String | ||
let content: JoinContent | ||
} |
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,38 @@ | ||
// | ||
// JoinViewModel.swift | ||
// Spon-us | ||
// | ||
// Created by 박현수 on 2/9/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Moya | ||
|
||
class JoinViewModel: ObservableObject { | ||
@Published var join: JoinModel? | ||
@Published var isButtonEnabled = false | ||
private let provider = MoyaProvider<SponusAPI>(plugins: [NetworkLoggerPlugin()]) | ||
|
||
func postJoin(name: String, email: String, password: String, orgType: OrgType, subOrgType: SubOrgType?) { | ||
provider.request(.postJoin(name: name, email: email, password: password, orgType: orgType, subOrgType: subOrgType)) { result in | ||
switch result { | ||
case let .success(response): | ||
do { | ||
if response.statusCode == 200 { | ||
self.isButtonEnabled = true | ||
} | ||
print(self.isButtonEnabled) | ||
print(try response.mapJSON()) | ||
let joinResponse = try response.map(JoinModel.self) | ||
self.join = joinResponse | ||
} catch { | ||
print("Error parsing response: \(error)") | ||
} | ||
|
||
case let .failure(error): | ||
print("Network request failed: \(error)") | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,8 +9,25 @@ import Foundation | |
import Moya | ||
import KeychainSwift | ||
|
||
enum OrgType { | ||
case student, company | ||
} | ||
|
||
enum SubOrgType { | ||
case studentCouncil, studentClub | ||
} | ||
|
||
struct JoinRequestBody: Codable { | ||
let name: String | ||
let email: String | ||
let password: String | ||
let organizationType: String | ||
let suborganizationType: String? | ||
} | ||
|
||
enum SponusAPI { | ||
case postEmail(email: String) | ||
case postJoin(name: String, email: String, password: String, orgType: OrgType, subOrgType: SubOrgType?) | ||
} | ||
|
||
extension SponusAPI: TargetType { | ||
|
@@ -20,22 +37,37 @@ extension SponusAPI: TargetType { | |
|
||
var path: String { | ||
switch self { | ||
case .postEmail(let email): | ||
case .postEmail: | ||
return "/api/v1/organizations/email" | ||
case .postJoin: | ||
return "/api/v1/organizations/join" | ||
} | ||
} | ||
|
||
var method: Moya.Method { | ||
switch self { | ||
case .postEmail: | ||
return .post | ||
case .postJoin: | ||
return .post | ||
} | ||
} | ||
|
||
var sampleData: Data { | ||
switch self { | ||
case .postEmail: | ||
return Data() | ||
case .postJoin: | ||
let response: [String : Any] = [ | ||
"statusCode": "OK", | ||
"message": "OK", | ||
"content": [ | ||
"id": 0, | ||
"email": "[email protected]", | ||
"name": "test" | ||
] | ||
] | ||
return try! JSONSerialization.data(withJSONObject: response, options: .prettyPrinted) | ||
} | ||
} | ||
|
||
|
@@ -44,13 +76,30 @@ extension SponusAPI: TargetType { | |
case .postEmail(let email): | ||
let parameters: [String: Any] = ["email": email] | ||
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) | ||
case .postJoin(let name, let email, let password, let orgType, let subOrgType): | ||
switch orgType { | ||
case .student: | ||
switch subOrgType! { | ||
case .studentCouncil: | ||
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: "STUDENT_COUNCIL") | ||
return .requestJSONEncodable(requestBody) | ||
case .studentClub: | ||
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: "STUDENT_CLUB") | ||
return .requestJSONEncodable(requestBody) | ||
} | ||
case .company: | ||
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: nil) | ||
return .requestJSONEncodable(requestBody) | ||
} | ||
} | ||
} | ||
|
||
var headers: [String: String]? { | ||
switch self { | ||
case .postEmail: | ||
return nil | ||
case .postJoin: | ||
return nil | ||
/* | ||
case .postLike: | ||
return ["Content-Type": "application/json", | ||
|
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