Skip to content

Commit

Permalink
feat: deleteBookCoverUseCase 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyxxn committed Dec 1, 2024
1 parent 8f60d0c commit 17b0ab2
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public final class HomeViewModel: ViewModelType {
case selectedCategory(category: String)
case dragAndDropBookCover(currentIndex: Int, destinationIndex: Int)
case likeButtonTapped(bookId: UUID)
case deleteBookCover(bookId: UUID)
}

public enum Output: Equatable {
Expand All @@ -23,6 +24,7 @@ public final class HomeViewModel: ViewModelType {
private let fetchMemorialHouseNameUseCase: FetchMemorialHouseNameUseCase
private let fetchAllBookCoverUseCase: FetchAllBookCoverUseCase
private let updateBookCoverUseCase: UpdateBookCoverUseCase
private let deleteBookCoverUseCase: DeleteBookCoverUseCase
private var cancellables = Set<AnyCancellable>()
private(set) var houseName = ""
private(set) var bookCovers = [BookCover]()
Expand All @@ -31,11 +33,13 @@ public final class HomeViewModel: ViewModelType {
public init(
fetchMemorialHouseUseCase: FetchMemorialHouseNameUseCase,
fetchAllBookCoverUseCase: FetchAllBookCoverUseCase,
updateBookCoverUseCase: UpdateBookCoverUseCase
updateBookCoverUseCase: UpdateBookCoverUseCase,
deleteBookCoverUseCase: DeleteBookCoverUseCase
) {
self.fetchMemorialHouseNameUseCase = fetchMemorialHouseUseCase
self.fetchAllBookCoverUseCase = fetchAllBookCoverUseCase
self.updateBookCoverUseCase = updateBookCoverUseCase
self.deleteBookCoverUseCase = deleteBookCoverUseCase
}

@MainActor
Expand Down Expand Up @@ -84,6 +88,22 @@ public final class HomeViewModel: ViewModelType {
self.bookCovers = bookCovers
self.currentBookCovers = bookCovers
output.send(.fetchedAllBookCover)

@MainActor
private func deleteBookCover(bookId: UUID) async {
do {
try await deleteBookCoverUseCase.execute(id: bookId)
guard
let bookCoverIndex = bookCovers.firstIndex(where: { $0.id == bookId }),
let currentBookCoverIndex = currentBookCovers.firstIndex(where: { $0.id == bookId })
else { return }

bookCovers.remove(at: bookCoverIndex)
currentBookCovers.remove(at: currentBookCoverIndex)
} catch {
MHLogger.error("삭제 에러 발생: \(error.localizedDescription)")
output.send(.fetchedFailure("삭제에 실패했습니다."))
}
}

private func filterBooks(by category: String) {
Expand All @@ -108,25 +128,4 @@ public final class HomeViewModel: ViewModelType {
currentBookCovers.insert(currentBookCover, at: destinationIndex)
output.send(.dragAndDropFinished)
}

private func likeButtonTapped(bookId: UUID) async throws {
guard
let bookCoverIndex = bookCovers.firstIndex(where: { $0.id == bookId }),
let currentBookCoverindex = currentBookCovers.firstIndex(where: { $0.id == bookId })
else { return }

let currentBookCover = currentBookCovers[currentBookCoverindex]
let bookCover = BookCover(
id: currentBookCover.id,
order: currentBookCover.order,
title: currentBookCover.title,
imageURL: currentBookCover.category,
color: currentBookCover.color,
category: currentBookCover.imageURL,
favorite: !currentBookCover.favorite
)
try await updateBookCoverUseCase.execute(id: bookId, with: bookCover)
bookCovers[bookCoverIndex] = bookCover
currentBookCovers[currentBookCoverindex] = bookCover
}
}

0 comments on commit 17b0ab2

Please sign in to comment.