Skip to content

Commit

Permalink
Merge pull request #71 from KCY-Fit-a-Pet/feat/70
Browse files Browse the repository at this point in the history
📮 pet 정보 리스트 api 연동
  • Loading branch information
psychology50 authored Jan 20, 2024
2 parents fa42027 + f0052be commit 4f342d6
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 109 deletions.
6 changes: 5 additions & 1 deletion fit-a-pet-client/fit-a-pet-client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
4AA9999B2A8485A600E495FA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4AA9999A2A8485A600E495FA /* Assets.xcassets */; };
4AA9999E2A8485A600E495FA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4AA9999C2A8485A600E495FA /* LaunchScreen.storyboard */; };
4AB6178E2B1CFAF300CED98E /* FindIdCheckVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AB6178D2B1CFAF300CED98E /* FindIdCheckVC.swift */; };
4ABE53002B5AD92F000EA41D /* PetDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ABE52FF2B5AD92F000EA41D /* PetDataManager.swift */; };
4AC579832B2DFD4B00735252 /* StructData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC579822B2DFD4B00735252 /* StructData.swift */; };
4AC579852B2DFF7500735252 /* CustomPopupView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC579842B2DFF7500735252 /* CustomPopupView.swift */; };
4AC579872B2E004400735252 /* CustomPopupVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC579862B2E004400735252 /* CustomPopupVC.swift */; };
Expand Down Expand Up @@ -136,6 +137,7 @@
4AA9999D2A8485A600E495FA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
4AA9999F2A8485A600E495FA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4AB6178D2B1CFAF300CED98E /* FindIdCheckVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindIdCheckVC.swift; sourceTree = "<group>"; };
4ABE52FF2B5AD92F000EA41D /* PetDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PetDataManager.swift; sourceTree = "<group>"; };
4AC579822B2DFD4B00735252 /* StructData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StructData.swift; sourceTree = "<group>"; };
4AC579842B2DFF7500735252 /* CustomPopupView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomPopupView.swift; sourceTree = "<group>"; };
4AC579862B2E004400735252 /* CustomPopupVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomPopupVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -282,9 +284,9 @@
4A40561D2B52FE3500A95891 /* PetCareCell */,
4A3D7BFB2B4F1F2000E3BBF8 /* PetCareView */,
4A9088752AC1681B000C7878 /* PetCollectViewCell.swift */,
4AF0C8552B25A93500A45EEE /* AlarmTableViewCell.swift */,
4AD089692AFBFD9400E79E52 /* CheckCareCollectionViewCell.swift */,
4AF0C8532B24E1A800A45EEE /* MyInfoTableViewCell.swift */,
4AF0C8552B25A93500A45EEE /* AlarmTableViewCell.swift */,
4AC579842B2DFF7500735252 /* CustomPopupView.swift */,
);
path = View;
Expand Down Expand Up @@ -321,6 +323,7 @@
4A0750D72B0D130F00281A4C /* PetRegistrationManager.swift */,
4A88D6162B53ABD2003A2D0F /* PetCareRegistrationManager.swift */,
4AC579822B2DFD4B00735252 /* StructData.swift */,
4ABE52FF2B5AD92F000EA41D /* PetDataManager.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -583,6 +586,7 @@
4AB6178E2B1CFAF300CED98E /* FindIdCheckVC.swift in Sources */,
4A0750D82B0D130F00281A4C /* PetRegistrationManager.swift in Sources */,
4AA999922A8485A500E495FA /* AppDelegate.swift in Sources */,
4ABE53002B5AD92F000EA41D /* PetDataManager.swift in Sources */,
4A88D6172B53ABD2003A2D0F /* PetCareRegistrationManager.swift in Sources */,
4ACDD4A92ADAFFD100CD8F1A /* InputPetBirthVC.swift in Sources */,
4A15F2142AA8B4A50098F2F7 /* InputPwVC.swift in Sources */,
Expand Down
Binary file not shown.
40 changes: 35 additions & 5 deletions fit-a-pet-client/fit-a-pet-client/API/AuthorizationAlamofire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ class AuthorizationAlamofire: TokenHandling {
}
}

