Skip to content

Commit

Permalink
Feat: api 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
SOOHYUNLEE08 committed Aug 16, 2024
1 parent 1edf2de commit 46081b6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 19 deletions.
13 changes: 6 additions & 7 deletions Drink-EG/Drink-EG/Resources/APIs/TastingNoteAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum TastingNoteAPI {
case getWineName(wineName: String)
case getNoteID(noteId: Int)
case postNewNote(wineId: Int, color: String, sugarContent: Int, acidity: Int, tannin: Int, body: Int, alcohol: Int, scentAroma: [String], scentTaste: [String], scentFinish: [String], satisfaction: Int, memo: String)
case patchNote(noteId: Int, wineId: Int, color: String, sugarContent: Int, acidity: Int, tannin: Int, body: Int, alcohol: Int, scentAroma: [String], scentTaste: [String], scentFinish: [String], satisfaction: Int, memo: String)
case patchNote(wineId: Int, color: String, sugarContent: Int, acidity: Int, tannin: Int, body: Int, alcohol: Int, scentAroma: [String], scentTaste: [String], scentFinish: [String], satisfaction: Int, review: String)
case deleteNote(noteId: Int)
}

Expand All @@ -38,11 +38,11 @@ extension TastingNoteAPI: TargetType {
case .getAllNotes:
return "tasting-note/all-note"
case .getNoteID(let noteId):
return "\(noteId)"
return "tasting-note/\(noteId)"
case .postNewNote:
return "tasting-note/new-note"
case .patchNote(let noteId, let wineId, let color, let sugarContent, let acidity, let tannin, let body, let alcohol, let scentAroma, let scentTaste, let scentFinish, let satisfaction, let memo):
return "tasting-note/\(noteId)"
case .patchNote(let wineId, let color, let sugarContent, let acidity, let tannin, let body, let alcohol, let scentAroma, let scentTaste, let scentFinish, let satisfaction, let review):
return "wine-note/\(wineId)"
case .deleteNote(let noteId):
return "tasting-note/\(noteId)"
}
Expand Down Expand Up @@ -84,9 +84,8 @@ extension TastingNoteAPI: TargetType {
"review": review
]
return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
case .patchNote(_, let wineId, let color, let sugarContent, let acidity, let tannin, let body, let alcohol, let scentAroma, let scentTaste, let scentFinish, let satisfaction, let memo):
case .patchNote(_, let color, let sugarContent, let acidity, let tannin, let body, let alcohol, let scentAroma, let scentTaste, let scentFinish, let satisfaction, let review):
let parameters: [String: Any] = [
"wineId": wineId,
"color": color,
"sugarContent": sugarContent,
"acidity": acidity,
Expand All @@ -97,7 +96,7 @@ extension TastingNoteAPI: TargetType {
"scentTaste": scentTaste,
"scentFinish": scentFinish,
"satisfaction": satisfaction,
"memo": memo
"review": review
]
return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
case .deleteNote:
Expand Down
100 changes: 97 additions & 3 deletions Drink-EG/Drink-EG/Sources/VCs/NoteListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class NoteListViewController: UIViewController, UICollectionViewDelegate, UIColl
let noteListLabel = UILabel() // 노트 보관함 Label
var noteListGrid: UICollectionView! // 테이스팅 노트를 보관할 CollectionView
let images = ["Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci", "Castello Monaci"]
var apiResult: [[String: String]] = []
var cellCount: Int = 0

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Expand Down Expand Up @@ -192,17 +194,23 @@ class NoteListViewController: UIViewController, UICollectionViewDelegate, UIColl

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// CollectionView Cell 개수를 설정하는 함수
return 12
return cellCount
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // 재사용 가능한 셀을 가져와서 NoteCollectionViewCell로 캐스팅
let cell = noteListGrid.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NoteCollectionViewCell

cell.imageView.image = UIImage(named: "Castello Monaci")
cell.nameLabel.text = "와인 이름\n1999"
let wineData = apiResult[indexPath.row]

cell.nameLabel.text = wineData["name"]
cell.nameLabel.numberOfLines = 2
cell.nameLabel.font = UIFont(name: "Pretendard-Bold", size: 14)
cell.backgroundColor = .clear
if let imageUrl = wineData["imageUrl"], let url = URL(string: imageUrl) {
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder"))
} else {
cell.imageView.image = UIImage(named: "placeholder")
}

return cell
}
Expand All @@ -219,6 +227,76 @@ class NoteListViewController: UIViewController, UICollectionViewDelegate, UIColl
return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedNoteId = apiResult[indexPath.row]["noteId"] // noteId 가져오기
guard let noteId = Int(selectedNoteId ?? "") else { return }

fetchNoteDetails(noteId: noteId)
}

func fetchNoteDetails(noteId: Int) {
provider.request(TastingNoteAPI.getNoteID(noteId: noteId)) { result in
switch result {
case .success(let response):
self.handleNoteDetailsResponse(response)
case .failure(let error):
print("Request failed: \(error)")
}
}
}

