커뮤니티 댓글

- 수정 / 삭제 API 적용
This commit is contained in:
Yu Sung 2023-12-20 21:31:31 +09:00
parent cb3a564a29
commit 302f69b265
7 changed files with 176 additions and 1 deletions

View File

@ -110,6 +110,7 @@ struct CreatorCommunityCommentListView: View {
isShowPopupMenuButton: true, isShowPopupMenuButton: true,
modifyComment: { commentId, comment in modifyComment: { commentId, comment in
hideKeyboard() hideKeyboard()
viewModel.modifyComment(commentId: commentId, postId: postId, comment: comment)
}, },
onClickDelete: { onClickDelete: {
commentId = $0 commentId = $0
@ -134,6 +135,7 @@ struct CreatorCommunityCommentListView: View {
desc: "삭제하시겠습니까?", desc: "삭제하시겠습니까?",
confirmButtonTitle: "삭제", confirmButtonTitle: "삭제",
confirmButtonAction: { confirmButtonAction: {
viewModel.modifyComment(commentId: commentId, postId: postId, isActive: false)
commentId = 0 commentId = 0
isShowDeletePopup = false isShowDeletePopup = false
}, },

View File

@ -119,4 +119,77 @@ final class CreatorCommunityCommentListViewModel: ObservableObject {
.store(in: &subscription) .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)
}
} }

View File

@ -115,6 +115,7 @@ struct CreatorCommunityCommentReplyView: View {
isShowPopupMenuButton: true, isShowPopupMenuButton: true,
modifyComment: { commentId, comment in modifyComment: { commentId, comment in
hideKeyboard() hideKeyboard()
viewModel.modifyComment(commentId: commentId, postId: postId, comment: comment)
}, },
onClickDelete: { onClickDelete: {
commentId = $0 commentId = $0
@ -140,6 +141,7 @@ struct CreatorCommunityCommentReplyView: View {
desc: "삭제하시겠습니까?", desc: "삭제하시겠습니까?",
confirmButtonTitle: "삭제", confirmButtonTitle: "삭제",
confirmButtonAction: { confirmButtonAction: {
viewModel.modifyComment(commentId: commentId, postId: postId, isActive: false)
commentId = 0 commentId = 0
isShowDeletePopup = false isShowDeletePopup = false
}, },

View File

@ -121,4 +121,77 @@ final class CreatorCommunityCommentReplyViewModel: ObservableObject {
.store(in: &subscription) .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)
}
} }

View File

@ -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
}

View File

@ -15,6 +15,7 @@ enum CreatorCommunityApi {
case createCommunityPostComment(comment: String, postId: Int, parentId: Int?) case createCommunityPostComment(comment: String, postId: Int, parentId: Int?)
case getCommunityPostCommentList(postId: Int, page: Int, size: Int) case getCommunityPostCommentList(postId: Int, page: Int, size: Int)
case getCommentReplyList(commentId: Int, page: Int, size: Int) case getCommentReplyList(commentId: Int, page: Int, size: Int)
case modifyComment(request: ModifyCommunityPostCommentRequest)
} }
extension CreatorCommunityApi: TargetType { extension CreatorCommunityApi: TargetType {
@ -30,7 +31,7 @@ extension CreatorCommunityApi: TargetType {
case .communityPostLike: case .communityPostLike:
return "/creator-community/like" return "/creator-community/like"
case .createCommunityPostComment: case .createCommunityPostComment, .modifyComment:
return "/creator-community/comment" return "/creator-community/comment"
case .getCommunityPostCommentList(let postId, _, _): case .getCommunityPostCommentList(let postId, _, _):
@ -48,6 +49,9 @@ extension CreatorCommunityApi: TargetType {
case .getCommunityPostList, .getCommunityPostCommentList, .getCommentReplyList: case .getCommunityPostList, .getCommunityPostCommentList, .getCommentReplyList:
return .get return .get
case .modifyComment:
return .put
} }
} }
@ -88,6 +92,9 @@ extension CreatorCommunityApi: TargetType {
"timezone": TimeZone.current.identifier "timezone": TimeZone.current.identifier
] as [String: Any] ] as [String: Any]
return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString) return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
case .modifyComment(let request):
return .requestJSONEncodable(request)
} }
} }

View File

@ -36,4 +36,8 @@ class CreatorCommunityRepository {
func getCommentReplyList(commentId: Int, page: Int, size: Int) -> AnyPublisher<Response, MoyaError> { func getCommentReplyList(commentId: Int, page: Int, size: Int) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getCommentReplyList(commentId: commentId, page: page, size: size)) return api.requestPublisher(.getCommentReplyList(commentId: commentId, page: page, size: size))
} }
func modifyComment(request: ModifyCommunityPostCommentRequest) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.modifyComment(request: request))
}
} }