diff --git a/ttubeokAR/ttubeokAR/ExploreView/ExploreAPI/APITarget/GuestBookAPITarget.swift b/ttubeokAR/ttubeokAR/ExploreView/ExploreAPI/APITarget/GuestBookAPITarget.swift index 026d082..3d0474f 100644 --- a/ttubeokAR/ttubeokAR/ExploreView/ExploreAPI/APITarget/GuestBookAPITarget.swift +++ b/ttubeokAR/ttubeokAR/ExploreView/ExploreAPI/APITarget/GuestBookAPITarget.swift @@ -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 { @@ -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" } } @@ -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 [ diff --git a/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/DetailViewModel.swift b/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/DetailViewModel.swift index 0a15aae..dbc07e9 100644 --- a/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/DetailViewModel.swift +++ b/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/DetailViewModel.swift @@ -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 @@ -40,22 +40,41 @@ 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 { + } } @@ -63,11 +82,11 @@ class DetailViewModel: NSObject, ObservableObject,CLLocationManagerDelegate { /// 선택한 산책로 데이터를 가져온다. /// - 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 { @@ -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 { @@ -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: - 방문하기 버튼 diff --git a/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/GuestBookCard/GuestBookModel.swift b/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/GuestBookCard/GuestBookModel.swift index 0e9e382..88c2714 100644 --- a/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/GuestBookCard/GuestBookModel.swift +++ b/ttubeokAR/ttubeokAR/ExploreView/ExploreDetail/GuestBookCard/GuestBookModel.swift @@ -8,7 +8,7 @@ import Foundation -/// 방명록 받아오는 데이터 +// MARK: - GuestBookData struct GuestBookModel: Codable, Hashable { var check: Bool var information: [GuestBookModelInfor] @@ -25,7 +25,7 @@ struct GuestBookModelInfor: Codable, Hashable { var star : Float } - +// MARK: - GuestBookImage struct GuestBookImage: Codable { var check: Bool var information: GuestBookImageData