func createCare(combinedData: [String: Any], completion: @escaping (Result<Data?, Error>) -> Void) {
func createCare(combinedData: [String: Any], petId: Int, completion: @escaping (Result<Data?, Error>) -> Void) {
os_log("AuthorizationAlamofire - createCare() called userInput: %@", log: .default, type: .info, combinedData)

self.session.request(MySearchRouter.createCare(combinedData: combinedData))
self.session.request(MySearchRouter.createCare(combinedData: combinedData, petId: petId))
.response { response in
switch response.result {
case .success(let data):
Expand All @@ -143,10 +143,10 @@ class AuthorizationAlamofire: TokenHandling {
}


func checkCareCategory(completion: @escaping (Result<Data?, Error>) -> Void) {
func checkCareCategory(_ petId: Int, completion: @escaping (Result<Data?, Error>) -> Void) {
os_log("AuthorizationAlamofire - checkCareCategory() called", log: .default, type: .info)

self.session.request(MySearchRouter.checkCareCategory)
self.session.request(MySearchRouter.checkCareCategory(petId: petId))
.response { response in
switch response.result {
case .success(let data):
Expand All @@ -173,7 +173,7 @@ class AuthorizationAlamofire: TokenHandling {

func careCategoryCheck(_ categoryName: String, _ pets: [Int] ,completion: @escaping (Result<Data?, Error>) -> Void) {
os_log("AuthorizationAlamofire - careCategoryCheck() called ,, %@ ,, %@ ", log: .default, type: .info, categoryName, pets)

self.session.request(MySearchRouter.careCategoryCheck(categoryName: categoryName, pets: pets))
.response { response in
switch response.result {
Expand All @@ -184,5 +184,35 @@ class AuthorizationAlamofire: TokenHandling {
}
}
}

func userPetInfoList(completion: @escaping (Result<Data?, Error>) -> Void) {
os_log("AuthorizationAlamofire - userPetInfoList() called", log: .default, type: .info)

self.session.request(MySearchRouter.userPetInfoList)
.response { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}

func userPetCareInfoList(_ petId: Int ,completion: @escaping (Result<Data?, Error>) -> Void) {
os_log("AuthorizationAlamofire - userPetCareInfoList() called", log: .default, type: .info)

self.session.request(MySearchRouter.userPetCareInfoList(petId: petId))
.response { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
}


}

40 changes: 26 additions & 14 deletions fit-a-pet-client/fit-a-pet-client/API/MySearchRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ enum MySearchRouter: URLRequestConvertible {
case findId(phone: String, code: String)
case findPw(phone: String, newPassword: String, code: String)
case existId(uid: String)
case userProfileInfo, oauthLogin, oauthSendSms, refresh, checkCareCategory ,userPetsList
case userProfileInfo, oauthLogin, oauthSendSms, refresh ,userPetsList, userPetInfoList
case userNotifyType(type: String)
case editUserPw(type: String, prePassword: String, newPassword: String)
case editUserName(type: String, name: String)
case oauthCheckSms(code: String)
case oauthRegistUser(name: String, uid: String)
case createCare(combinedData: [String:Any])
case createCare(combinedData: [String:Any], petId: Int)
case checkCareCategory(petId: Int)
case careCategoryCheck(categoryName: String, pets: [Int])
case userPetCareInfoList(petId: Int)

var baseURL: URL {
switch self {
Expand All @@ -41,7 +43,7 @@ enum MySearchRouter: URLRequestConvertible {
switch self {
case .sendSms, .checkSms, .login, .regist, .presignedurl, .registPet,.sendAuthSms, .checkAuthSms, .findId, .findPw, .oauthLogin, .oauthSendSms, .oauthCheckSms, .oauthRegistUser, .createCare, .careCategoryCheck:
return .post
case .existId, .userProfileInfo, .userNotifyType, .refresh ,.checkCareCategory, .userPetsList:
case .existId, .userProfileInfo, .userNotifyType, .refresh ,.checkCareCategory, .userPetsList, .userPetInfoList, .userPetCareInfoList:
return .get
case .uploadImage, .editUserPw, .editUserName:
return .put
Expand Down Expand Up @@ -80,14 +82,12 @@ enum MySearchRouter: URLRequestConvertible {
return "v2/accounts/\(UserDefaults.standard.string(forKey: "id")!)"
case .userNotifyType:
return "v2/accounts/\(UserDefaults.standard.string(forKey: "id")!)/notify"
case .createCare:
return "v2/users/\(UserDefaults.standard.string(forKey: "id")!)/pets/1/cares" //TODO: 임시 pet id 값
case .checkCareCategory:
return "v2/users/\(UserDefaults.standard.string(forKey: "id")!)/pets/1/cares/categories" //TODO: 임시 pet id 값
case .userPetsList:
return "v2/users/\(UserDefaults.standard.string(forKey: "id")!)/pets/summary"
case .careCategoryCheck:
return "v2/users/\(UserDefaults.standard.string(forKey: "id")!)/pets/categories-check"
case .userPetInfoList, .userPetCareInfoList, .createCare, .checkCareCategory:
return "v2/users/\(UserDefaults.standard.string(forKey: "id")!)/pets"
}
}

Expand Down Expand Up @@ -120,21 +120,21 @@ enum MySearchRouter: URLRequestConvertible {
return ["type": type, "prePassword": prePassword, "newPassword": newPassword]
case let .editUserName(type, name):
return ["type": type, "name": name]
case .uploadImage(_), .userProfileInfo, .oauthLogin, .oauthSendSms, .refresh, .checkCareCategory, .userPetsList, .createCare:
return [:]
case let .oauthCheckSms(code):
return ["code": code]
case let .oauthRegistUser(name, uid):
return ["name": name, "uid": uid]
case let .careCategoryCheck(categoryName, pets):
return ["categoryName": categoryName, "pets": pets]


case .uploadImage(_), .userProfileInfo, .oauthLogin, .oauthSendSms, .refresh, .checkCareCategory, .userPetsList, .createCare, .userPetInfoList:
return [:]
case let .userPetCareInfoList(userId):
return["userId": userId]
}
}

func asURLRequest() throws -> URLRequest {
let url = baseURL.appendingPathComponent(path)
var url = baseURL.appendingPathComponent(path)
var request: URLRequest

switch self {
Expand All @@ -148,7 +148,8 @@ enum MySearchRouter: URLRequestConvertible {
// case .regist:
// request = createURLRequestWithBody(url: url)

case .createCare(let combinedData):
case .createCare(let combinedData, let petId):
url = url.appendingPathComponent("/\(petId)/cares")
request = URLRequest(url: url)
request.httpMethod = method.rawValue
request = try JSONEncoding.default.encode(request, withJSONObject: combinedData)
Expand Down Expand Up @@ -276,13 +277,24 @@ enum MySearchRouter: URLRequestConvertible {

request = createURLRequestWithBodyAndQuery(url: url, bodyParameters: bodyParameters, queryParameters: queryParameters)

case .checkCareCategory, .userPetsList:
case .userPetsList, .userPetInfoList:
request = URLRequest(url: url)
request.httpMethod = method.rawValue

case .checkCareCategory(let petId):
url = url.appendingPathComponent("/\(petId)/cares/categories")
request = URLRequest(url: url)
request.httpMethod = method.rawValue

case .userPetCareInfoList(let petId):
url = url.appendingPathComponent("/\(petId)/cares")
request = URLRequest(url: url)
request.httpMethod = method.rawValue

default:
request = createURLRequestWithBody(url: url)
}

return request
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ extension CustomEditNavigationBar{

print("combinedData: \(combinedData)")

AuthorizationAlamofire.shared.createCare(combinedData: combinedData) { result in
AuthorizationAlamofire.shared.createCare(combinedData: combinedData, petId: SelectedPetId.petId) { result in
switch result {
case .success(let data):
if let responseData = data,
Expand Down
4 changes: 4 additions & 0 deletions fit-a-pet-client/fit-a-pet-client/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ enum ViewState {
case daysTableView
static var stateNum = 0
}

enum SelectedPetId{
static var petId = 0
}
80 changes: 80 additions & 0 deletions fit-a-pet-client/fit-a-pet-client/Utils/PetDataManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Foundation

struct PetInfoResponse: Codable {
let status: String
let data: PetData?
}

struct PetData: Codable {
let pets: [Pet]?
}

struct Pet: Codable {
let id: Int
let petName: String
let gender: String
let petProfileImage: String
let feed: String
let age: Int
}

struct CareInfoResponse: Codable {
let status: String
let data: CareInfo?
}

struct CareInfo: Codable {
var careCategories: [CareCategory]?

}

struct CareCategory: Codable {
let careCategoryId: Int
let categoryName: String
var cares: [Care]
}

struct Care: Codable {
let careId: Int
let careDateId: Int
let careName: String
let careDate: String
let isClear: Bool
}


struct PetDataManager {
static var pets: [Pet] = []
static var careCategories: [CareCategory] = []

static func updatePets(with data: Data) {
do {
let decoder = JSONDecoder()
let petInfoResponse = try decoder.decode(PetInfoResponse.self, from: data)

if let newPets = petInfoResponse.data?.pets {
pets = newPets

print("petsList: \(pets)")
}
} catch {
print("Error updating pet data: \(error)")
}
}


static func updateCareInfo(with data: Data) {
do {
let decoder = JSONDecoder()
let careInfoResponse = try decoder.decode(CareInfoResponse.self, from: data)

if let newCareCategories = careInfoResponse.data?.careCategories {
careCategories = newCareCategories
print("careCategories: \(careCategories)")
}
} catch {
print("Error updating care info: \(error)")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@ class PetCareListCollectionViewCell: UICollectionViewCell {
}

private func setupSubviews() {
addSubview(careName)
contentView.addSubview(careName)
contentView.layer.cornerRadius = 8
contentView.layer.masksToBounds = true
contentView.backgroundColor = UIColor(named: "Gray1")

careName.text = "아침"
careName.font = .systemFont(ofSize: 14)
careName.textColor = UIColor(named: "Gray3")
careName.backgroundColor = UIColor(named: "Gray1")

careName.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}

override var isSelected: Bool {
didSet {
if isSelected {
careName.textColor = UIColor(named: "PrimaryColor")
careName.backgroundColor = UIColor(named: "Secondary")
}else{
careName.textColor = UIColor(named: "Gray3")
careName.backgroundColor = UIColor(named: "Gray1")
}
}
}
// override var isSelected: Bool {
// didSet {
// if isSelected {
// careName.textColor = UIColor(named: "PrimaryColor")
// careName.backgroundColor = UIColor(named: "Secondary")
//
// }else{
// careName.textColor = UIColor(named: "Gray3")
// careName.backgroundColor = UIColor(named: "Gray1")
// }
// }
// }

func configure(_ newText: String) {
careName.text = newText
Expand Down
Loading

0 comments on commit 4f342d6

Please sign in to comment.