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

[Feat] #116 - 스크랩 API 연결 #127

Merged
merged 11 commits into from
Jul 18, 2022
36 changes: 24 additions & 12 deletions HealthFoodMe/HealthFoodMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
695758EC28830E3000E36789 /* SplashVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 695758EB28830E3000E36789 /* SplashVC.swift */; };
695758EE28830E3F00E36789 /* Splash.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 695758ED28830E3F00E36789 /* Splash.storyboard */; };
695758F4288545CC00E36789 /* CopingTabEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 695758F3288545CC00E36789 /* CopingTabEntity.swift */; };
A90CFDB528852C960018AB94 /* ScrapListEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = A90CFDB428852C960018AB94 /* ScrapListEntity.swift */; };
A90CFDBA28856BCB0018AB94 /* ScrapEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = A90CFDB928856BCB0018AB94 /* ScrapEntity.swift */; };
A9325274287D3065001EDF50 /* SearchResultTVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9325273287D3065001EDF50 /* SearchResultTVC.swift */; };
A9325277287D4F10001EDF50 /* SearchResultModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9325276287D4F10001EDF50 /* SearchResultModel.swift */; };
A932527E287DD261001EDF50 /* SearchResultVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A932527D287DD261001EDF50 /* SearchResultVC.swift */; };
Expand All @@ -64,7 +66,6 @@
A93252B5287F2CA6001EDF50 /* ScrapCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252B4287F2CA6001EDF50 /* ScrapCVC.swift */; };
A93252B7287F3272001EDF50 /* Scrap.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A93252B6287F3272001EDF50 /* Scrap.storyboard */; };
A93252BA287F3E73001EDF50 /* ScrapEmptyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252B9287F3E73001EDF50 /* ScrapEmptyView.swift */; };
A93252BD287F52E4001EDF50 /* ScrapDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252BC287F52E4001EDF50 /* ScrapDataModel.swift */; };
A93252C628808B82001EDF50 /* ReviewDeleteAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252C528808B82001EDF50 /* ReviewDeleteAlertView.swift */; };
A93252CD28808BE2001EDF50 /* LogoutAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252CC28808BE2001EDF50 /* LogoutAlertView.swift */; };
A93252CF2880913F001EDF50 /* HelfmeAlertVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A93252CE2880913F001EDF50 /* HelfmeAlertVC.swift */; };
Expand Down Expand Up @@ -262,6 +263,8 @@
695758EB28830E3000E36789 /* SplashVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashVC.swift; sourceTree = "<group>"; };
695758ED28830E3F00E36789 /* Splash.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Splash.storyboard; sourceTree = "<group>"; };
695758F3288545CC00E36789 /* CopingTabEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CopingTabEntity.swift; sourceTree = "<group>"; };
A90CFDB428852C960018AB94 /* ScrapListEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrapListEntity.swift; sourceTree = "<group>"; };
A90CFDB928856BCB0018AB94 /* ScrapEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrapEntity.swift; sourceTree = "<group>"; };
A9325273287D3065001EDF50 /* SearchResultTVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultTVC.swift; sourceTree = "<group>"; };
A9325276287D4F10001EDF50 /* SearchResultModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultModel.swift; sourceTree = "<group>"; };
A932527D287DD261001EDF50 /* SearchResultVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultVC.swift; sourceTree = "<group>"; };
Expand All @@ -271,7 +274,6 @@
A93252B4287F2CA6001EDF50 /* ScrapCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrapCVC.swift; sourceTree = "<group>"; };
A93252B6287F3272001EDF50 /* Scrap.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Scrap.storyboard; sourceTree = "<group>"; };
A93252B9287F3E73001EDF50 /* ScrapEmptyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrapEmptyView.swift; sourceTree = "<group>"; };
A93252BC287F52E4001EDF50 /* ScrapDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrapDataModel.swift; sourceTree = "<group>"; };
A93252C528808B82001EDF50 /* ReviewDeleteAlertView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReviewDeleteAlertView.swift; sourceTree = "<group>"; };
A93252CC28808BE2001EDF50 /* LogoutAlertView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoutAlertView.swift; sourceTree = "<group>"; };
A93252CE2880913F001EDF50 /* HelfmeAlertVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelfmeAlertVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -720,6 +722,23 @@
name = "Recovered References";
sourceTree = "<group>";
};
A90CFDB228852C550018AB94 /* User */ = {
isa = PBXGroup;
children = (
A90CFDB828853B4A0018AB94 /* ScrapScene */,
);
path = User;
sourceTree = "<group>";
};
A90CFDB828853B4A0018AB94 /* ScrapScene */ = {
isa = PBXGroup;
children = (
A90CFDB428852C960018AB94 /* ScrapListEntity.swift */,
A90CFDB928856BCB0018AB94 /* ScrapEntity.swift */,
);
path = ScrapScene;
sourceTree = "<group>";
};
A9325272287D24CD001EDF50 /* Search */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -755,7 +774,6 @@
A93252AF287F2ADB001EDF50 /* ScrapScene */ = {
isa = PBXGroup;
children = (
A93252BB287F52B0001EDF50 /* Model */,
A93252B2287F2AFE001EDF50 /* VC */,
A93252B3287F2B36001EDF50 /* Cells */,
A93252B8287F3E4D001EDF50 /* View */,
Expand Down Expand Up @@ -788,14 +806,6 @@
path = View;
sourceTree = "<group>";
};
A93252BB287F52B0001EDF50 /* Model */ = {
isa = PBXGroup;
children = (
A93252BC287F52E4001EDF50 /* ScrapDataModel.swift */,
);
path = Model;
sourceTree = "<group>";
};
A93252BE288086BA001EDF50 /* HelpmeAlerts */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1535,6 +1545,7 @@
EBF66B062872AE4200DE0ED1 /* Entity */ = {
isa = PBXGroup;
children = (
A90CFDB228852C550018AB94 /* User */,
FDF2E21A287F5D7F0007D4F9 /* Author */,
EB6A44F82873BF4000749582 /* Map */,
EB6A44DC28737DFA00749582 /* Detail */,
Expand Down Expand Up @@ -1919,6 +1930,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A90CFDBA28856BCB0018AB94 /* ScrapEntity.swift in Sources */,
EBF66AFF287227F500DE0ED1 /* makeAlert.swift in Sources */,
EBF66AF3287227F500DE0ED1 /* applyShadow.swift in Sources */,
EB6A44D628737CC400749582 /* MainDetailEntity.swift in Sources */,
Expand Down Expand Up @@ -1963,10 +1975,10 @@
EBF66AFE287227F500DE0ED1 /* makeVibrate.swift in Sources */,
FDF2E1FC287EFC710007D4F9 /* StarRatingSlider.swift in Sources */,
695758DE28811E9D00E36789 /* Config.swift in Sources */,
A90CFDB528852C960018AB94 /* ScrapListEntity.swift in Sources */,
EBD19262287DBC1800EA053E /* MapDetailSummaryView.swift in Sources */,
EBF66B0A2872B21F00DE0ED1 /* StringLiterals.swift in Sources */,
EB6A45172874276B00749582 /* DetailTabTitleHeader.swift in Sources */,
A93252BD287F52E4001EDF50 /* ScrapDataModel.swift in Sources */,
FD39FACE2880C30100A01CDA /* NicknameChangeUseCase.swift in Sources */,
EB6A451B2875365B00749582 /* TabIndicator.swift in Sources */,
A93252BA287F3E73001EDF50 /* ScrapEmptyView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ScrapEntity.swift
// HealthFoodMe
//
// Created by 김영인 on 2022/07/18.
//

import Foundation

struct ScrapEntity: Codable {
let isScrap: Bool
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ScrapListEntity.swift
// HealthFoodMe
//
// Created by 김영인 on 2022/07/18.
//

import Foundation

struct ScrapListEntity: Codable {
let _id, name, logo: String
let score: Int
let category: String
let hashtag: [String]
let address: String
let latitude, longtitude: Double
}
26 changes: 21 additions & 5 deletions HealthFoodMe/HealthFoodMe/Data/Network/Routers/UserRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,44 @@
import Alamofire

enum UserRouter {

case getScrapList(userId: String)
case putScrap(userId: String, restaurantId: String)
}

extension UserRouter: BaseRouter {
var method: HTTPMethod {
switch self {
default :
case .getScrapList:
return .get
case .putScrap:
return .put
}
}

var path: String {
switch self {
default:
return ""
case .getScrapList(let userId):
return "/user/\(userId)/scrapList"
case .putScrap(let userId, let restaurantId):
return "/user/\(userId)/scrap/\(restaurantId)"
}
}

var parameters: RequestParams {
switch self {
default:
case .getScrapList:
return .requestPlain
case .putScrap:
return .requestPlain
}
}

var header: HeaderType {
switch self {
case .getScrapList:
return .withToken
case .putScrap:
return .withToken
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ class UserService: BaseService {
}

extension UserService {
func getScrapList(userId: String, completion: @escaping(NetworkResult<Any>) -> Void) {
requestObject(UserRouter.getScrapList(userId: userId), type: [ScrapListEntity].self, decodingMode: .model, completion: completion)
}

func putScrap(userId: String, restaurantId: String, completion: @escaping(NetworkResult<Any>) -> Void) {
requestObject(UserRouter.putScrap(userId: userId, restaurantId: restaurantId), type: ScrapEntity.self, decodingMode: .message, completion: completion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import UIKit
import SnapKit

protocol ScrapCVCDelegate: AnyObject {
func scrapCVCButtonDidTap(index: Int, isSelected: Bool)
func scrapCVCButtonDidTap(restaurantId: String)
}

class ScrapCVC: UICollectionViewCell, UICollectionViewRegisterable {

// MARK: - Properties

static var isFromNib: Bool = false
var index: Int = 0
weak var delegate: ScrapCVCDelegate?
var restaurantId: String = ""

// MARK: - UI Components

Expand Down Expand Up @@ -70,17 +70,18 @@ class ScrapCVC: UICollectionViewCell, UICollectionViewRegisterable {
extension ScrapCVC {
@objc func didTapScrapButton(_ sender: UIButton) {
sender.isSelected.toggle()
delegate?.scrapCVCButtonDidTap(index: index, isSelected: sender.isSelected)
delegate?.scrapCVCButtonDidTap(restaurantId: restaurantId)
}
}

// MARK: - Methods

extension ScrapCVC {
func setData(data: ScrapDataModel) {
storeImageView.image = UIImage(named: data.scrapimageUrl)
storeNameLabel.text = data.storeName
locationLabel.text = data.storeLocation
func setData(data: ScrapListEntity) {
storeImageView.image = UIImage(named: data.logo)
storeNameLabel.text = data.name
locationLabel.text = data.address
restaurantId = data._id
}

private func setUI() {
Expand All @@ -90,9 +91,7 @@ extension ScrapCVC {
}

private func setLayout() {
contentView.addSubviews(storeImageView,
scrapButton,
storeNameLabel,
contentView.addSubviews(storeImageView, scrapButton, storeNameLabel,
locationLabel)

storeImageView.snp.makeConstraints {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import UIKit

import SnapKit
import RealmSwift

class ScrapVC: UIViewController {

// MARK: - Properties

private let scrapEmptyView = ScrapEmptyView()
private var scrapList: [ScrapDataModel] = []
private var scrapList: [ScrapListEntity] = []

// MARK: - UI Components

Expand Down Expand Up @@ -78,7 +79,7 @@ extension ScrapVC {

extension ScrapVC {
private func fetchData() {
scrapList = ScrapDataModel.sampleScrapData
getScrapList(userId: "62d4e93f0ff2f900ea88bed1")
isScrapEmpty()
}

Expand Down Expand Up @@ -165,7 +166,7 @@ extension ScrapVC: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ScrapCVC.className, for: indexPath) as? ScrapCVC else { return UICollectionViewCell() }
cell.setData(data: scrapList[indexPath.row])
cell.index = indexPath.row
cell.restaurantId = scrapList[indexPath.row]._id
cell.delegate = self
return cell
}
Expand Down Expand Up @@ -193,8 +194,8 @@ extension ScrapVC: UICollectionViewDelegateFlowLayout {
}

extension ScrapVC: ScrapCVCDelegate {
func scrapCVCButtonDidTap(index: Int, isSelected: Bool) {
print("\(index) 번 스크랩 \(isSelected) 상태")
func scrapCVCButtonDidTap(restaurantId: String) {
putScrap(userId: "62d4e93f0ff2f900ea88bed1", restaurantId: restaurantId)
}
}

Expand All @@ -207,5 +208,29 @@ extension ScrapVC: ScrapEmptyViewDelegate {
// MARK: - Network

extension ScrapVC {
func getScrapList(userId: String) {
UserService.shared.getScrapList(userId: userId) { networkResult in
switch networkResult {
case .success(let data):
if let data = data as? [ScrapListEntity] {
self.scrapList = data
self.isScrapEmpty()
self.scrapCollectionView.reloadData()
}
default:
break;
}
}
}

func putScrap(userId: String, restaurantId: String) {
UserService.shared.putScrap(userId: userId, restaurantId: restaurantId) { networkResult in
switch networkResult {
case .success(let message):
print(message)
default:
break;
}
}
}
}