diff --git a/Projects/App/Sources/Application/GPleApp.swift b/Projects/App/Sources/Application/GPleApp.swift index 29f1033..2cd5dae 100644 --- a/Projects/App/Sources/Application/GPleApp.swift +++ b/Projects/App/Sources/Application/GPleApp.swift @@ -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()) } } } diff --git a/Projects/App/Sources/Feature/MyPageFeature/Sources/MyPageView.swift b/Projects/App/Sources/Feature/MyPageFeature/Sources/MyPageView.swift index 5ea16da..24a836a 100644 --- a/Projects/App/Sources/Feature/MyPageFeature/Sources/MyPageView.swift +++ b/Projects/App/Sources/Feature/MyPageFeature/Sources/MyPageView.swift @@ -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)) @@ -242,6 +242,14 @@ struct MyPageView: View { print("반응 게시물 최신화 실패") } } + + postViewModel.myInfo { success in + if success { + print("내 정보 불러오기 성공") + } else { + print("내 정보 불러오기 실패") + } + } } .navigationBarBackButtonHidden(true) } diff --git a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift index 8a930b0..288c97a 100644 --- a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift +++ b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift @@ -8,6 +8,7 @@ public final class PostViewModel: ObservableObject { private let emojiProvider = MoyaProvider( plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))] ) + private let userProvider = MoyaProvider() private var title: String = "" private var accessToken: String = "Bearer eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiIyIiwiaWF0IjoxNzM0NjYyNTg4LCJleHAiOjE3NDQ2NjI1ODh9.FG4FVQ4oikC4HNy5h7gq0QyCIjVZtceIOKwAMnkULAt4y0lX5gGIF1s2Mdj9qr1H" private var userList: [Int] = [] @@ -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? @@ -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) { diff --git a/Projects/Domain/Sources/API/User/UserAPI.swift b/Projects/Domain/Sources/API/User/UserAPI.swift index 79240d2..22b523c 100644 --- a/Projects/Domain/Sources/API/User/UserAPI.swift +++ b/Projects/Domain/Sources/API/User/UserAPI.swift @@ -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] + } + } +} diff --git a/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift new file mode 100644 index 0000000..c7a2c27 --- /dev/null +++ b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift @@ -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 +}