Skip to content

Commit

Permalink
Feat: 방명록 API 함수 분리 (단일 책임 원칙)
Browse files Browse the repository at this point in the history
SRP에 맞춰 뷰모델 내, 방명록 호출 함수 분리
  • Loading branch information
JEONG-J committed May 9, 2024
1 parent cabe2f1 commit b3c6637
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Moya

enum GuestBookAPITarget {
case getImage(guestBookId: Int, token: String)
case getData(spotId: Int, page: Int, token: String)
case getSpotGuestBookData(spotId: Int, page: Int, token: String)
case getStoreGuestBookData(storeId: Int, page: Int, token: String)
}

extension GuestBookAPITarget: TargetType {
Expand All @@ -20,7 +21,9 @@ extension GuestBookAPITarget: TargetType {
switch self {
case .getImage:
return "/api/v1/image/guestbook"
case .getData:
case .getSpotGuestBookData:
return "/api/v1/guestbook"
case .getStoreGuestBookData:
return "/api/v1/guestbook"
}
}
Expand All @@ -29,24 +32,28 @@ extension GuestBookAPITarget: TargetType {
switch self {
case .getImage:
return .get
case .getData:
case .getSpotGuestBookData:
return .get
case .getStoreGuestBookData:
return .get
}
}

var task: Task {
switch self {
case .getData(let id, let page, _):
return .requestParameters(parameters: ["spotId": id, "pageNum": page], encoding: URLEncoding.queryString)
case .getImage(let id, _):
return .requestParameters(parameters: ["guestBookId": id], encoding: URLEncoding.queryString)
case .getSpotGuestBookData(let id, let page, _):
return .requestParameters(parameters: ["spotId": id, "pageNum": page], encoding: URLEncoding.queryString)
case .getStoreGuestBookData(let id, let page, _):
return .requestParameters(parameters: ["storeId": id, "pageNum": page] , encoding: URLEncoding.queryString)
}
}

var headers: [String : String]? {
let token: String
switch self {
case .getData(_, _, let tokenValue), .getImage(_, let tokenValue):
case .getSpotGuestBookData(_, _, let tokenValue), .getStoreGuestBookData(_, _, let tokenValue) ,.getImage(_, let tokenValue):
token = tokenValue
}
return [
Expand Down
112 changes: 104 additions & 8 deletions ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/DetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DetailViewModel: NSObject, ObservableObject,CLLocationManagerDelegate {
@Published var walkwayDetailDataModel: WalkwayDetailDataModel?
@Published var storeDetailDataModel: StoreDetailDataModel?
@Published var walkwayImageModel: WalkImageModel?
@Published var storeImageModel : StoreImageModel?
@Published var storeImageModel: StoreImageModel?
@Published var guestBookModel: GuestBookModel?

// MARK: - Property
Expand All @@ -40,34 +40,53 @@ class DetailViewModel: NSObject, ObservableObject,CLLocationManagerDelegate {
@Published var currentImageIndex = 0
@Published var placeType: PlaceTypeValue = .spot

private var placeId: Int? = nil

var locationManager = CLLocationManager()
var currentLocation: CLLocation? {
locationManager.location
}

// MARK: - ScrollProperty
@Published var currentPage: Int = 0

//MARK: - API Fetch 함수
/// 장소 타입에 맞춰 API 호출
/// - Parameter place: 전달 받은 장소 타입
public func fetchDetails(for place: ExploreDetailInfor) {
if place.placeType.spot {
self.placeType = .spot
walkWayGet(get: place)
self.placeId = place.placeId
walkWayGet(get: placeId ?? 0)

}
else if place.placeType.store {
self.placeType = .store
storeGet(get: place)
self.placeId = place.placeId
storeGet(get: placeId ?? 0)
self.placeId = place.placeId
}
}

/// 방명록 카드 스크롤 API 호출
/// - Parameter page: 페이징을 값 입력
public func fetchScrollData(page: Int) {
if placeType == .spot {

} else if placeType == .store {

}
}

//MARK: - 산책로 상세 조회 처리

/// 선택한 산책로 데이터를 가져온다.
/// - Parameter place: 선택한 산책로 정보
private func walkWayGet(get place: ExploreDetailInfor) {
private func walkWayGet(get placeId: Int) {

guard let accessToken = KeyChainManager.standard.getAccessToken(for: "userSession") else { return }

provider.request(.fetchWalkWayDetail(spotId: place.placeId, token: accessToken)) { [weak self] result in
provider.request(.fetchWalkWayDetail(spotId: placeId, token: accessToken)) { [weak self] result in
switch result {
case .success(let response):
do {
Expand Down Expand Up @@ -115,11 +134,11 @@ class DetailViewModel: NSObject, ObservableObject,CLLocationManagerDelegate {

/// 매장 상세 데이터 조회
/// - Parameter place: 장소 타입
private func storeGet(get place: ExploreDetailInfor) {
private func storeGet(get placeId: Int) {

guard let accessToken = KeyChainManager.standard.getAccessToken(for: "userSession") else { return }

provider.request(.fetchStoreDetail(storeId: place.placeId, token: accessToken)) { [weak self] result in
provider.request(.fetchStoreDetail(storeId: placeId, token: accessToken)) { [weak self] result in
switch result {
case .success(let response):
do {
Expand Down Expand Up @@ -162,9 +181,86 @@ class DetailViewModel: NSObject, ObservableObject,CLLocationManagerDelegate {
}
}
}
// MARK: - 방명록 카드 조회
/// 페이지 초기화
public func resetPage() {
currentPage = 0
}

/// 방명록 SpotData 받아오는 API 호출 함수
/// - Parameter page: 받고자 하는 페이지 값
/// - Parameter placeId: 받고자하는 placeId 입력
public func getSpotData(page: Int, placeId: Int) {
guard let accessToken = KeyChainManager.standard.getAccessToken(for: "userSession") else {
print("상세 장소 Spot 방명록 조회 액세스 가져오기 오류")
return
}

guestProvider.request(.getSpotGuestBookData(spotId: self.placeId ?? 0,
page: page,
token: accessToken)) {
[weak self] result in

switch result {
case .success(let response):
do {
let decodedData = try JSONDecoder().decode(GuestBookModel.self, from: response.data)
DispatchQueue.main.async {
if page == 0 {
self?.guestBookModel = decodedData
print("방명록 데이터 Spot 조회 1페이지 디코드 완료")
} else {
self?.guestBookModel?.information.append(contentsOf: decodedData.information)
print("방명록 데이터 Spot 조회 추가 페이지 디코드 완료")
}
self?.currentPage = page
}
} catch {
print("방명록 데이터 Spot 조회 오류: \(error)")
}
case .failure(let error):
print("방명록 데이터 Spot 조회 네트워크 오류: \(error)")
}
}
}

//MARK: - 방명록 불러오기 API

/// 방명록 StoreData 받아오는 API 호출 함수
/// - Parameters:
/// - page: 받고자 하는 페이지 값
/// - placeId: 받고자하는 placeId 입력
public func getStoreData(page: Int, placeId: Int) {
guard let accessToken = KeyChainManager.standard.getAccessToken(for: "userSession") else {
print("상세 장소 Store 방명록 조회 액세스 가져오기 오류")
return
}

guestProvider.request(.getStoreGuestBookData(storeId: placeId,
page: page,
token: accessToken)) {
[weak self] result in

switch result {
case .success(let response):
do {
let decodedData = try JSONDecoder().decode(GuestBookModel.self, from: response.data)
DispatchQueue.main.async {
if page == 0 {
self?.guestBookModel = decodedData
print("방명록 데이터 Store 조회 1페이지 디코더 완료")
} else {
self?.guestBookModel?.information.append(contentsOf: decodedData.information)
}
self?.currentPage = page
}
} catch {
print("방명록 데이터 Store 조회 오류: \(error)")
}
case .failure(let error):
print("방명록 데이터 Store 조회 네트워크 오류: \(error)")
}
}
}


// MARK: - 방문하기 버튼
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation


/// 방명록 받아오는 데이터
// MARK: - GuestBookData
struct GuestBookModel: Codable, Hashable {
var check: Bool
var information: [GuestBookModelInfor]
Expand All @@ -25,7 +25,7 @@ struct GuestBookModelInfor: Codable, Hashable {
var star : Float
}


// MARK: - GuestBookImage
struct GuestBookImage: Codable {
var check: Bool
var information: GuestBookImageData
Expand Down

0 comments on commit b3c6637

Please sign in to comment.