From 0048aaf4c020f477279baaf27b756241ff42e874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxixn2=E2=80=9D?= Date: Fri, 27 Dec 2024 14:52:59 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20::=20[#30]=20MyInfoResponse=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/PostViewModel.swift | 26 +++++++++++++++++++ .../Response/Post/MyInfoResponse.swift | 8 ++++++ 2 files changed, 34 insertions(+) create mode 100644 Projects/Domain/Sources/Response/Post/MyInfoResponse.swift diff --git a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift index 8a930b0..cc2317d 100644 --- a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift +++ b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift @@ -201,6 +201,32 @@ public final class PostViewModel: ObservableObject { } } + public func myInfo(completion: @escaping (Bool) -> Void) { + authProvider.request(.popularityUserList(authorization: accessToken)) { result in + switch result { + case let .success(response): + do { + print("성공: 유저 리스트 불러오기") + + self.popularityUserList = try JSONDecoder().decode([PopularityRankingUserListResponse].self, from: response.data) + + print("불러온 유저 리스트:") + for (index, user) in self.popularityUserList.enumerated() { + print("[\(index)] \(user)") + } + + 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/Response/Post/MyInfoResponse.swift b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift new file mode 100644 index 0000000..3e80b3c --- /dev/null +++ b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift @@ -0,0 +1,8 @@ +import Foundation + +public struct MyInfoResponsed: Identifiable, Codable { + public let id: Int + public let name: String + public let grade: Int + public let profileImage: String +} From 5ef614e5175450c6a28dd3163eb602f2c934505c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxixn2=E2=80=9D?= Date: Fri, 27 Dec 2024 14:55:24 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20::=20[#30]=20User=20Profile=20I?= =?UTF-8?q?nfo=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Domain/Sources/API/User/UserAPI.swift | 102 ++++++++---------- 1 file changed, 44 insertions(+), 58 deletions(-) diff --git a/Projects/Domain/Sources/API/User/UserAPI.swift b/Projects/Domain/Sources/API/User/UserAPI.swift index 79240d2..26ce312 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] + } + } +} From 76c78053981ad8e92bbf95fed1ddf3e2a14f09dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxixn2=E2=80=9D?= Date: Fri, 27 Dec 2024 14:58:26 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20::=20[#30]=20My=20Info=20ViewMo?= =?UTF-8?q?del=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostCreateFeature/Sources/PostViewModel.swift | 11 ++++------- .../Domain/Sources/Response/Post/MyInfoResponse.swift | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift index cc2317d..34eb7cd 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? @@ -202,18 +204,13 @@ public final class PostViewModel: ObservableObject { } public func myInfo(completion: @escaping (Bool) -> Void) { - authProvider.request(.popularityUserList(authorization: accessToken)) { result in + userProvider.request(.userInfoInput(authorization: accessToken)) { result in switch result { case let .success(response): do { print("성공: 유저 리스트 불러오기") - self.popularityUserList = try JSONDecoder().decode([PopularityRankingUserListResponse].self, from: response.data) - - print("불러온 유저 리스트:") - for (index, user) in self.popularityUserList.enumerated() { - print("[\(index)] \(user)") - } + self.myInfo = try JSONDecoder().decode(MyInfoResponse.self, from: response.data) completion(true) } catch { diff --git a/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift index 3e80b3c..9ae9626 100644 --- a/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift +++ b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift @@ -1,6 +1,6 @@ import Foundation -public struct MyInfoResponsed: Identifiable, Codable { +public struct MyInfoResponse: Identifiable, Codable { public let id: Int public let name: String public let grade: Int From 91b1865eeba0a2ed687bd92713f18c32dc25b18d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxixn2=E2=80=9D?= Date: Fri, 27 Dec 2024 15:05:53 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=A8=20::=20[#30]=20MyPage=20Info=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Projects/App/Sources/Application/GPleApp.swift | 4 ++-- .../Feature/MyPageFeature/Sources/MyPageView.swift | 10 +++++++++- .../PostCreateFeature/Sources/PostViewModel.swift | 2 +- Projects/Domain/Sources/API/User/UserAPI.swift | 2 +- .../Domain/Sources/Response/Post/MyInfoResponse.swift | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) 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 34eb7cd..288c97a 100644 --- a/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift +++ b/Projects/App/Sources/Feature/PostCreateFeature/Sources/PostViewModel.swift @@ -208,7 +208,7 @@ public final class PostViewModel: ObservableObject { switch result { case let .success(response): do { - print("성공: 유저 리스트 불러오기") + print("성공: 내 정보 불러오기") self.myInfo = try JSONDecoder().decode(MyInfoResponse.self, from: response.data) diff --git a/Projects/Domain/Sources/API/User/UserAPI.swift b/Projects/Domain/Sources/API/User/UserAPI.swift index 26ce312..22b523c 100644 --- a/Projects/Domain/Sources/API/User/UserAPI.swift +++ b/Projects/Domain/Sources/API/User/UserAPI.swift @@ -13,7 +13,7 @@ extension UserAPI: TargetType { public var path: String { switch self { case .userInfoInput: - return "/User/profile" + return "/user/profile" } } diff --git a/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift index 9ae9626..c7a2c27 100644 --- a/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift +++ b/Projects/Domain/Sources/Response/Post/MyInfoResponse.swift @@ -2,7 +2,7 @@ import Foundation public struct MyInfoResponse: Identifiable, Codable { public let id: Int - public let name: String public let grade: Int + public let name: String public let profileImage: String }