Skip to content

Commit

Permalink
Merge pull request #87 from lee-yeonwoo/#81CourseUploadAPI
Browse files Browse the repository at this point in the history
[Feat] #81 - 코스업로드하기 API 연결 완료
  • Loading branch information
lee-yeonwoo authored Jan 12, 2023
2 parents e6c08b0 + 884cee7 commit 2b764db
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
DA20D849296697B400F1581F /* CourseUploadVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA20D848296697B400F1581F /* CourseUploadVC.swift */; };
DA20D84E2966A9B300F1581F /* CourseSearchVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA20D84D2966A9B300F1581F /* CourseSearchVC.swift */; };
DA97A02F296DC3300086760E /* CourseSearchingRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA97A02E296DC3300086760E /* CourseSearchingRouter.swift */; };
DA97A033296E65D80086760E /* CourseUploadingRequestDto.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA97A032296E65D80086760E /* CourseUploadingRequestDto.swift */; };
DA97A035296E685C0086760E /* CourseUploadingRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA97A034296E685C0086760E /* CourseUploadingRouter.swift */; };
DAD5A3D8296C6D9600C8166B /* AdImageCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5A3D7296C6D9600C8166B /* AdImageCollectionViewCell.swift */; };
DAD5A3DA296C6DA500C8166B /* TitleCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5A3D9296C6DA500C8166B /* TitleCollectionViewCell.swift */; };
DAD5A3DC296C6DB800C8166B /* MapCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5A3DB296C6DB800C8166B /* MapCollectionViewCell.swift */; };
Expand Down Expand Up @@ -292,6 +294,8 @@
DA20D848296697B400F1581F /* CourseUploadVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseUploadVC.swift; sourceTree = "<group>"; };
DA20D84D2966A9B300F1581F /* CourseSearchVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseSearchVC.swift; sourceTree = "<group>"; };
DA97A02E296DC3300086760E /* CourseSearchingRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseSearchingRouter.swift; sourceTree = "<group>"; };
DA97A032296E65D80086760E /* CourseUploadingRequestDto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseUploadingRequestDto.swift; sourceTree = "<group>"; };
DA97A034296E685C0086760E /* CourseUploadingRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseUploadingRouter.swift; sourceTree = "<group>"; };
DAD5A3D7296C6D9600C8166B /* AdImageCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdImageCollectionViewCell.swift; sourceTree = "<group>"; };
DAD5A3D9296C6DA500C8166B /* TitleCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TitleCollectionViewCell.swift; sourceTree = "<group>"; };
DAD5A3DB296C6DB800C8166B /* MapCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapCollectionViewCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -744,6 +748,7 @@
children = (
DAD5A3E3296D526D00C8166B /* CourseDiscoveryRouter.swift */,
DA97A02E296DC3300086760E /* CourseSearchingRouter.swift */,
DA97A034296E685C0086760E /* CourseUploadingRouter.swift */,
);
path = CourseDiscoveryRouter;
sourceTree = "<group>";
Expand Down Expand Up @@ -1148,6 +1153,7 @@
DA97A029296DC2020086760E /* RequestDto */ = {
isa = PBXGroup;
children = (
DA97A032296E65D80086760E /* CourseUploadingRequestDto.swift */,
);
path = RequestDto;
sourceTree = "<group>";
Expand Down Expand Up @@ -1307,6 +1313,7 @@
CE9291272965D0ED0010959C /* StatsInfoView.swift in Sources */,
CEC2A68729629B9B00160BF7 /* SignInVC.swift in Sources */,
CE665602295D918000C64E12 /* JsonCoder.swift in Sources */,
DA97A033296E65D80086760E /* CourseUploadingRequestDto.swift in Sources */,
CE4545CD295D7AF4003201E1 /* TaBarController.swift in Sources */,
A3305A97296EF58C000B1A10 /* GoalRewardInfoDto.swift in Sources */,
CE6655F4295D898400C64E12 /* UIViewController+.swift in Sources */,
Expand Down Expand Up @@ -1416,6 +1423,7 @@
A3BC2F382963CE3700198261 /* ActivityRecordInfoModel.swift in Sources */,
CE6655E6295D887F00C64E12 /* UIStackView+.swift in Sources */,
A3BC2F34296303A600198261 /* GoalRewardInfoCVC.swift in Sources */,
DA97A035296E685C0086760E /* CourseUploadingRouter.swift in Sources */,
CEB8416E2962C45300BF8080 /* LocationSearchResultTVC.swift in Sources */,
CE6655CA295D84DD00C64E12 /* UserDefaultKeyList.swift in Sources */,
CE6655F2295D894D00C64E12 /* UIView+.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extension Encodable {
as? [String: Any] else {
throw NSError()
}

return dictionary
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CourseUploadingRequestDto.swift
// Runnect-iOS
//
// Created by YEONOO on 2023/01/11.
//

import Foundation

// MARK: - CourseUploadingRequestDto

struct CourseUploadingRequestDto: Codable {
let courseId: Int
let title, description: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// CourseUploadingRouter.swift
// Runnect-iOS
//
// Created by YEONOO on 2023/01/11.
//

import Foundation

import Moya

enum CourseUploadingRouter {
case courseUploadingData(param: CourseUploadingRequestDto)
}

extension CourseUploadingRouter: TargetType {
var baseURL: URL {
guard let url = URL(string: Config.baseURL) else {
fatalError("baseURL could not be configured")
}

return url
}

var path: String {
switch self {
case .courseUploadingData:
return "/public-course"
}
}

var method: Moya.Method {
switch self {
case .courseUploadingData:
return .post
}
}

var task: Moya.Task {
switch self {
case .courseUploadingData(param: let param):
return .requestParameters(parameters: try! param.asParameter(), encoding: JSONEncoding.default)
}
}

var headers: [String: String]? {
switch self {
case .courseUploadingData:
return Config.headerWithDeviceId
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ final class CourseSearchVC: UIViewController {
// MARK: - Properties

private let CourseSearchingRouter = MoyaProvider<CourseSearchingRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)])
plugins: [NetworkLoggerPlugin(verbose: true)]
)

private var courseList = [PublicCourse]()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import UIKit

import SnapKit
import Then
import Moya

class CourseUploadVC: UIViewController {

// MARK: - Properties

// private var runningModel: RunningModel?
private let courseUploadingProvider = MoyaProvider<CourseUploadingRouter>(
plugins: [NetworkLoggerPlugin(verbose: true)]
)
private var courseModel: Course?
private let courseTitleMaxLength = 20

Expand Down Expand Up @@ -88,8 +92,8 @@ extension CourseUploadVC {
}

private func setAddTarget() {
self.uploadButton.addTarget(self, action: #selector(pushToCourseDiscoveryVC), for: .touchUpInside)
self.courseTitleTextField.addTarget(self, action: #selector(textFieldTextDidChange), for: .editingChanged)
self.uploadButton.addTarget(self, action: #selector(uploadButtonDidTap), for: .touchUpInside)
}

// 키보드가 올라오면 scrollView 위치 조정
Expand Down Expand Up @@ -127,19 +131,12 @@ extension CourseUploadVC {
object: nil)
}
}

// MARK: - @objc Function

extension CourseUploadVC {

@objc private func pushToCourseDiscoveryVC() {
let nextVC = CourseDiscoveryVC()
self.navigationController?.pushViewController(nextVC, animated: true)
}

@objc private func textFieldTextDidChange() {
guard let text = courseTitleTextField.text else { return }

if text.count > courseTitleMaxLength {
let index = text.index(text.startIndex, offsetBy: courseTitleMaxLength)
let newString = text[text.startIndex..<index]
Expand Down Expand Up @@ -182,6 +179,10 @@ extension CourseUploadVC {
scrollView.contentInset = contentInset
scrollView.scrollIndicatorInsets = contentInset
}

@objc func uploadButtonDidTap() {
self.uploadCourse()
}
}

// MARK: - naviVar Layout
Expand Down Expand Up @@ -314,3 +315,34 @@ extension CourseUploadVC: UITextViewDelegate {
}
}
}

// MARK: - Network

extension CourseUploadVC {
private func uploadCourse() {
guard let courseId = courseModel?.id else { return }
guard let titletext = courseTitleTextField.text else { return }
guard let descriptiontext = activityTextView.text else { return }
let requsetDto = CourseUploadingRequestDto(courseId: courseId, title: titletext, description: descriptiontext)

LoadingIndicator.showLoading()
courseUploadingProvider.request(.courseUploadingData(param: requsetDto)) { [weak self] response in
LoadingIndicator.hideLoading()
guard let self = self else { return }
switch response {
case .success(let result):
let status = result.statusCode
if 200..<300 ~= status {
self.navigationController?.popToRootViewController(animated: true)
}
if status >= 400 {
print("400 error")
self.showNetworkFailureToast()
}
case .failure(let error):
print(error.localizedDescription)
self.showNetworkFailureToast()
}
}
}
}

0 comments on commit 2b764db

Please sign in to comment.