To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

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 {
events.delegate = self
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
......
......@@ -74,13 +74,9 @@ extension OnboardingNavigator: LoginViewControllerDelegate {
let userManager = NetworkManager<AMIVApiUser>()
userManager.getUserInfo({ (user, error) in
guard error == nil, let user = user else {
debugPrint(error)
return
}
if user.saveLocal() {
debugPrint(User.loadLocal())
}
let _ = user.saveLocal()
})
DispatchQueue.main.async {
......
......@@ -12,6 +12,7 @@ public enum AMIVApiEvents {
case events
case eventSignups
case signup(_ eventSignup: EventSignup)
case media(_ path: String)
}
......@@ -22,7 +23,7 @@ extension AMIVApiEvents: EndPointType {
switch self {
case .events:
return "/events"
case .eventSignups:
case .eventSignups, .signup:
return "/eventsignups"
case .media(let path):
return path
......@@ -33,6 +34,8 @@ extension AMIVApiEvents: EndPointType {
switch self {
case .events, .media, .eventSignups:
return .get
case .signup:
return .post
}
}
......@@ -40,12 +43,14 @@ extension AMIVApiEvents: EndPointType {
switch self {
case .events, .media, .eventSignups:
return .request
case .signup(let eventSignup):
return .requestJson(json: eventSignup)
}
}
public var headers: HTTPHeaders? {
switch self {
case .events, .media, .eventSignups:
case .events, .media, .eventSignups, .signup:
return nil
}
}
......@@ -54,7 +59,7 @@ extension AMIVApiEvents: EndPointType {
switch self {
case .events, .media:
return false
case .eventSignups:
case .eventSignups, .signup:
return true
}
}
......
......@@ -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
guard error == nil else {
completion(nil, error?.localizedDescription)
......@@ -118,13 +118,43 @@ extension NetworkManager where EndPoint == AMIVApiEvents {
completion(nil, NetworkResponse.noData.rawValue)
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):
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 {
......
......@@ -10,35 +10,39 @@ import Foundation
public struct EventSignup {
public var event: String
public var user: String
public var eventID: String
public var userID: String?
public var email: String
public var isConfirmed: 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 {
case event = "event"
case user = "user"
case eventID = "event"
case userID = "user"
case email = "email"
case isConfirmed = "confirmed"
case isAccepted = "accepted"
case id = "_id"
case signupID = "_id"
case etag = "_etag"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: EventSignupCodingKeys.self)
self.event = try container.decode(String.self, forKey: .event)
self.user = try container.decode(String.self, forKey: .user)
self.eventID = try container.decode(String.self, forKey: .eventID)
self.userID = try? container.decode(String.self, forKey: .userID)
self.email = try container.decode(String.self, forKey: .email)
self.isConfirmed = try container.decode(Bool.self, forKey: .isConfirmed)
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 {
case request
case requestParameters(bodyParameters: Parameters?, urlParameter: Parameters?)
case requestParametersAndHeaders(bodyParameters: Parameters?, urlParameter: Parameters?, additionalHeaders: HTTPHeaders?)
case requestJson(json: EventSignup)
}
......@@ -73,6 +73,12 @@ public class Router<EndPoint: EndPointType>: NetworkRouter {
case .requestParametersAndHeaders(let bodyParameters, let urlParameters, let additionalHeaders):
self.addAdditionalHeaders(additionalHeaders, 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
......
Markdown is supported
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