-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from f-lab-edu/feature/comment
댓글들을 로드하는 기능을 생성하였습니다.
- Loading branch information
Showing
17 changed files
with
779 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
HomeCafeRecipes/HomeCafeRecipes/Data/Network/CommentService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// CommentService.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/6/24. | ||
// | ||
|
||
import UIKit | ||
|
||
import RxSwift | ||
|
||
protocol CommentService { | ||
func fetchComment(recipeID: Int) -> Single<[Comment]> | ||
} | ||
|
||
final class CommentServiceImpl: CommentService { | ||
private let networkService: NetworkService | ||
|
||
init(networkService: NetworkService) { | ||
self.networkService = networkService | ||
} | ||
|
||
private func makeURL(ednpoint: String) -> URL { | ||
return APIConfig().baseURL.appendingPathComponent(ednpoint) | ||
} | ||
|
||
func fetchComment(recipeID: Int) -> Single<[Comment]> { | ||
let url = makeURL(ednpoint: "comments/\(recipeID)") | ||
|
||
return networkService.getRequest( | ||
url: url, | ||
responseType: NetworkResponseDTO<[CommentDTO]>.self | ||
).map { response in | ||
response.data.map { $0.toDomain() } | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/CommentDTO.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// CommentDTO.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/6/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CommentDTO: Decodable { | ||
|
||
let commentID: Int | ||
let comment: String | ||
let commentLikeCount: Int | ||
let isLiked: Bool | ||
let createdAt: String | ||
let writer: UserDTO | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case commentID = "commentId" | ||
case comment = "content" | ||
case commentLikeCount = "commentLikesCnt" | ||
case isLiked = "isLiked" | ||
case createdAt = "createdAt" | ||
case writer = "writer" | ||
} | ||
} | ||
|
||
extension CommentDTO { | ||
func toDomain() -> Comment{ | ||
return Comment ( | ||
commentID: commentID, | ||
comment: comment, | ||
commentLikeCount: commentLikeCount, | ||
isLiked: isLiked, | ||
createAt: DateFormatter.iso8601.date(from: createdAt) ?? Date(), | ||
writer: writer.toDomain() | ||
) | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
HomeCafeRecipes/HomeCafeRecipes/Data/Repositories/CommentListRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// CommentListRepository.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/6/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import RxSwift | ||
|
||
protocol CommentListRepository { | ||
func fetchComments(recipeID: Int) -> Single<[Comment]> | ||
} | ||
|
||
final class CommentListRepositoryImpl: CommentListRepository { | ||
private let commnetServie: CommentService | ||
|
||
init(commnetServie: CommentService) { | ||
self.commnetServie = commnetServie | ||
} | ||
|
||
func fetchComments(recipeID: Int) -> Single<[Comment]> { | ||
return commnetServie.fetchComment(recipeID: recipeID) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
HomeCafeRecipes/HomeCafeRecipes/Domain/Entities/Comment.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Comment.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/5/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Comment { | ||
let commentID: Int | ||
let comment: String | ||
let commentLikeCount: Int | ||
let isLiked: Bool | ||
let createAt: Date | ||
let writer: User | ||
} |
52 changes: 52 additions & 0 deletions
52
HomeCafeRecipes/HomeCafeRecipes/Domain/Interactor/CommentInteractor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// CommentInteractor.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/6/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import RxSwift | ||
|
||
protocol CommentInteractorDelegate: AnyObject { | ||
func fetchedComments(result: Result<[Comment], Error>) | ||
} | ||
|
||
protocol CommentInteractor { | ||
func loadComment(recipeID: Int) | ||
} | ||
|
||
final class CommentInteractorImpl: CommentInteractor { | ||
private let disposeBag = DisposeBag() | ||
private let usecase: FetchCommentUsecase | ||
private var allComments: [Comment] = [] | ||
weak var delegate: CommentInteractorDelegate? | ||
|
||
init(usecase: FetchCommentUsecase) { | ||
self.usecase = usecase | ||
} | ||
|
||
func loadComment(recipeID: Int) { | ||
usecase.execute(recipeID: recipeID) | ||
.subscribe(onSuccess: { [weak self] comments in | ||
self?.handleResult(.success(comments)) | ||
}, onError: { [weak self] error in | ||
self?.handleResult(.failure(error)) | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
private func handleResult(_ result: Result<[Comment], Error>) { | ||
switch result { | ||
case .success(let comments): | ||
if comments.isEmpty { | ||
return | ||
} | ||
allComments = comments | ||
delegate?.fetchedComments(result: .success(allComments)) | ||
case .failure(let error): | ||
delegate?.fetchedComments(result: .failure(error)) | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
HomeCafeRecipes/HomeCafeRecipes/Domain/UseCases/FetchCommentUsecase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// FetchCommentUsecase.swift | ||
// HomeCafeRecipes | ||
// | ||
// Created by 김건호 on 10/6/24. | ||
// | ||
|
||
import RxSwift | ||
|
||
protocol FetchCommentUsecase{ | ||
func execute(recipeID: Int) -> Single<[Comment]> | ||
} | ||
|
||
|
||
final class FetchCommentUsecaseImpl: FetchCommentUsecase{ | ||
private let repository: CommentListRepository | ||
|
||
init(repository: CommentListRepository) { | ||
self.repository = repository | ||
} | ||
|
||
func execute(recipeID: Int) -> Single<[Comment]> { | ||
return repository.fetchComments(recipeID: recipeID) | ||
} | ||
} |
Oops, something went wrong.