func handleNoteDetailsResponse(_ response: Response) {
do {
if let jsonData = try response.mapJSON() as? [String: Any] {
// 응답 데이터를 콘솔에 출력
print("Response JSON: \(jsonData)")

if let resultData = jsonData["result"] as? [String: Any] {
let scentAroma = (resultData["scentAroma"] as? [String] ?? []).map { $0.unescapedString }
let scentTaste = (resultData["scentTaste"] as? [String] ?? []).map { $0.unescapedString }
let scentFinish = (resultData["scentFinish"] as? [String] ?? []).map { $0.unescapedString }

let review = (resultData["review"] as? String ?? "").unescapedString
let satisfaction = resultData["satisfaction"] as? Int ?? 0
let sugarContent = resultData["sugarContent"] as? Int ?? 0
let acidity = resultData["acidity"] as? Int ?? 0
let tannin = resultData["tannin"] as? Int ?? 0
let alcohol = resultData["alcohol"] as? Int ?? 0
let body = resultData["body"] as? Int ?? 0
let wineName = resultData["name"] as? String ?? ""
let wineImageUrl = resultData["picture"] as? String ?? ""

let scentData: [String: [String]] = [
"scentAroma": scentAroma,
"scentTaste": scentTaste,
"scentFinish": scentFinish
]

let dataList: [RadarChartData] = [
RadarChartData(type: .acid, value: acidity),
RadarChartData(type: .tannin, value: tannin),
RadarChartData(type: .alcohol, value: alcohol),
RadarChartData(type: .bodied, value: body),
RadarChartData(type: .sweetness, value: sugarContent)
]

let nextVC = CheckNoteViewController()
nextVC.reviewString = review
nextVC.value = Double(satisfaction)
nextVC.dataList = dataList
nextVC.selectedOptions = scentData
nextVC.selectedWineName = wineName
nextVC.selectedWineImage = wineImageUrl

self.navigationController?.pushViewController(nextVC, animated: true)
}
}
} catch {
print("Failed to map data: \(error)")
}
}


// MARK: "새로 적기" 버튼에 관한 UI
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionFooter {
Expand All @@ -241,6 +319,14 @@ class NoteListViewController: UIViewController, UICollectionViewDelegate, UIColl
case .success(let response):
do {
let data = try response.map(AllNotesResponse.self)
self.cellCount = data.result.count
for note in data.result {
let wineData: [String: String] = ["noteId": "\(note.noteId)", "name": note.name, "imageUrl": note.imageUrl]
self.apiResult.append(wineData)
}
print(self.apiResult)
print(self.cellCount)
self.noteListGrid.reloadData()
print("User Data: \(data)")
} catch {
print("Failed to map data: \(error)")
Expand All @@ -251,3 +337,11 @@ class NoteListViewController: UIViewController, UICollectionViewDelegate, UIColl
}
}
}

extension String {
var unescapedString: String {
var mutableString = NSMutableString(string: self)
CFStringTransform(mutableString, nil, "Any-Hex/Java" as NSString, true)
return mutableString as String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class CheckNoteViewController: UIViewController {
name.snp.makeConstraints { make in
make.top.equalToSuperview().offset(11)
make.leading.equalTo(imageView.snp.trailing).offset(20)
// make.trailing.equalTo(score.snp.leading).offset(-10)
}

infoView.addSubview(specInfo)
Expand Down Expand Up @@ -348,6 +349,7 @@ class CheckNoteViewController: UIViewController {
make.top.equalTo(finishLabel.snp.bottom).offset(7)
make.centerX.equalTo(finishLabel.snp.centerX)
make.leading.equalTo(tasteButton.snp.trailing).offset(53)
make.trailing.equalTo(tasteView.snp.trailing).offset(-9)
}

tastingNoteView.addSubview(vectorView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ class RatingViewController: UIViewController {
@objc func completeButtonTapped() {
let nextVC = CheckNoteViewController()
postNewNoteAPI()
patchNoteAPI(noteId: 270)
patchNoteAPI(wineId: selectedWineId!)
nextVC.dataList = dataList
nextVC.selectedOptions = selectedOptions
nextVC.reviewString = reviewText.text ?? ""
Expand All @@ -429,7 +429,7 @@ class RatingViewController: UIViewController {
}

func postNewNoteAPI() {
let wineId = selectedWineId // 수정필요
let wineId = selectedWineId
let color = receivedColor
let satisfaction = Int(value)
let memo = reviewText.text ?? ""
Expand Down Expand Up @@ -487,11 +487,10 @@ class RatingViewController: UIViewController {
}
}

func patchNoteAPI(noteId: Int) {
let wineId = selectedWineId // 수정 필요
func patchNoteAPI(wineId: Int) {
let color = receivedColor
let satisfaction = Int(value)
let memo = reviewText.text ?? ""
let review = reviewText.text ?? ""
let scentAroma = selectedOptions["Aroma"] ?? []
let scentTaste = selectedOptions["Taste"] ?? []
let scentFinish = selectedOptions["Finish"] ?? []
Expand All @@ -518,8 +517,7 @@ class RatingViewController: UIViewController {
}

provider.request(.patchNote(
noteId: noteId,
wineId: wineId!,
wineId: wineId,
color: color,
sugarContent: sugarContent ?? 0,
acidity: acidity ?? 0,
Expand All @@ -530,15 +528,15 @@ class RatingViewController: UIViewController {
scentTaste: scentTaste,
scentFinish: scentFinish,
satisfaction: satisfaction,
memo: memo)) { result in
review: review)) { result in
switch result {
case .success(let response):
print("Note successfully patched with response: \(response)")
// 수정이 성공하면 다음 화면으로 이동
let nextVC = CheckNoteViewController()
nextVC.dataList = self.dataList
nextVC.selectedOptions = self.selectedOptions
nextVC.reviewString = memo
nextVC.reviewString = review
nextVC.value = self.value
self.navigationController?.pushViewController(nextVC, animated: true)
case .failure(let error):
Expand Down

0 comments on commit 46081b6

Please sign in to comment.