Skip to content

Commit

Permalink
feat: toBookCoverDTO, toBookCover 메서드 구현 및 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
k2645 committed Nov 25, 2024
1 parent c22a99a commit e1a4c46
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
14 changes: 14 additions & 0 deletions MemorialHouse/MHData/MHData/DTO/BookCoverDTO.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MHFoundation
import MHDomain

public struct BookCoverDTO {
let identifier: UUID
Expand All @@ -7,4 +8,17 @@ public struct BookCoverDTO {
let color: String
let category: String?
let favorite: Bool

func toBookCover() -> BookCover? {
guard let color = BookColor(rawValue: self.color) else { return nil }

return BookCover(
identifier: self.identifier,
title: self.title,
imageURL: self.imageURL,
color: color,
category: self.category,
favorite: self.favorite
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ extension CoreDataBookCoverStorage: BookCoverStorage {

do {
let bookCoverEntities = try context.fetch(request)
let result = bookCoverEntities.compactMap { entity -> BookCoverDTO? in
guard let identifier = entity.identifier,
let title = entity.title,
let color = entity.color else { return nil }

return BookCoverDTO(
identifier: identifier,
title: title,
imageURL: entity.imageURL,
color: color,
category: entity.category,
favorite: entity.favorite
)
}
let result = bookCoverEntities.compactMap { $0.toBookCoverDTO() }

return .success(result)
} catch {
Expand Down Expand Up @@ -98,3 +85,21 @@ extension CoreDataBookCoverStorage: BookCoverStorage {
return try context.fetch(request).first(where: { $0.identifier == id })
}
}

// MARK: - BookCoverEntity Extension
extension BookCoverEntity {
func toBookCoverDTO() -> BookCoverDTO? {
guard let identifier = self.identifier,
let title = self.title,
let color = self.color else { return nil }

return BookCoverDTO(
identifier: identifier,
title: title,
imageURL: self.imageURL,
color: color,
category: self.category,
favorite: self.favorite
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ public struct LocalBookCoverRepository: BookCoverRepository {

switch result {
case .success(let bookCoverDTOs):
return bookCoverDTOs.compactMap { (dto: BookCoverDTO) -> BookCover? in
guard let color = BookColor(rawValue: dto.color) else { return nil }
return BookCover(
identifier: dto.identifier,
title: dto.title,
imageURL: dto.imageURL,
color: color,
category: dto.category,
favorite: dto.favorite
)
}
return bookCoverDTOs.compactMap { $0.toBookCover() }
case .failure(let failure):
MHLogger.debug("\(failure.description)")
}
Expand All @@ -50,17 +40,7 @@ public struct LocalBookCoverRepository: BookCoverRepository {
switch result {
case .success(let bookCoverDTOs):
let bookCoverDTO = bookCoverDTOs.filter({ $0.identifier == id }).first
guard let bookCoverDTO,
let color = BookColor(rawValue: bookCoverDTO.color) else { return nil }

return BookCover(
identifier: bookCoverDTO.identifier,
title: bookCoverDTO.title,
imageURL: bookCoverDTO.imageURL,
color: color,
category: bookCoverDTO.category,
favorite: bookCoverDTO.favorite
)
return bookCoverDTO?.toBookCover()
case .failure(let failure):
MHLogger.debug("\(failure.description)")
}
Expand Down

0 comments on commit e1a4c46

Please sign in to comment.