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)
Part 1: Declare the API and the update
- 1.
Open Auth Service → APIs → Add an API. Provided, PATCH,/api/v1/me, summaryChange my display name. Under Authentication, pick Bearer token. Request bodydisplayName: string. 200 bodydisplayName: string. +400 bodyerror: string. Click Create endpoint. - 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.
On the PATCH /api/v1/merow, click + Logic. On Request has abodyconnection point and anauth tokenconnection point. - 2.
Add Get Property with path displayNameonbody, then Trim on itsvalue. - 3.
Add Is Empty on the Trim result, and a BranchName empty?on itsresult. - 4.
From True: Return Response 400, body Make Objecterror=The display name can’t be empty. - 5.
From False: Database Update (label Save name), Operationusers.updateName.userIdfrom theauth token,displayNamefrom the Trimresult.In real code the token is decoded into a user id first. Here the token stands for the user. - 6.
Then Return Response 200, with the update’sresultconnected intobody.
Check:
Part 3: Test it
- 1.
Click Test. Body { "displayName": " Nia " }answers 200, and the step data ofSave nameshowsNiawithout the spaces. - 2.
Body { "displayName": " " }answers 400 with “The display name can’t be empty”.
