From e1a4c46c261a09840487c0324b7f1ad990422455 Mon Sep 17 00:00:00 2001 From: k2645 Date: Mon, 25 Nov 2024 23:41:35 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20toBookCoverDTO,=20toBookCover=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MHData/MHData/DTO/BookCoverDTO.swift | 14 ++++++++ .../CoreData/CoreDataBookCoverStorage.swift | 33 +++++++++++-------- .../Repository/LocalBookCoverRepository.swift | 24 ++------------ 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/MemorialHouse/MHData/MHData/DTO/BookCoverDTO.swift b/MemorialHouse/MHData/MHData/DTO/BookCoverDTO.swift index 0f038eac..0f0b68fc 100644 --- a/MemorialHouse/MHData/MHData/DTO/BookCoverDTO.swift +++ b/MemorialHouse/MHData/MHData/DTO/BookCoverDTO.swift @@ -1,4 +1,5 @@ import MHFoundation +import MHDomain public struct BookCoverDTO { let identifier: UUID @@ -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 + ) + } } diff --git a/MemorialHouse/MHData/MHData/LocalStorage/CoreData/CoreDataBookCoverStorage.swift b/MemorialHouse/MHData/MHData/LocalStorage/CoreData/CoreDataBookCoverStorage.swift index 1d9fa3e5..1c5b33cf 100644 --- a/MemorialHouse/MHData/MHData/LocalStorage/CoreData/CoreDataBookCoverStorage.swift +++ b/MemorialHouse/MHData/MHData/LocalStorage/CoreData/CoreDataBookCoverStorage.swift @@ -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 { @@ -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 + ) + } +} diff --git a/MemorialHouse/MHData/MHData/Repository/LocalBookCoverRepository.swift b/MemorialHouse/MHData/MHData/Repository/LocalBookCoverRepository.swift index 6369a21c..674a24ac 100644 --- a/MemorialHouse/MHData/MHData/Repository/LocalBookCoverRepository.swift +++ b/MemorialHouse/MHData/MHData/Repository/LocalBookCoverRepository.swift @@ -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)") } @@ -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)") }