Skip to content

Commit

Permalink
[#22]Feat: 최근 검색 API 연동 구현
Browse files Browse the repository at this point in the history
- entity, use case response type, repository
- use case interface와 repository interface를 나누기로 했었는데 추후에 할 예정
  • Loading branch information
Minny27 committed Mar 14, 2024
1 parent 39023a8 commit c2fd7f5
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// KeywordResponseDTO.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

struct RecentKeywordResponse: Decodable {
let keywordList: [RecentKeywordResponseDTO]
let page: Int
let totalPages: Int

enum CodingKeys: String, CodingKey {
case keywordList
case page
case totalPages = "total_pages"
}
}

struct RecentKeywordResponseDTO: Decodable {
let keyword: String
}

extension RecentKeywordResponse {
func toDomain() -> RecentKeywordPage {
return RecentKeywordPage(
keywordList: self.keywordList.map { $0.toDomain() },
page: self.page,
totalPages: self.totalPages
)
}
}

extension RecentKeywordResponseDTO {
func toDomain() -> RecentKeyword {
return RecentKeyword(
keyword: self.keyword
)
}
}
28 changes: 28 additions & 0 deletions Nagaza/Sources/Data/Repositories/Map/MapRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// MapRepository.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

import RxSwift
import Moya

final class MapRepository: ProviderProtocol {
typealias Target = MapTarget
var provider: MoyaProvider<Target>

init(isStub: Bool, sampleStatusCode: Int, customEndpointClosure: ((Target) -> Moya.Endpoint)?) {
self.provider = Self.consProvider(isStub, sampleStatusCode, customEndpointClosure)
}
}

extension MapRepository: MapRepositoryInterface {
func fetchRecentKeywordList() -> Single<RecentKeywordPage> {
request(
type: RecentKeywordResponse.self,
target: .fetchRecentKeywordList
)
.map { $0.toDomain() }
}
}
39 changes: 39 additions & 0 deletions Nagaza/Sources/Data/Repositories/Map/MapTarget+SampleData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// MapTarget+SampleData.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

import Foundation

import Moya

extension MapTarget {
var sampleData: Data {
switch self {
case .fetchRecentKeywordList:
return Data(
"""
{
"keywordList": [
{
"keyword": "리그오브디저트"
},
{
"keyword": "방탈출"
},
{
"keyword": "풀문"
},
{
"keyword": "마스터키"
}
],
"page": 1,
"total_pages": 1
}
""".utf8)
}
}
}
36 changes: 36 additions & 0 deletions Nagaza/Sources/Data/Repositories/Map/MapTarget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// MapTarget.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

import Moya

enum MapTarget {
case fetchRecentKeywordList
}

extension MapTarget: BaseTargetType {
var path: String {
switch self {
case .fetchRecentKeywordList:
return "v1/search/keyword"
}
}

var method: Moya.Method {
switch self {
case .fetchRecentKeywordList:
return .get
}
}

var task: Moya.Task {
switch self {
case .fetchRecentKeywordList:
return .requestPlain
}
}
}

33 changes: 33 additions & 0 deletions Nagaza/Sources/Domain/Entities/RecentKeyword.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// RecentKeyword.swift
// Nagaza
//
// Created by SeungMin on 3/13/24.
//

import Foundation

enum SearchSection {
case home
}

struct RecentKeyword: Hashable {
let identifier = UUID()
let keyword: String
}

struct RecentKeywordPage {
let keywordList: [RecentKeyword]
let page: Int
let totalPages: Int
}

extension RecentKeyword {
public func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}

public static func == (lhs: RecentKeyword, rhs: RecentKeyword) -> Bool {
lhs.identifier == rhs.identifier
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// Created by SeungMin on 1/6/24.
//

import Foundation

import RxSwift

protocol HomeRepositoryInterface {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// MapRepositoryInterface.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

import RxSwift

protocol MapRepositoryInterface {
func fetchRecentKeywordList() -> Single<RecentKeywordPage>
}
20 changes: 20 additions & 0 deletions Nagaza/Sources/Domain/UseCases/Map/MapUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MapUseCase.swift
// Nagaza
//
// Created by SeungMin on 3/14/24.
//

import RxSwift

final class MapUseCase {
private let repository: MapRepositoryInterface

init(roomsRepository: MapRepositoryInterface) {
self.repository = roomsRepository
}

func fetchCafesList() -> Single<RecentKeywordPage> {
return repository.fetchRecentKeywordList()
}
}

0 comments on commit c2fd7f5

Please sign in to comment.