FRE-4665: Implement Phase 3 AI training plans and premium features
- Models: TrainingPlan, Race, FamilyPlan, BeginnerMode, CommunityEvent - Services: 5 service layers with protocol-based architecture - ViewModels: 5 view models with @MainActor ObservableObject pattern - Views: 10 SwiftUI views for all Phase 3 features - Updated README with full Phase 3 documentation Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
153
Lendair/Services/CommunityEventService.swift
Normal file
153
Lendair/Services/CommunityEventService.swift
Normal file
@@ -0,0 +1,153 @@
|
||||
import Foundation
|
||||
|
||||
// MARK: - Service Protocol
|
||||
|
||||
protocol CommunityEventServiceProtocol: Sendable {
|
||||
func listEvents(filter: EventFilter) async throws -> [CommunityEvent]
|
||||
func getEvent(id: String) async throws -> (event: CommunityEvent, participants: [EventParticipant])
|
||||
func createEvent(request: CreateEventRequest) async throws -> CommunityEvent
|
||||
func updateEvent(id: String, request: UpdateEventRequest) async throws -> CommunityEvent
|
||||
func RSVP(eventId: String, status: RSVPStatus) async throws
|
||||
}
|
||||
|
||||
// MARK: - Default Service
|
||||
|
||||
class CommunityEventService: CommunityEventServiceProtocol {
|
||||
private let baseURL: URL
|
||||
private let session: URLSession
|
||||
private let authToken: String?
|
||||
|
||||
init(
|
||||
baseURL: URL = URL(string: "http://localhost:3000")!,
|
||||
session: URLSession = .shared,
|
||||
authToken: String? = nil
|
||||
) {
|
||||
self.baseURL = baseURL
|
||||
self.session = session
|
||||
self.authToken = authToken
|
||||
}
|
||||
|
||||
func listEvents(filter: EventFilter = EventFilter()) async throws -> [CommunityEvent] {
|
||||
var components = URLComponents(url: baseURL.appendingPathComponent("/api/events"), resolvingAgainstBaseURL: true)!
|
||||
var queryItems: [URLQueryItem] = [
|
||||
URLQueryItem(name: "limit", value: String(filter.limit)),
|
||||
URLQueryItem(name: "offset", value: String(filter.offset))
|
||||
]
|
||||
if let type = filter.eventType { queryItems.append(URLQueryItem(name: "type", value: type.rawValue)) }
|
||||
if let startDate = filter.startDate { queryItems.append(URLQueryItem(name: "startDate", value: ISO8601DateFormatter().string(from: startDate))) }
|
||||
if let endDate = filter.endDate { queryItems.append(URLQueryItem(name: "endDate", value: ISO8601DateFormatter().string(from: endDate))) }
|
||||
if let location = filter.location { queryItems.append(URLQueryItem(name: "location", value: location)) }
|
||||
if let radius = filter.radiusKm { queryItems.append(URLQueryItem(name: "radius", value: String(radius))) }
|
||||
if let rsvp = filter.rsvpStatus { queryItems.append(URLQueryItem(name: "rsvp", value: rsvp.rawValue)) }
|
||||
components.queryItems = queryItems
|
||||
|
||||
let request = try buildRequest(url: components.url!)
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(EventListResponse.self, from: data)
|
||||
return decoded.events
|
||||
}
|
||||
|
||||
func getEvent(id: String) async throws -> (event: CommunityEvent, participants: [EventParticipant]) {
|
||||
let url = baseURL.appendingPathComponent("/api/events/\(id)")
|
||||
let request = try buildRequest(url: url)
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(EventDetailResponse.self, from: data)
|
||||
return (decoded.event, decoded.participants)
|
||||
}
|
||||
|
||||
func createEvent(request: CreateEventRequest) async throws -> CommunityEvent {
|
||||
let url = baseURL.appendingPathComponent("/api/events")
|
||||
var request = try buildRequest(url: url, method: .post)
|
||||
request.httpBody = try JSONEncoder().encode(request)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(CreateEventResponse.self, from: data)
|
||||
return decoded.event
|
||||
}
|
||||
|
||||
func updateEvent(id: String, request: UpdateEventRequest) async throws -> CommunityEvent {
|
||||
let url = baseURL.appendingPathComponent("/api/events/\(id)")
|
||||
var request = try buildRequest(url: url, method: .patch)
|
||||
request.httpBody = try JSONEncoder().encode(request)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(UpdateEventResponse.self, from: data)
|
||||
return decoded.event
|
||||
}
|
||||
|
||||
func RSVP(eventId: String, status: RSVPStatus) async throws {
|
||||
let url = baseURL.appendingPathComponent("/api/events/\(eventId)/rsvp")
|
||||
var request = try buildRequest(url: url, method: .post)
|
||||
request.httpBody = try JSONEncoder().encode(["status": status.rawValue])
|
||||
|
||||
let (_, response) = try await session.data(for: request)
|
||||
try validateResponse(response)
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func buildRequest(url: URL, method: HTTPMethod = .get, body: Data? = nil) throws -> URLRequest {
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method.rawValue
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
|
||||
if let token = authToken {
|
||||
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
|
||||
if let body = body {
|
||||
request.httpBody = body
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
private func validateResponse(_ response: URLResponse) throws {
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw CommunityEventError.invalidResponse
|
||||
}
|
||||
|
||||
guard (200...299).contains(httpResponse.statusCode) else {
|
||||
switch httpResponse.statusCode {
|
||||
case 401: throw CommunityEventError.unauthorized
|
||||
case 403: throw CommunityEventError.forbidden
|
||||
case 404: throw CommunityEventError.notFound
|
||||
case 429: throw CommunityEventError.rateLimited
|
||||
case 500...599: throw CommunityEventError.serverError(httpResponse.statusCode)
|
||||
default: throw CommunityEventError.httpError(httpResponse.statusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Error Types
|
||||
|
||||
enum CommunityEventError: LocalizedError {
|
||||
case invalidResponse
|
||||
case unauthorized
|
||||
case forbidden
|
||||
case notFound
|
||||
case rateLimited
|
||||
case serverError(Int)
|
||||
case httpError(Int)
|
||||
|
||||
var errorDescription: String {
|
||||
switch self {
|
||||
case .invalidResponse: return "Invalid server response"
|
||||
case .unauthorized: return "Unauthorized — please log in again"
|
||||
case .forbidden: return "Forbidden — check permissions"
|
||||
case .notFound: return "Event not found"
|
||||
case .rateLimited: return "Too many requests — try again shortly"
|
||||
case .serverError(let code): return "Server error (\(code))"
|
||||
case .httpError(let code): return "HTTP error (\(code))"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user