Skip to content

Commit

Permalink
Merge pull request #90 from Team-MindWay/86-book-domain-setting
Browse files Browse the repository at this point in the history
🔀 :: book 도메인 세팅
  • Loading branch information
shwaaaa authored Jun 19, 2024
2 parents ba7643c + 49df990 commit fccf9db
Show file tree
Hide file tree
Showing 24 changed files with 366 additions and 3 deletions.
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
}
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

0 comments on commit fccf9db

Please sign in to comment.