Fix P0-P3 code review issues for clubs and challenges (FRE-4664)
P0: Fix variable shadowing in ClubService.createClub/updateClub and
ChallengeService.createChallenge/updateChallenge — renamed local
'var request' to 'var urlRequest' so JSONEncoder encodes the
typed parameter, not the URLRequest.
P1: Wire CreateClubSheet and CreateChallengeSheet to parent ViewModel —
sheets now receive viewModel and call createClub/createChallenge
before dismissing.
P2: Extract HTTPMethod enum to shared Utils/HTTPMethod.swift (was
defined in NotificationService). Remove dead 'body' parameter from
buildRequest in all three services. Add error alert UI to
ClubsView and ChallengesView.
P3: Replace forced URL unwrap with static let defaultBaseURL in all
three services. Fix MockChallengeService.updateChallenge to track
updateCalled instead of always throwing notFound.
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -20,8 +20,10 @@ class ClubService: ClubServiceProtocol {
|
||||
private let session: URLSession
|
||||
private let authToken: String?
|
||||
|
||||
static let defaultBaseURL: URL = URL(string: "http://localhost:3000")!
|
||||
|
||||
init(
|
||||
baseURL: URL = URL(string: "http://localhost:3000")!,
|
||||
baseURL: URL = defaultBaseURL,
|
||||
session: URLSession = .shared,
|
||||
authToken: String? = nil
|
||||
) {
|
||||
@@ -63,10 +65,10 @@ class ClubService: ClubServiceProtocol {
|
||||
|
||||
func createClub(request: CreateClubRequest) async throws -> Club {
|
||||
let url = baseURL.appendingPathComponent("/api/clubs")
|
||||
var request = try buildRequest(url: url, method: .post)
|
||||
request.httpBody = try JSONEncoder().encode(request)
|
||||
var urlRequest = try buildRequest(url: url, method: .post)
|
||||
urlRequest.httpBody = try JSONEncoder().encode(request)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
let (data, response) = try await session.data(for: urlRequest)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(CreateClubResponse.self, from: data)
|
||||
@@ -75,10 +77,10 @@ class ClubService: ClubServiceProtocol {
|
||||
|
||||
func updateClub(id: String, request: UpdateClubRequest) async throws -> Club {
|
||||
let url = baseURL.appendingPathComponent("/api/clubs/\(id)")
|
||||
var request = try buildRequest(url: url, method: .patch)
|
||||
request.httpBody = try JSONEncoder().encode(request)
|
||||
var urlRequest = try buildRequest(url: url, method: .patch)
|
||||
urlRequest.httpBody = try JSONEncoder().encode(request)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
let (data, response) = try await session.data(for: urlRequest)
|
||||
try validateResponse(response)
|
||||
|
||||
let decoded = try JSONDecoder().decode(UpdateClubResponse.self, from: data)
|
||||
@@ -120,7 +122,7 @@ class ClubService: ClubServiceProtocol {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func buildRequest(url: URL, method: HTTPMethod = .get, body: Data? = nil) throws -> URLRequest {
|
||||
private func buildRequest(url: URL, method: HTTPMethod = .get) throws -> URLRequest {
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method.rawValue
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
@@ -129,10 +131,6 @@ class ClubService: ClubServiceProtocol {
|
||||
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
|
||||
if let body = body {
|
||||
request.httpBody = body
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user