From 302f69b265114e736a20ab21703df675f4d9caf4 Mon Sep 17 00:00:00 2001 From: Yu Sung Date: Wed, 20 Dec 2023 21:31:31 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BB=A4=EB=AE=A4=EB=8B=88=ED=8B=B0=20?= =?UTF-8?q?=EB=8C=93=EA=B8=80=20-=20=EC=88=98=EC=A0=95=20/=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20API=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CreatorCommunityCommentListView.swift | 2 + ...CreatorCommunityCommentListViewModel.swift | 73 +++++++++++++++++++ .../CreatorCommunityCommentReplyView.swift | 2 + ...reatorCommunityCommentReplyViewModel.swift | 73 +++++++++++++++++++ .../ModifyCommunityPostCommentRequest.swift | 14 ++++ .../CreatorCommunityApi.swift | 9 ++- .../CreatorCommunityRepository.swift | 4 + 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/ModifyCommunityPostCommentRequest.swift diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListView.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListView.swift index dfe0632..4db30df 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListView.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListView.swift @@ -110,6 +110,7 @@ struct CreatorCommunityCommentListView: View { isShowPopupMenuButton: true, modifyComment: { commentId, comment in hideKeyboard() + viewModel.modifyComment(commentId: commentId, postId: postId, comment: comment) }, onClickDelete: { commentId = $0 @@ -134,6 +135,7 @@ struct CreatorCommunityCommentListView: View { desc: "삭제하시겠습니까?", confirmButtonTitle: "삭제", confirmButtonAction: { + viewModel.modifyComment(commentId: commentId, postId: postId, isActive: false) commentId = 0 isShowDeletePopup = false }, diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListViewModel.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListViewModel.swift index 4ac1b45..c38814d 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListViewModel.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentListViewModel.swift @@ -119,4 +119,77 @@ final class CreatorCommunityCommentListViewModel: ObservableObject { .store(in: &subscription) } } + + func modifyComment( + commentId: Int, + postId: Int, + comment: String? = nil, + isActive: Bool? = nil + ) { + if comment == nil && isActive == nil { + errorMessage = "변경사항이 없습니다." + isShowPopup = true + return + } + + if let comment = comment, comment.trimmingCharacters(in: .whitespaces).isEmpty { + errorMessage = "내용을 입력하세요." + isShowPopup = true + return + } + + isLoading = true + + var request = ModifyCommunityPostCommentRequest(commentId: commentId) + + if let comment = comment { + request.comment = comment + } + + if let isActive = isActive { + request.isActive = isActive + } + + repository.modifyComment(request: request) + .sink { result in + switch result { + case .finished: + DEBUG_LOG("finish") + case .failure(let error): + ERROR_LOG(error.localizedDescription) + } + } receiveValue: { [unowned self] response in + self.isLoading = false + let responseData = response.data + + do { + let jsonDecoder = JSONDecoder() + let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData) + + if decoded.success { + self.comment = "" + self.page = 1 + self.isLast = false + + self.commentList.removeAll() + DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { + self.getCommentList() + } + } else { + if let message = decoded.message { + self.errorMessage = message + } else { + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + } + + self.isShowPopup = true + } + } catch { + self.isLoading = false + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + self.isShowPopup = true + } + } + .store(in: &subscription) + } } diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyView.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyView.swift index 4c1f463..7ea7952 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyView.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyView.swift @@ -115,6 +115,7 @@ struct CreatorCommunityCommentReplyView: View { isShowPopupMenuButton: true, modifyComment: { commentId, comment in hideKeyboard() + viewModel.modifyComment(commentId: commentId, postId: postId, comment: comment) }, onClickDelete: { commentId = $0 @@ -140,6 +141,7 @@ struct CreatorCommunityCommentReplyView: View { desc: "삭제하시겠습니까?", confirmButtonTitle: "삭제", confirmButtonAction: { + viewModel.modifyComment(commentId: commentId, postId: postId, isActive: false) commentId = 0 isShowDeletePopup = false }, diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyViewModel.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyViewModel.swift index ed87396..d4c21c6 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyViewModel.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/CreatorCommunityCommentReplyViewModel.swift @@ -121,4 +121,77 @@ final class CreatorCommunityCommentReplyViewModel: ObservableObject { .store(in: &subscription) } } + + func modifyComment( + commentId: Int, + postId: Int, + comment: String? = nil, + isActive: Bool? = nil + ) { + if comment == nil && isActive == nil { + errorMessage = "변경사항이 없습니다." + isShowPopup = true + return + } + + if let comment = comment, comment.trimmingCharacters(in: .whitespaces).isEmpty { + errorMessage = "내용을 입력하세요." + isShowPopup = true + return + } + + isLoading = true + + var request = ModifyCommunityPostCommentRequest(commentId: commentId) + + if let comment = comment { + request.comment = comment + } + + if let isActive = isActive { + request.isActive = isActive + } + + repository.modifyComment(request: request) + .sink { result in + switch result { + case .finished: + DEBUG_LOG("finish") + case .failure(let error): + ERROR_LOG(error.localizedDescription) + } + } receiveValue: { [unowned self] response in + self.isLoading = false + let responseData = response.data + + do { + let jsonDecoder = JSONDecoder() + let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData) + + if decoded.success { + self.comment = "" + self.page = 1 + self.isLast = false + + self.commentList.removeAll() + DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { + self.getCommentList() + } + } else { + if let message = decoded.message { + self.errorMessage = message + } else { + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + } + + self.isShowPopup = true + } + } catch { + self.isLoading = false + self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다." + self.isShowPopup = true + } + } + .store(in: &subscription) + } } diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/ModifyCommunityPostCommentRequest.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/ModifyCommunityPostCommentRequest.swift new file mode 100644 index 0000000..e267ef5 --- /dev/null +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/All/Comment/ModifyCommunityPostCommentRequest.swift @@ -0,0 +1,14 @@ +// +// ModifyCommunityPostCommentRequest.swift +// SodaLive +// +// Created by klaus on 2023/12/20. +// + +import Foundation + +struct ModifyCommunityPostCommentRequest: Encodable { + let commentId: Int + var comment: String? = nil + var isActive: Bool? = nil +} diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityApi.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityApi.swift index 24a1805..c81e674 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityApi.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityApi.swift @@ -15,6 +15,7 @@ enum CreatorCommunityApi { case createCommunityPostComment(comment: String, postId: Int, parentId: Int?) case getCommunityPostCommentList(postId: Int, page: Int, size: Int) case getCommentReplyList(commentId: Int, page: Int, size: Int) + case modifyComment(request: ModifyCommunityPostCommentRequest) } extension CreatorCommunityApi: TargetType { @@ -30,7 +31,7 @@ extension CreatorCommunityApi: TargetType { case .communityPostLike: return "/creator-community/like" - case .createCommunityPostComment: + case .createCommunityPostComment, .modifyComment: return "/creator-community/comment" case .getCommunityPostCommentList(let postId, _, _): @@ -48,6 +49,9 @@ extension CreatorCommunityApi: TargetType { case .getCommunityPostList, .getCommunityPostCommentList, .getCommentReplyList: return .get + + case .modifyComment: + return .put } } @@ -88,6 +92,9 @@ extension CreatorCommunityApi: TargetType { "timezone": TimeZone.current.identifier ] as [String: Any] return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) + + case .modifyComment(let request): + return .requestJSONEncodable(request) } } diff --git a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityRepository.swift b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityRepository.swift index 8353dbb..673ed4c 100644 --- a/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityRepository.swift +++ b/SodaLive/Sources/Explorer/Profile/CreatorCommunity/CreatorCommunityRepository.swift @@ -36,4 +36,8 @@ class CreatorCommunityRepository { func getCommentReplyList(commentId: Int, page: Int, size: Int) -> AnyPublisher { return api.requestPublisher(.getCommentReplyList(commentId: commentId, page: page, size: size)) } + + func modifyComment(request: ModifyCommunityPostCommentRequest) -> AnyPublisher { + return api.requestPublisher(.modifyComment(request: request)) + } }