Commit a7985354 authored by domenicw's avatar domenicw
Browse files

More work on event signup

parent 38c2efc0
...@@ -30,6 +30,17 @@ public class EventsNavigator: Navigator { ...@@ -30,6 +30,17 @@ public class EventsNavigator: Navigator {
events.delegate = self events.delegate = self
self.refreshData(events) self.refreshData(events)
self.networkManager.getEventSignups { (events, error) in
guard let event = events?.first else {
return
}
self.networkManager.signupTo(event, { (success, error) in
debugPrint(success)
debugPrint(error)
})
}
} }
// MARK: - Navigation // MARK: - Navigation
......
...@@ -74,13 +74,9 @@ extension OnboardingNavigator: LoginViewControllerDelegate { ...@@ -74,13 +74,9 @@ extension OnboardingNavigator: LoginViewControllerDelegate {
let userManager = NetworkManager<AMIVApiUser>() let userManager = NetworkManager<AMIVApiUser>()
userManager.getUserInfo({ (user, error) in userManager.getUserInfo({ (user, error) in
guard error == nil, let user = user else { guard error == nil, let user = user else {
debugPrint(error)
return return
} }
let _ = user.saveLocal()
if user.saveLocal() {
debugPrint(User.loadLocal())
}
}) })
DispatchQueue.main.async { DispatchQueue.main.async {
......
...@@ -12,6 +12,7 @@ public enum AMIVApiEvents { ...@@ -12,6 +12,7 @@ public enum AMIVApiEvents {
case events case events
case eventSignups case eventSignups
case signup(_ eventSignup: EventSignup)
case media(_ path: String) case media(_ path: String)
} }
...@@ -22,7 +23,7 @@ extension AMIVApiEvents: EndPointType { ...@@ -22,7 +23,7 @@ extension AMIVApiEvents: EndPointType {
switch self { switch self {
case .events: case .events:
return "/events" return "/events"
case .eventSignups: case .eventSignups, .signup:
return "/eventsignups" return "/eventsignups"
case .media(let path): case .media(let path):
return path return path
...@@ -33,6 +34,8 @@ extension AMIVApiEvents: EndPointType { ...@@ -33,6 +34,8 @@ extension AMIVApiEvents: EndPointType {
switch self { switch self {
case .events, .media, .eventSignups: case .events, .media, .eventSignups:
return .get return .get
case .signup:
return .post
} }
} }
...@@ -40,12 +43,14 @@ extension AMIVApiEvents: EndPointType { ...@@ -40,12 +43,14 @@ extension AMIVApiEvents: EndPointType {
switch self { switch self {
case .events, .media, .eventSignups: case .events, .media, .eventSignups:
return .request return .request
case .signup(let eventSignup):
return .requestJson(json: eventSignup)
} }
} }
public var headers: HTTPHeaders? { public var headers: HTTPHeaders? {
switch self { switch self {
case .events, .media, .eventSignups: case .events, .media, .eventSignups, .signup:
return nil return nil
} }
} }
...@@ -54,7 +59,7 @@ extension AMIVApiEvents: EndPointType { ...@@ -54,7 +59,7 @@ extension AMIVApiEvents: EndPointType {
switch self { switch self {
case .events, .media: case .events, .media:
return false return false
case .eventSignups: case .eventSignups, .signup:
return true return true
} }
} }
......
...@@ -100,7 +100,7 @@ extension NetworkManager where EndPoint == AMIVApiEvents { ...@@ -100,7 +100,7 @@ extension NetworkManager where EndPoint == AMIVApiEvents {
} }
} }
public func getEventSignups(_ completion: @escaping (_ image: Data?, _ error: String?) -> Void) { public func getEventSignups(_ completion: @escaping (_ signups: [EventSignup]?, _ error: String?) -> Void) {
router.request(.eventSignups) { (data, response, error) in router.request(.eventSignups) { (data, response, error) in
guard error == nil else { guard error == nil else {
completion(nil, error?.localizedDescription) completion(nil, error?.localizedDescription)
...@@ -118,13 +118,43 @@ extension NetworkManager where EndPoint == AMIVApiEvents { ...@@ -118,13 +118,43 @@ extension NetworkManager where EndPoint == AMIVApiEvents {
completion(nil, NetworkResponse.noData.rawValue) completion(nil, NetworkResponse.noData.rawValue)
return return
} }
completion(responseData, nil) do {
let json = try JSONDecoder().decode(EventsSignupResponse.self, from: responseData)
completion(json.eventSignups, nil)
} catch {
completion(nil, NetworkResponse.unableToDecode.rawValue)
}
case .failure(let error): case .failure(let error):
completion(nil, error) completion(nil, error)
} }
} }
} }
public func signupTo(_ event: EventSignup, _ completion: @escaping (_ success: Bool, _ error: String?) -> Void) {
router.request(.signup(event)) { (data, response, error) in
guard error == nil else {
completion(false, error?.localizedDescription)
return
}
guard let response = response as? HTTPURLResponse else {
return
}
if let data = data {
debugPrint(String(data: data, encoding: .utf8))
}
let result = self.handleNetworkRequest(response)
switch result {
case .success:
completion(true, nil)
case .failure(let error):
completion(false, error)
}
}
}
} }
extension NetworkManager where EndPoint == AMIVApiJobs { extension NetworkManager where EndPoint == AMIVApiJobs {
......
...@@ -10,35 +10,39 @@ import Foundation ...@@ -10,35 +10,39 @@ import Foundation
public struct EventSignup { public struct EventSignup {
public var event: String public var eventID: String
public var user: String public var userID: String?
public var email: String public var email: String
public var isConfirmed: Bool public var isConfirmed: Bool
public var isAccepted: Bool public var isAccepted: Bool
public var id: String public var signupID: String
public var etag: String
} }
extension EventSignup: Decodable { extension EventSignup: Codable {
private enum EventSignupCodingKeys: String, CodingKey { private enum EventSignupCodingKeys: String, CodingKey {
case event = "event" case eventID = "event"
case user = "user" case userID = "user"
case email = "email" case email = "email"
case isConfirmed = "confirmed" case isConfirmed = "confirmed"
case isAccepted = "accepted" case isAccepted = "accepted"
case id = "_id" case signupID = "_id"
case etag = "_etag"
} }
public init(from decoder: Decoder) throws { public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: EventSignupCodingKeys.self) let container = try decoder.container(keyedBy: EventSignupCodingKeys.self)
self.event = try container.decode(String.self, forKey: .event) self.eventID = try container.decode(String.self, forKey: .eventID)
self.user = try container.decode(String.self, forKey: .user) self.userID = try? container.decode(String.self, forKey: .userID)
self.email = try container.decode(String.self, forKey: .email) self.email = try container.decode(String.self, forKey: .email)
self.isConfirmed = try container.decode(Bool.self, forKey: .isConfirmed) self.isConfirmed = try container.decode(Bool.self, forKey: .isConfirmed)
self.isAccepted = try container.decode(Bool.self, forKey: .isAccepted) self.isAccepted = try container.decode(Bool.self, forKey: .isAccepted)
self.id = try container.decode(String.self, forKey: .id) self.signupID = try container.decode(String.self, forKey: .signupID)
self.etag = try container.decode(String.self, forKey: .etag)
} }
// TODO: - Implement encodable
} }
...@@ -15,5 +15,6 @@ public enum HTTPTask { ...@@ -15,5 +15,6 @@ public enum HTTPTask {
case request case request
case requestParameters(bodyParameters: Parameters?, urlParameter: Parameters?) case requestParameters(bodyParameters: Parameters?, urlParameter: Parameters?)
case requestParametersAndHeaders(bodyParameters: Parameters?, urlParameter: Parameters?, additionalHeaders: HTTPHeaders?) case requestParametersAndHeaders(bodyParameters: Parameters?, urlParameter: Parameters?, additionalHeaders: HTTPHeaders?)
case requestJson(json: EventSignup)
} }
...@@ -73,6 +73,12 @@ public class Router<EndPoint: EndPointType>: NetworkRouter { ...@@ -73,6 +73,12 @@ public class Router<EndPoint: EndPointType>: NetworkRouter {
case .requestParametersAndHeaders(let bodyParameters, let urlParameters, let additionalHeaders): case .requestParametersAndHeaders(let bodyParameters, let urlParameters, let additionalHeaders):
self.addAdditionalHeaders(additionalHeaders, request: &request) self.addAdditionalHeaders(additionalHeaders, request: &request)
try self.configureParameters(bodyParameters: bodyParameters, urlParameters: urlParameters, request: &request) try self.configureParameters(bodyParameters: bodyParameters, urlParameters: urlParameters, request: &request)
case .requestJson(let json):
let jsonData = try JSONEncoder().encode(json)
request.httpBody = jsonData
if request.value(forHTTPHeaderField: "Content-Type") == nil {
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
}
} }
return request return request
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment