🛠️

Vapor API Update and Delete Routes

Jul 18, 2024

Vapor API Update and Delete Routes

Overview

  • Create two new routes for our Vapor API: update and delete data in the database.
  • Call these routes from a SwiftUI iOS app.

Update Route Creation

  1. Define the Update Route

    • Make a new function update that throws requests and returns an HTTP status.
    • Decode the incoming data from the iOS app using a similar method as the POST request.
  2. Find the Song

    • Query the song based on its ID.
    • If the song is not found, return a 404 error.
    • Unwrap the optional value returned by the query.
  3. Update the Song

    • Set the new title property for the song.
    • Update the song in the database.
    • Return an OK status if successful.

Delete Route Creation

  1. Define the Delete Route

    • Make a new function delete that throws requests and returns an HTTP status.
    • Use the .delete method to define the route.
  2. Find the Song

    • Use fluent to query the database for the song by its ID.
    • If the song is not found, return a 404 error.
    • Unwrap the optional value returned by the query.
  3. Delete the Song

    • Delete the song from the database.
    • Return an OK status if successful.

Running and Testing with Docker

  • Use the Docker desktop application to ensure the database is running.
  • Check the connection using Azure Data Studio.
  • Ensure that the database runs and responds correctly.

Implementing in SwiftUI iOS App

  1. Update Functionality

    • Add the new update function in the SwiftUI app.
    • Ensure the app can send PUT requests with the new data to the server.
  2. Delete Functionality

    • Add the new delete function in the SwiftUI app.
    • Ensure the app can send DELETE requests to the server.

Postman Testing

  • Use Postman to test both PUT and DELETE requests.
  • Ensure the correct status codes are returned (200 OK).
  • Confirm changes using Azure Data Studio to check the database.

Writing Functions in iOS App

  1. Update Song Function

    • Implement the update song function by creating a URL, encoding the song data, and sending a PUT request.
    • Confirm changes by querying the database.
  2. Delete Song Function

    • Implement the delete song function triggered by swipe actions in the song list.
    • Ensure the song is removed from both: database and in-app list.

Conclusion

  • Successfully implemented update and delete functionalities in both the Vapor API and the SwiftUI iOS app.
  • CRUD operations (Create, Read, Update, Delete) are fully functional.

Next Steps

  • Deploy the API to Heroku in the next session.