Skip to content

Commit

Permalink
Merge pull request #31 from GPle-Team/30-MyPage-MyInfo-Fetch
Browse files Browse the repository at this point in the history
🔀 :: [#30] 마이페이지 정보 불러오기 구현
  • Loading branch information
Xixn2 authored Dec 27, 2024
2 parents f52be0e + 91b1865 commit 3ace344
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Projects/App/Sources/Application/GPleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import SwiftUI
struct GPleApp: App {
var body: some Scene {
WindowGroup {
RankView(postViewModel: PostViewModel())
//MyPageView(viewModel: MyPageViewModel(),postViewModel: PostViewModel())
//RankView(postViewModel: PostViewModel())
MyPageView(viewModel: MyPageViewModel(),postViewModel: PostViewModel())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct MyPageView: View {

HStack(spacing: 0) {
VStack(alignment: .leading, spacing: 4) {
Text("\(viewModel.name)님,")
Text("\(postViewModel.myInfo?.name ?? "")님,")
.foregroundStyle(.white)
.font(GPleFontFamily.Pretendard.regular.swiftUIFont(size: 20))

Expand Down Expand Up @@ -242,6 +242,14 @@ struct MyPageView: View {
print("반응 게시물 최신화 실패")
}
}

postViewModel.myInfo { success in
if success {
print("내 정보 불러오기 성공")
} else {
print("내 정보 불러오기 실패")
}
}
}
.navigationBarBackButtonHidden(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class PostViewModel: ObservableObject {
private let emojiProvider = MoyaProvider<EmojiAPI>(
plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))]
)
private let userProvider = MoyaProvider<UserAPI>()
private var title: String = ""
private var accessToken: String = "Bearer eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiIyIiwiaWF0IjoxNzM0NjYyNTg4LCJleHAiOjE3NDQ2NjI1ODh9.FG4FVQ4oikC4HNy5h7gq0QyCIjVZtceIOKwAMnkULAt4y0lX5gGIF1s2Mdj9qr1H"
private var userList: [Int] = []
Expand All @@ -24,6 +25,7 @@ public final class PostViewModel: ObservableObject {
@Published var myPostList: [MyPostListResponse] = []
@Published var myReactionPostList: [MyReactionPostListResponse] = []
@Published var popularityPostList: [PopularityResponse] = []
@Published public var myInfo: MyInfoResponse?
private var imageUploadResponse: ImageUploadResponse?


Expand Down Expand Up @@ -201,6 +203,27 @@ public final class PostViewModel: ObservableObject {
}
}

public func myInfo(completion: @escaping (Bool) -> Void) {
userProvider.request(.userInfoInput(authorization: accessToken)) { result in
switch result {
case let .success(response):
do {
print("성공: 내 정보 불러오기")

self.myInfo = try JSONDecoder().decode(MyInfoResponse.self, from: response.data)

completion(true)
} catch {
print("Failed to decode JSON response: \(error)")
completion(false)
}
case let .failure(err):
print("Network request failed: \(err)")
completion(false)
}
}
}



public func uploadImages(completion: @escaping (Bool) -> Void) {
Expand Down
102 changes: 44 additions & 58 deletions Projects/Domain/Sources/API/User/UserAPI.swift
Original file line number Diff line number Diff line change
@@ -1,58 +1,44 @@
//import Foundation
//import Moya
//
//public enum UserAPI {
// case userInfoInput(authorization: String, name: String, number: String, file: Data)
//}
//
//extension UserAPI: TargetType {
// public var baseURL: URL {
// return URL(string: "https://port-0-gple-backend-eg4e2alkoplc4q.sel4.cloudtype.app")!
// }
//
// public var path: String {
// switch self {
// case .userInfoInput:
// return "/profile"
// }
// }
//
// public var method: Moya.Method {
// switch self {
// case .userInfoInput:
// return .post
// }
// }
//
// public var sampleData: Data {
// return "{}".data(using: .utf8)!
// }
//
// public var task: Task {
// switch self {
// case .userInfoInput(let authorization, let name, let number, let file):
// // Form data (name, number) and file upload using multipart
// return .requestCompositeParameters(
// bodyParameters: [
// "name": name,
// "number": number
// ],
// bodyEncoding: JSONEncoding.default, // Form data with JSON encoding
// urlParameters: [:], // No URL parameters
// multipartBody: [
// MultipartFormData(provider: .data(file), name: "file", fileName: "profile_image.jpg", mimeType: "image/jpeg")
// ]
// )
// }
// }
//
// public var headers: [String : String]? {
// switch self {
// case .userInfoInput(let authorization, _, _, _):
// return [
// "Authorization": "Bearer \(authorization)", // Authorization token
// "Content-Type": "multipart/form-data" // Set content type for multipart requests
// ]
// }
// }
//}
import Foundation
import Moya

public enum UserAPI {
case userInfoInput(authorization: String)
}

extension UserAPI: TargetType {
public var baseURL: URL {
return URL(string: "https://port-0-gple-backend-eg4e2alkoplc4q.sel4.cloudtype.app")!
}

public var path: String {
switch self {
case .userInfoInput:
return "/user/profile"
}
}

public var method: Moya.Method {
switch self {
case .userInfoInput:
return .get
}
}

public var sampleData: Data {
return "{}".data(using: .utf8)!
}

public var task: Task {
switch self {
case .userInfoInput:
return .requestPlain
}
}

public var headers: [String : String]? {
switch self {
case .userInfoInput(let authorization):
return ["Authorization": authorization]
}
}
}
8 changes: 8 additions & 0 deletions Projects/Domain/Sources/Response/Post/MyInfoResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

public struct MyInfoResponse: Identifiable, Codable {
public let id: Int
public let grade: Int
public let name: String
public let profileImage: String
}

0 comments on commit 3ace344

Please sign in to comment.