Skip to content

Commit

Permalink
[Fix] #256 - error 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongdung-eo committed Apr 28, 2024
1 parent f12c064 commit 8c17ec0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protocol MissionServiceProtocol {
typealias DefaultMissionService = BaseService<MissionAPI>

extension DefaultMissionService: MissionServiceProtocol {

func getDailyMission(date: String) -> AnyPublisher<DailyMissionData, Error> {
return requestWithCombine(MissionAPI.dailyMission(date: date))
}
Expand Down Expand Up @@ -47,9 +47,9 @@ protocol MissionServiceType {
func getRecentMission(completion: @escaping (RecentMissionData?) -> Void)
func deleteMission(id: Int, completion: @escaping (GeneralResponse<VoidType>?) -> Void)
func patchUpdateMissionStatus(id: Int, status: String, completion: @escaping (UpdateMissionStatus?) -> Void)
func postAnotherDay(id: Int, dates: [String], completion: @escaping (AddAnotherDay?) -> Void)
func postAddMission(request: AddMissionRequest, completion: @escaping(AddMissionsData?) -> Void)
func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(UpdateMissionData?) -> Void)
func postAnotherDay(id: Int, dates: [String], completion: @escaping (ErrorResponse?) -> Void)
func postAddMission(request: AddMissionRequest, completion: @escaping(ErrorResponse?) -> Void)
func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(ErrorResponse?) -> Void)
}

final class MissionService: MissionServiceType {
Expand Down Expand Up @@ -202,56 +202,92 @@ final class MissionService: MissionServiceType {

// MARK: - Post

func postAnotherDay(id: Int, dates: [String], completion: @escaping (AddAnotherDay?) -> Void) {
func postAnotherDay(id: Int, dates: [String], completion: @escaping (ErrorResponse?) -> Void) {
provider.request(.addAnotherDay(id: id, dates: dates)) { result in
switch result {
case .success(let response):
do {
let response = try response.map(AddAnotherDay?.self)
completion(response)
completion(nil)
} catch let err {
print(err.localizedDescription, 500)
completion(nil)
}
case .failure(let err):
print(err.localizedDescription)
completion(nil)
if let response = err.response {
do {
let errorResponse = try response.map(ErrorResponse.self)
print("💎 Server Error 💎: \(errorResponse.message)")
completion(errorResponse)
} catch {
print("💎 Mapping Error 💎: \(error.localizedDescription)")
completion(nil)
}
} else {
print("💎 Network Error 💎: \(err.localizedDescription)")
completion(nil)
}
}
}
}

func postAddMission(request: AddMissionRequest,
completion: @escaping(AddMissionsData?) -> Void) {
completion: @escaping(ErrorResponse?) -> Void) {
provider.request(.addMission(request: request)) { result in
switch result {
case .success(let response):
do {
let response = try response.map(AddMissionsData?.self)
completion(response)
completion(nil)
} catch let err {
print(err.localizedDescription, 500)
completion(nil)
}
case .failure(let err):
print(err.localizedDescription)
completion(nil)
if let response = err.response {
do {
let errorResponse = try response.map(ErrorResponse.self)
print("💎 Server Error 💎: \(errorResponse.message)")
completion(errorResponse)
} catch {
print("💎 Mapping Error 💎: \(error.localizedDescription)")
completion(nil)
}
} else {
print("💎 Network Error 💎: \(err.localizedDescription)")
completion(nil)
}
}
}
}

// MARK: PUT

func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(UpdateMissionData?) -> Void) {
func putUpdateMission(request: UpdateMissionRequest, completion: @escaping(ErrorResponse?) -> Void) {
provider.request(.updateMission(request: request)) { result in
switch result {
case .success(let response):
do {
let response = try response.map(UpdateMissionData?.self)
completion(response)
completion(nil)
} catch let err {
print(err.localizedDescription, 500)
}
case .failure(let err):
print(err.localizedDescription)
completion(nil)
if let response = err.response {
do {
let errorResponse = try response.map(ErrorResponse.self)
print("💎 Server Error 💎: \(errorResponse.message)")
completion(errorResponse)
} catch {
print("💎 Mapping Error 💎: \(error.localizedDescription)")
completion(nil)
}
} else {
print("💎 Network Error 💎: \(err.localizedDescription)")
completion(nil)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,49 +280,47 @@ extension AddMissionViewController {
private func requestPostAddMission(title: String, situation: String,
actions: [String]?, goal: String?, dates: [String]?) {
let request = AddMissionRequest(title: title, situation: situation, actions: actions, goal: goal, dates: dates ?? [""])
MissionService.shared.postAddMission(request: request) { response in
guard let response = response else { return }
switch response.status {
case 200..<300:
AmplitudeAnalyticsService.shared.send(
event: AnalyticsEvent.CreateMission.completeCreateMission(
date: dates ?? [],
goal: goal ?? "",
title: title,
situation: situation,
action: actions ?? []
)
)
self.coordinator?.dismiss()
default:
let toastMessage = self.htmlToString(response.message ?? "")?.string ?? ""
MissionService.shared.postAddMission(request: request) { err in
if let err = err {
let toastMessage = self.htmlToString(err.message)?.string ?? ""
self.checkToastMessage(toastMessage)
self.showToast(message: toastMessage, controller: self)
return
}

AmplitudeAnalyticsService.shared.send(
event: AnalyticsEvent.CreateMission.completeCreateMission(
date: dates ?? [],
goal: goal ?? "",
title: title,
situation: situation,
action: actions ?? []
)
)
self.coordinator?.dismiss()

}
}

private func requestPutUpdateMission(id: Int, title: String, situation: String, actions: [String]?, goal: String?) {
let request = UpdateMissionRequest(id: id, title: title, situation: situation, actions: actions, goal: goal)
MissionService.shared.putUpdateMission(request: request) { response in
guard let response = response else { return }
print(response.status)
switch response.status {
case 200..<300:
AmplitudeAnalyticsService.shared.send(
event: AnalyticsEvent.UpdateMission.completeUpdateMission(
date: self.dateList,
goal: self.nottodoInfoList[4],
title: self.nottodoInfoList[1],
situation: self.nottodoInfoList[2],
action: self.nottodoInfoList[3])
)
self.coordinator?.dismiss()
default:
let toastMessage = self.htmlToString(response.message ?? "")?.string ?? ""
MissionService.shared.putUpdateMission(request: request) { err in
if let err = err {
let toastMessage = self.htmlToString(err.message)?.string ?? ""
self.checkToastMessage(toastMessage)
self.showToast(message: toastMessage, controller: self)
return
}

AmplitudeAnalyticsService.shared.send(
event: AnalyticsEvent.UpdateMission.completeUpdateMission(
date: self.dateList,
goal: self.nottodoInfoList[4],
title: self.nottodoInfoList[1],
situation: self.nottodoInfoList[2],
action: self.nottodoInfoList[3])
)
self.coordinator?.dismiss()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,25 @@ extension DetailCalendarViewController: FSCalendarDelegate, FSCalendarDataSource
extension DetailCalendarViewController {

private func requestAddAnotherDay(id: Int, dates: [String]) {
MissionService.shared.postAnotherDay(id: id, dates: dates) { response in
guard response != nil else { return }
guard let statusCode = response?.status else { return }
switch statusCode {
case 200..<204:
self.setUI()
self.coordinator?.dismiss()
AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.closeAnotherDayModal)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy.MM.dd"
if let earliestDate = dateFormatter.date(from: self.anotherDate.min() ?? "") {
let earliestDateString = dateFormatter.string(from: earliestDate)
self.movedateClosure?(earliestDateString)
}
default:
self.showToast(message: self.htmlToString(response?.message ?? "")?.string ?? "", controller: self)
MissionService.shared.postAnotherDay(id: id, dates: dates) { err in
if let err = err {
self.showToast(message: self.htmlToString(err.message)?.string ?? "", controller: self)
AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.appearMaxedIssueMessage)
return
}

self.setUI()
self.coordinator?.dismiss()
AmplitudeAnalyticsService.shared.send(event: AnalyticsEvent.SelectDate.closeAnotherDayModal)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy.MM.dd"
if let earliestDate = dateFormatter.date(from: self.anotherDate.min() ?? "") {
let earliestDateString = dateFormatter.string(from: earliestDate)
self.movedateClosure?(earliestDateString)
}
}
}

func requestParticualrDatesAPI(id: Int) {
MissionService.shared.particularMissionDates(id: id) { [weak self] response in
guard let dates = response.data else { return }
Expand Down

0 comments on commit 8c17ec0

Please sign in to comment.