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: 취향 조회 API 구현 (#385) #392

Merged
merged 6 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NADA-iOS-forRelease.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
F8C83FC9272FA3190009DF0D /* GroupAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C83FC8272FA3190009DF0D /* GroupAPI.swift */; };
F8C83FCB272FA32C0009DF0D /* GroupService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C83FCA272FA32C0009DF0D /* GroupService.swift */; };
F8D74DD8276C7FB60071E5FC /* UIImageView+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8D74DD7276C7FB60071E5FC /* UIImageView+Extension.swift */; };
F8D92E0A29D5324E002ACC73 /* Taste.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8D92E0929D5324E002ACC73 /* Taste.swift */; };
F8F5D0AA270800FD00D99D2E /* Xib.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8F5D0A9270800FD00D99D2E /* Xib.swift */; };
F8FC438626C01CDD0033E151 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FC438526C01CDD0033E151 /* AppDelegate.swift */; };
F8FC438826C01CDD0033E151 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FC438726C01CDD0033E151 /* SceneDelegate.swift */; };
Expand Down Expand Up @@ -391,6 +392,7 @@
F8C83FC8272FA3190009DF0D /* GroupAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupAPI.swift; sourceTree = "<group>"; };
F8C83FCA272FA32C0009DF0D /* GroupService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupService.swift; sourceTree = "<group>"; };
F8D74DD7276C7FB60071E5FC /* UIImageView+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+Extension.swift"; sourceTree = "<group>"; };
F8D92E0929D5324E002ACC73 /* Taste.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Taste.swift; sourceTree = "<group>"; };
F8F5D0A9270800FD00D99D2E /* Xib.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Xib.swift; sourceTree = "<group>"; };
F8FC438226C01CDD0033E151 /* NADA-iOS-forRelease.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "NADA-iOS-forRelease.app"; sourceTree = BUILT_PRODUCTS_DIR; };
F8FC438526C01CDD0033E151 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -884,6 +886,7 @@
3981148D273BEBB300E28630 /* CardListEditRequest.swift */,
3981148F273BEBCE00E28630 /* CardListRequest.swift */,
39DC06992778BEFB00C8ECCC /* CardListLookUp.swift */,
F8D92E0929D5324E002ACC73 /* Taste.swift */,
);
path = Card;
sourceTree = "<group>";
Expand Down Expand Up @@ -1537,6 +1540,7 @@
3958F239270FFBBF00B100B2 /* GroupViewController.swift in Sources */,
3909242F26FA15E800236C51 /* UIView+Extension.swift in Sources */,
39523E09270184A700536900 /* CardListViewController.swift in Sources */,
F8D92E0A29D5324E002ACC73 /* Taste.swift in Sources */,
F85711A5274A6B2200F59F0B /* CardCreationPreviewViewController.swift in Sources */,
39DC069A2778BEFB00C8ECCC /* CardListLookUp.swift in Sources */,
777FF89B27359B7800BF69D3 /* Groups.swift in Sources */,
Expand Down
24 changes: 24 additions & 0 deletions NADA-iOS-forRelease/Sources/NetworkModel/Card/Taste.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Taste.swift
// NADA-iOS-forRelease
//
// Created by kimhyungyu on 2023/03/30.
//

import Foundation

struct Taste: Codable {
let careType: CardType
let tasteInfos: [TasteInfo]
}

enum CardType: String, Codable {
case basic = "BASIC"
case company = "COMPANY"
case fan = "FAN"
}

struct TasteInfo: Codable {
let sortOrder: Int
let tasteName: String
}
34 changes: 34 additions & 0 deletions NADA-iOS-forRelease/Sources/NetworkService/Card/CardAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,42 @@ public class CardAPI {
}
}

func tasteFetch(cardType: CardType, completion: @escaping (NetworkResult<Any>) -> Void) {
cardProvider.request(.tasteFetch(cardType: cardType)) { result in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeTasteFetchStatus(by: statusCode, data)

completion(networkResult)
case .failure(let err):
print(err)
}
}
}

// MARK: - JudgeStatus methods

private func judgeTasteFetchStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<Taste>.self, from: data)
else {
return .pathErr
}

switch statusCode {
case 200:
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.error?.message ?? "error message")
case 500:
return .serverErr
default:
return .networkFail
}
}

private func judgeCardDetailFetchStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum CardService {
case cardListEdit(request: CardListEditRequest)
case cardDelete(cardID: String)
case imageUpload(image: UIImage)
case tasteFetch(cardType: CardType)
}

extension CardService: TargetType {
Expand All @@ -37,12 +38,14 @@ extension CardService: TargetType {
return "/card/\(cardID)"
case .imageUpload:
return "/image"
case .tasteFetch(let cardType):
return "/card/\(cardType)/taste"
}
}

var method: Moya.Method {
switch self {
case .cardDetailFetch, .cardListFetch:
case .cardDetailFetch, .cardListFetch, .tasteFetch:
return .get
case .cardCreation, .imageUpload:
return .post
Expand All @@ -59,7 +62,7 @@ extension CardService: TargetType {

var task: Task {
switch self {
case .cardDetailFetch, .cardDelete:
case .cardDetailFetch, .cardDelete, .tasteFetch:
return .requestPlain
case .cardCreation(let request, let image):

Expand Down Expand Up @@ -120,7 +123,7 @@ extension CardService: TargetType {

var headers: [String: String]? {
switch self {
case .cardDetailFetch, .cardListFetch, .cardDelete:
case .cardDetailFetch, .cardListFetch, .cardDelete, .tasteFetch:
return Const.Header.bearerHeader()
case .cardListEdit:
return Const.Header.basicHeader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class CardCreationViewController: UIViewController {
private var mbtiText: String?
private var birthText: String?
private var newImage: UIImage?
private var cardType: CardType = .basic
private var tasteInfo: [TasteInfo]?

// MARK: - @IBOutlet Properties

Expand All @@ -61,6 +63,7 @@ class CardCreationViewController: UIViewController {
registerCell()
setTextLabelGesture()
setNotification()
tasteFetchWithAPI()

// FIXME: 서버통신 테스트 중. 추후 호출 위치 변경.
// cardDetailFetchWithAPI(cardID: "cardA")
Expand Down Expand Up @@ -363,3 +366,27 @@ extension CardCreationViewController: UIImagePickerControllerDelegate, UINavigat
dismiss(animated: true, completion: nil)
}
}

// MARK: - API methods

extension CardCreationViewController {
func tasteFetchWithAPI() {
CardAPI.shared.tasteFetch(cardType: cardType) { response in
switch response {
case .success(let data):
print("cardCreationWithAPI - success")
if let tastes = data as? Taste {
self.tasteInfo = tastes.tasteInfos
}
case .requestErr(let message):
print("tasteFetchWithAPI - requestErr: \(message)")
case .pathErr:
print("tasteFetchWithAPI - pathErr")
case .serverErr:
print("tasteFetchWithAPI - serverErr")
case .networkFail:
print("tasteFetchWithAPI - networkFail")
}
}
}
}