Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”€ :: book 도메인 μ„ΈνŒ… #90

Merged
merged 14 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Service/Sources/Domain/BookDomain/API/BookAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation
import Moya

public enum BookAPI {
case writeBook(req: BookInfoRequestDTO)
case bookList
case bookDetail(book_id: Int)
case modifyBook(book_id: Int, req: BookInfoRequestDTO)
case deleteBook(book_id: Int)
}

extension BookAPI: MindWayAPI {
public typealias ErrorType = BookDomainError

public var domain: MindWayDomain {
.book
}

public var urlPath: String {
switch self {
case .writeBook, .bookList:
return ""
case let .bookDetail(book_id),
let .modifyBook(book_id, _),
let .deleteBook(book_id):
return "/\(book_id)"
}
}

public var method: Moya.Method {
switch self {
case .bookList, .bookDetail:
return .get
case .writeBook:
return .post
case .modifyBook:
return .patch
case .deleteBook:
return .delete
}
}

public var task: Moya.Task {
switch self {
case let .writeBook(req):
return .requestJSONEncodable(req)
case let .modifyBook(_, req):
return .requestJSONEncodable(req)
case .bookList, .bookDetail, .deleteBook:
return .requestPlain
}
}

public var jwtTokenType: JwtTokenType {
switch self {
default:
return .accessToken
}
}

public var errorMap: [Int: ErrorType] {
switch self {
default:
return [
401: .unauthorized
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

public struct FetchBookDetailInfoResponseDTO: Decodable {
public let title: String
public let plot: String

public init(
title: String,
plot: String
) {
self.title = title
self.plot = plot
}
}

extension FetchBookDetailInfoResponseDTO {
func toDomain() -> BookDetialInfoEntity {
BookDetialInfoEntity(
title: title,
plot: plot
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation

public struct FetchBookInfoResponseDTO: Decodable {
public let book: [BookInfoResponseDTO]

public init(book: [BookInfoResponseDTO]) {
self.book = book
}
}

public struct BookInfoResponseDTO: Decodable {
public let id: Int
public let title: String
public let plot: String
public let created_at: Date

public init(
id: Int,
title: String,
plot: String,
created_at: Date
) {
self.id = id
self.title = title
self.plot = plot
self.created_at = created_at
}
}

extension FetchBookInfoResponseDTO {
func toDomain() -> [BookInfoEntity] {
book.map { $0.toDomain() }
}
}

extension BookInfoResponseDTO {
func toDomain() -> BookInfoEntity {
BookInfoEntity(
id: id,
title: title,
plot: plot,
created_at: created_at
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

public final class RemoteBookDataSourceImpl: BaseRemoteDataSource<BookAPI>, RemoteBookDataSource {
public func writeBook(req: BookInfoRequestDTO) async throws {
try await request(.writeBook(req: req))
}

public func fetchBookList() async throws -> [BookInfoEntity] {
try await request(.bookList, dto: FetchBookInfoResponseDTO.self)
.toDomain()
}

public func modifyBook(book_id: Int, req: BookInfoRequestDTO) async throws {
try await request(.modifyBook(book_id: book_id, req: req))
}

public func deleteBook(book_id: Int) async throws {
try await request(.deleteBook(book_id: book_id))
}

public func fetchBookDetail(book_id: Int) async throws -> BookDetailInfoEntity {
try await request(.bookDetail(book_id: book_id), dto: FetchBookDetailInfoResponseDTO.self)
.toDomain()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

public struct BookDetailInfoEntity: Equatable {
public let title: String
public let plot: String

public init(
title: String,
plot: String
) {
self.title = title
self.plot = plot
}
}
20 changes: 20 additions & 0 deletions Service/Sources/Domain/BookDomain/Entity/BookInfoEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

public struct BookInfoEntity: Equatable {
public let id: Int
public let title: String
public let plot: String
public let created_at: Date

public init(
id: Int,
title: String,
plot: String,
created_at: Date
) {
self.id = id
self.title = title
self.plot = plot
self.created_at = created_at
}
}
14 changes: 14 additions & 0 deletions Service/Sources/Domain/BookDomain/Error/BookDomainError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

public enum BookDomainError: Error {
case unauthorized
}

extension BookDomainError: LocalizedError {
public var errorDescription: String? {
switch self {
case .unauthorized:
return "κΆŒν•œμ΄ μ—†μŠ΅λ‹ˆλ‹€."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

public struct BookRepositoryImpl: BookRepository {
private let remoteBookDataSource: any RemoteBookDataSource

public init(
remoteBookDataSource: any RemoteBookDataSource
) {
self.remoteBookDataSource = remoteBookDataSource
}

public func writeBook(req: BookInfoRequestDTO) async throws {
try await remoteBookDataSource.writeBook(req: req)
}

public func fetchBookList() async throws -> [BookInfoEntity] {
try await remoteBookDataSource.fetchBookList()
}

public func modifyBook(req: BookInfoRequestDTO) async throws {
try await remoteBookDataSource.modifyBook(req: req)
}

public func deleteBook() async throws {
try await remoteBookDataSource.deleteBook()
}

public func fetchBookDetail() async throws -> BookDetailInfoEntity {
try await remoteBookDataSource.fetchBookDetail()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct DeleteBookUseCaseImpl: DeleteBookUseCase {
private let bookRepository: any BookRepository

public init(bookRepository: any BookRepository) {
self.bookRepository = bookRepository
}

public func execute() async throws {
try await bookRepository.deleteBook()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct FetchBookDetailUseCaseImpl: FetchBookDetailUseCase {
private let bookRepository: any BookRepository

public init(bookRepository: any BookRepository) {
self.bookRepository = bookRepository
}

public func execute() async throws -> BookDetailInfoEntity {
try await bookRepository.fetchBookDetail()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct FetchBookListUseCaseImpl: FetchBookListUseCase {
private let bookRepository: any BookRepository

public init(bookRepository: any BookRepository) {
self.bookRepository = bookRepository
}

public func execute() async throws -> [BookInfoEntity]{
try await bookRepository.fetchBookList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct ModifyBookUseCaseImpl: ModifyBookUseCase {
private let bookRepository: any BookRepository

public init(bookRepository: any BookRepository) {
self.bookRepository = bookRepository
}

public func execute(req: BookInfoRequestDTO) async throws {
try await bookRepository.modifyBook(req: req)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

public struct WriteBookUseCaseImpl: WriteBookUseCase {
private let bookRepository: any BookRepository

public init(bookRepository: any BookRepository) {
self.bookRepository = bookRepository
}

public func execute(req: BookInfoRequestDTO) async throws {
try await bookRepository.writeBook(req: req)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public struct SettingGoalUseCaseImpl: SettingGoalUseCase {
self.goalRepository = goalRepository
}

public func execute() async throws {
try await goalRepository.settingGoal()
public func execute(req: SettingGoalRequestDTO) async throws {
try await goalRepository.settingGoal(req: req)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

public struct BookInfoRequestDTO: Encodable {
public let title: String
public let plot: String

public init(
title: String,
plot: String
) {
self.title = title
self.plot = plot
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public protocol RemoteBookDataSource {
func writeBook(req: BookInfoRequestDTO) async throws
func fetchBookList() async throws -> [BookInfoEntity]
func modifyBook(book_id: Int, req: BookInfoRequestDTO) async throws
func deleteBook(book_id: Int) async throws
func fetchBookDetail(book_id: Int) async throws -> BookDetailInfoEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public protocol BookRepository {
func writeBook(req: BookInfoRequestDTO) async throws
func fetchBookList() async throws -> [BookInfoEntity]
func modifyBook(book_id: Int, req: BookInfoRequestDTO) async throws
func deleteBook(book_id: Int) async throws
func fetchBookDetail(book_id: Int) async throws -> BookDetailInfoEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol DeleteBookUseCase {
func execute(book_id: Int) async throws
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API λͺ…μ„Έμ„œμ— Long이면 String으둜 쓰지 μ•Šλ‚˜μš”??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

long이면 μ‹€μˆ˜ν˜•μ΄μ–΄μ„œ Int둜 μ¨μ•Όν•˜λŠ”κ±Έλ‘œ μ•Œκ³ μžˆμ–΄μš”!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol FetchBookDetailUseCase {
func execute(book_id: Int) async throws -> BookDetailInfoEntity
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol FetchBookListUseCase {
func execute() async throws -> [BookInfoEntity]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol ModifyBookUseCase {
func execute(book_id: Int, req: BookInfoRequestDTO) async throws
}
5 changes: 5 additions & 0 deletions Service/Sources/Interface/UseCase/Book/WriteBookUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol WriteBookUseCase {
func execute(req: BookInfoRequestDTO) async throws
}
Loading
Loading