Swift - How to populate array

Viewed 107

I want to send a post request to my API but I am having some issues with mapping.

The user starts on VCA and sees a list of users that is retrieved via GET request, the user can tap on a cell and see the details (VCB). on VCB, you can tap a cell again, and you get an action sheet which will update that status to available or unavailable. The issue I have is to populate the dailyInjuryStatus array in the POST request.

Get request model:

struct DailyInjuryIllnessPlayer: Hashable, Codable {
        var playerId: Int
        var playerName: String?
        var numberOfStatusMissingDays: Int
        var entries: [Entry]

        func hash(into hasher: inout Hasher) {
            hasher.combine(playerId)
        }

        static func == (lhs: DailyInjuryIllnessPlayer, rhs: DailyInjuryIllnessPlayer) -> Bool {
            return lhs.playerId == rhs.playerId
        }
    }

    struct Entry: Codable {
        var injuryIllnessId: Int
        var injuryIllnessName: String?
        var statusDate: Date
        var modifiedBy: String?
        var status: String?
        var playerStatusId: Int
        var parentEventId: Int?
    }

The model for the POST

struct CreateDailyInjuryIllnessNote: Encodable {
        var teamId: UInt32
        var dailyInjuryIllnessStatus: [DailyInjuryIllnessStatus]

    }

    struct DailyInjuryIllnessStatus: Encodable {
        var iIeventId: UInt32
        var effectiveDate: Date
        var eventStatusId: UInt32
        var playerId: UInt32
    }

The code I am using for the request (Extracting the values from the screen to send to the API):


private extension DailyInjuryIllnessAPI.Model.Request.CreateDailyInjuryIllnessNote {

    static func from(playerId: Int?, statusNote: DailyInjuryIllnessPlayer) -> Self? {
        guard let playerId = playerId else { return nil }
        let teamId = getTeamId(from: statusNote)

// Here is the issue, I can populate teamId, but I cannot populate dailyInjuryIllnessStatus.

        return .init(teamId: UInt32(teamId), dailyInjuryIllnessStatus: <#T##[DailyInjuryIllnessAPI.Model.Request.DailyInjuryIllnessStatus]#>)
        }


    private static func getTeamId(from model: DailyInjuryIllnessPlayer) -> Int {
        let answer = model.playerId
        return answer
    }

    private static func getEventId(from model: DailyInjuryIllnessPlayer) -> Int {
        var answer = 1
        for eventId in model.entries {
            answer = eventId.injuryIllnessId
        }

        return answer
    }

    private static func getEffectiveDate(from model: DailyInjuryIllnessPlayer) -> Date {
        var answer = Date()
        for date in model.entries {
            answer = date.statusDate
        }
        return answer
    }

    private static func getEventStatusId(from model: DailyInjuryIllnessPlayer) -> Int {
        var answer = 1
        for statusId in model.entries {
            answer = statusId.playerStatusId
        }
        return answer
    }

    private static func getPlayerId(from model: DailyInjuryIllnessPlayer) -> Int {
        var answer = model.playerId
        return answer
    }



    }

The issue as stated in the code is at the return .init Swift expects it to be like the model which has a teamId and an array of DailyInjuryIllnessStatus. How can I put the methods I used to extract those values into the array

0 Answers
Related