커뮤니티 게시물 삭제 API 적용

This commit is contained in:
Yu Sung 2023-12-21 00:38:30 +09:00
parent 9f360e9fcd
commit f40642f90f
5 changed files with 99 additions and 2 deletions

View File

@ -73,6 +73,9 @@ struct CreatorCommunityAllView: View {
modifyAction: {
},
deleteAction: {
if creatorId == UserDefaults.int(forKey: .userId) {
viewModel.isShowDeleteConfirm = true
}
},
reportAction: {
viewModel.isShowReportView = true
@ -95,6 +98,19 @@ struct CreatorCommunityAllView: View {
}
if viewModel.isShowDeleteConfirm {
SodaDialog(
title: "게시물 삭제",
desc: "삭제하시겠습니까?",
confirmButtonTitle: "삭제",
confirmButtonAction: {
viewModel.deleteCommunityPost()
viewModel.isShowDeleteConfirm = false
},
cancelButtonTitle: "취소",
cancelButtonAction: {
viewModel.isShowDeleteConfirm = false
}
)
}
}
}

View File

@ -198,4 +198,62 @@ class CreatorCommunityAllViewModel: ObservableObject {
}
.store(in: &subscription)
}
func deleteCommunityPost() {
isLoading = true
let request = ModifyCommunityPostRequest(creatorCommunityId: postId, isActive: false)
var multipartData = [MultipartFormData]()
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
let jsonData = try? encoder.encode(request)
if let jsonData = jsonData {
multipartData.append(MultipartFormData(provider: .data(jsonData), name: "request"))
repository.modifyCommunityPost(parameters: multipartData)
.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.errorMessage = "삭제되었습니다"
self.isShowPopup = true
self.page = 1
self.isLast = false
self.getCommunityPostList()
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
}
.store(in: &subscription)
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
self.isLoading = false
}
}
}

View File

@ -10,6 +10,7 @@ import Moya
enum CreatorCommunityApi {
case createCommunityPost(parameters: [MultipartFormData])
case modifyCommunityPost(parameters: [MultipartFormData])
case getCommunityPostList(creatorId: Int, page: Int, size: Int)
case communityPostLike(postId: Int)
case createCommunityPostComment(comment: String, postId: Int, parentId: Int?)
@ -25,7 +26,7 @@ extension CreatorCommunityApi: TargetType {
var path: String {
switch self {
case .createCommunityPost, .getCommunityPostList:
case .createCommunityPost, .getCommunityPostList, .modifyCommunityPost:
return "/creator-community"
case .communityPostLike:
@ -50,7 +51,7 @@ extension CreatorCommunityApi: TargetType {
case .getCommunityPostList, .getCommunityPostCommentList, .getCommentReplyList:
return .get
case .modifyComment:
case .modifyComment, .modifyCommunityPost:
return .put
}
}
@ -60,6 +61,9 @@ extension CreatorCommunityApi: TargetType {
case .createCommunityPost(let parameters):
return .uploadMultipart(parameters)
case .modifyCommunityPost(let parameters):
return .uploadMultipart(parameters)
case .getCommunityPostList(let creatorId, let page, let size):
let parameters = [
"creatorId": creatorId,
@ -95,6 +99,7 @@ extension CreatorCommunityApi: TargetType {
case .modifyComment(let request):
return .requestJSONEncodable(request)
}
}

View File

@ -17,6 +17,10 @@ class CreatorCommunityRepository {
return api.requestPublisher(.createCommunityPost(parameters: parameters))
}
func modifyCommunityPost(parameters: [MultipartFormData]) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.modifyCommunityPost(parameters: parameters))
}
func getCommunityPostList(creatorId: Int, page: Int, size: Int) -> AnyPublisher<Response, MoyaError> {
return api.requestPublisher(.getCommunityPostList(creatorId: creatorId, page: page, size: size))
}

View File

@ -0,0 +1,14 @@
//
// ModifyCommunityPostRequest.swift
// SodaLive
//
// Created by klaus on 2023/12/21.
//
struct ModifyCommunityPostRequest: Encodable {
let creatorCommunityId: Int
var content: String? = nil
var isCommentAvailable: Bool? = nil
var isAdult: Bool? = nil
var isActive: Bool? = nil
}