I am new in swift and I am also trying to use Alamofire to call data from API. I am quite puzzled on how I will use the PUT Request to update data. I've read some solutions here in SO but I don't know how I will apply on my app. I am creating an Event App, the scenario should be, when the participant clicked the Check In Button, it will update the registered_flag
to true
meaning the participant will marked as Registered and the button will be changed to Check Out. I really don't know if my API Service is correct or not. Hope you could help me. Thank you so much.
JSON of the Event Participant Where in the registered_flag should be updated once checkInOutButton
{
"event_name": "Q & A",
"event_participants": [
{
"participant_id": "70984656-92bc-4c36-9314-2c741f068523",
"employee_number": null,
"last_name": "Surname",
"first_name": "FirstName",
"middle_name": null,
"display_name": "Surname, FirstName ",
"department_name": "Medical Informatics",
"position_name": "Application Developer",
"registered_flag": true,
"registered_datetime": "2018-09-13T08:54:40.150",
"registration_type": 1,
"delete_flag": false,
"manual_reg_flag": false,
"out_flag": false,
"out_datetime": null,
"classification": 6,
"others": "Guest"
}
}
JSON to update for check in
{
"registered_flag": true,
"registration_type": 1
}
updateType
enum UpdateParticipantType: String {
case checkIn = "Check In"
case checkOut = "Check Out"
}
APIService for UpdateParticipant
func updateParticipant(updateType: UpdateParticipantType,
participantID: String,
successBlock: @escaping ([Attendee]) -> Void,
failureBlock: @escaping (Error) -> Void)
{
let updateParticipantURL = URL(string: "\(REGISTER_PARTICIPANT_URL)/\(updateType)/\(participantID)")
Alamofire.request(updateParticipantURL!, method: .put).responseJSON { (response) in
print(response)
if let error = response.error
{
failureBlock(error)
print(error)
return
}
if let jsonArray = response.result.value as? [[String : Any]] {
for anItem in jsonArray {
if let eventparticipants = anItem["event_participants"] as? [[String : Any]] {
var extractedAttendees = [Attendee]()
for participants in eventparticipants{
let success = Attendee.init(JSON: participants)
extractedAttendees.append(success!)
}
successBlock(extractedAttendees)
}
}
}
}
}
As per Alamofire documentation:
let parameters: Parameters = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
"x": 1,
"y": 2,
"z": 3
]
]
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
For a given json
{
"registered_flag": true,
"registration_type": 1
}
let parameters: Parameters = [
"registered_flag": true
"registration_type": 1
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With