RabbinatixDocs
Sign inGet started

Example

Write endpoint logic that updates a record

A signed-in person changes their display name. The users service trims the name, refuses an empty one, saves it and answers with the new name. It shows text clean-up, a 400 path and a database update.

You will build

  • The PATCH /api/v1/me API
  • A users.updateName operation
  • Endpoint logic with a clean-up step, a check and a save

System map

  • Auth ServiceUsers DBusers.updateName (update)

Uses Auth Service and Users DB from the Login example. Any backend service with a database will do.

Part 1: Declare the API and the update

  1. 1.

    Open Auth Service → APIs → Add an API. Provided, PATCH, /api/v1/me, summary Change my display name. Under Authentication, pick Bearer token. Request body displayName: string. 200 body displayName: string. +400 body error: string. Click Create endpoint.

  2. 2.

    Open Users DB → Resources → Operation. Untick may return nothing and Save.

    Name
    users.updateName
    Type
    update
    Query / description
    UPDATE users SET display_name = :displayName WHERE id = :userId
    Input fields
    userId: string displayName: string
    Output fields
    displayName: string

Part 2: Build the logic

  1. 1.

    On the PATCH /api/v1/me row, click + Logic. On Request has a body connection point and an auth token connection point.

  2. 2.

    Add Get Property with path displayName on body, then Trim on its value.

  3. 3.

    Add Is Empty on the Trim result, and a Branch Name empty? on its result.

  4. 4.

    From True: Return Response 400, body Make Object error = The display name can’t be empty.

  5. 5.

    From False: Database Update (label Save name), Operation users.updateName. userId from the auth token, displayName from the Trim result.

    In real code the token is decoded into a user id first. Here the token stands for the user.

  6. 6.

    Then Return Response 200, with the update’s result connected into body.

Check: The Problems tab is empty.

Part 3: Test it

  1. 1.

    Click Test. Body { "displayName": " Nia " } answers 200, and the step data of Save name shows Nia without the spaces.

  2. 2.

    Body { "displayName": " " } answers 400 with “The display name can’t be empty”.