|
| 1 | +// |
| 2 | +// AnswerCommentRepository.swift |
| 3 | +// Qapple |
| 4 | +// |
| 5 | +// Created by 문인범 on 5/20/25. |
| 6 | +// |
| 7 | + |
| 8 | +import ComposableArchitecture |
| 9 | +import QappleRepository |
| 10 | +import Foundation |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + AnswerComment API 의존성 |
| 15 | + */ |
| 16 | +struct AnswerCommentRepository { |
| 17 | + var fetchAnswerComments: (_ answerId: Int) async throws -> [AnswerComment] |
| 18 | + var createAnswerComment: (_ answerId: Int, _ content: String) async throws -> Void |
| 19 | + var likeAnswerComment: (_ answerCommentId: Int) async throws -> Void |
| 20 | + var deleteAnswerComment: (_ answerCommentId: Int) async throws -> Void |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +// MARK: - DependencyKey |
| 25 | +extension AnswerCommentRepository: DependencyKey { |
| 26 | + static let liveValue: AnswerCommentRepository = Self( |
| 27 | + fetchAnswerComments: { answerId in |
| 28 | + let response = try await RepositoryService.shared.request { server, accessToken in |
| 29 | + try await AnswerCommentAPI.fetchAnswerComments( |
| 30 | + answerId: answerId, |
| 31 | + server: server, |
| 32 | + accessToken: accessToken |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + let list = response.answerCommentInfos.map { |
| 37 | + AnswerComment( |
| 38 | + id: $0.answerCommentId, |
| 39 | + writeId: $0.writerId, |
| 40 | + // TODO: 5/20 문의 필요(writer generation, isLiked, isMine, isReport이 있는지 여부) |
| 41 | + writerGeneration: "", |
| 42 | + content: $0.content, |
| 43 | + heartCount: $0.heartCount, |
| 44 | + isLiked: false, |
| 45 | + isMine: false, |
| 46 | + isReport: false, |
| 47 | + createdAt: $0.createdAt.ISO8601ToDate(.yearMonthDateTimeMilliseconds), |
| 48 | + anonymityId: -2 |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + return list |
| 53 | + }, |
| 54 | + createAnswerComment: { answerId, content in |
| 55 | + let _ = try await RepositoryService.shared.request { server, accessToken in |
| 56 | + try await AnswerCommentAPI.createAnswerComment( |
| 57 | + answerId: answerId, |
| 58 | + content: content, |
| 59 | + server: server, |
| 60 | + accessToken: accessToken |
| 61 | + ) |
| 62 | + } |
| 63 | + }, |
| 64 | + likeAnswerComment: { answerCommentId in |
| 65 | + let _ = try await RepositoryService.shared.request { server, accessToken in |
| 66 | + try await AnswerCommentAPI.likeAnswerComment( |
| 67 | + answerCommentId: answerCommentId, |
| 68 | + server: server, |
| 69 | + accessToken: accessToken |
| 70 | + ) |
| 71 | + } |
| 72 | + }, |
| 73 | + deleteAnswerComment: { answerCommentId in |
| 74 | + let _ = try await RepositoryService.shared.request { server, accessToken in |
| 75 | + try await AnswerCommentAPI.deleteAnswerComment( |
| 76 | + answerCommentId: answerCommentId, |
| 77 | + server: server, |
| 78 | + accessToken: accessToken |
| 79 | + ) |
| 80 | + } |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + static let previewValue: AnswerCommentRepository = .init( |
| 85 | + fetchAnswerComments: { _ in |
| 86 | + sampleComments |
| 87 | + }, |
| 88 | + createAnswerComment: { answerId, content in |
| 89 | + print("\(answerId) 게시글에 \"\(content)\" 댓글을 작성했습니다.") |
| 90 | + }, |
| 91 | + likeAnswerComment: { answerCommentId in |
| 92 | + print("\(answerCommentId) 댓글에 좋아요를 눌렀습니다.") |
| 93 | + }, |
| 94 | + deleteAnswerComment: { answerCommentId in |
| 95 | + print("\(answerCommentId) 댓글을 삭제했습니다.") |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + static let testValue: AnswerCommentRepository = .init( |
| 100 | + fetchAnswerComments: { _ in |
| 101 | + sampleComments |
| 102 | + }, |
| 103 | + createAnswerComment: { _, _ in }, |
| 104 | + likeAnswerComment: { _ in }, |
| 105 | + deleteAnswerComment: { _ in } |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +// MARK: DependencyValues |
| 111 | +extension DependencyValues { |
| 112 | + var answerCommentRepository: AnswerCommentRepository { |
| 113 | + get { self[AnswerCommentRepository.self] } |
| 114 | + set { self[AnswerCommentRepository.self] = newValue } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +// MARK: Test Values |
| 120 | +extension AnswerCommentRepository { |
| 121 | + private static let sampleComments: [AnswerComment] = { |
| 122 | + var result = [AnswerComment]() |
| 123 | + for i in 1...10 { |
| 124 | + result.append( |
| 125 | + AnswerComment( |
| 126 | + id: i, |
| 127 | + writeId: i, |
| 128 | + writerGeneration: "4기", |
| 129 | + content: "테스트 댓글\(i)", |
| 130 | + heartCount: i, |
| 131 | + isLiked: i == 1, |
| 132 | + isMine: i == 2, |
| 133 | + isReport: i == 3, |
| 134 | + createdAt: .now, |
| 135 | + anonymityId: -2 |
| 136 | + ) |
| 137 | + ) |
| 138 | + } |
| 139 | + return result |
| 140 | + }() |
| 141 | +} |
0 commit comments