7.8: Setting Main Photo in the API
We have a button to set a user's main photo - let's get that working next.
Adding SetMainPhotoForUser() Service Method
First, in our IPhotoService.cs
file, add two method signatures:
Next, we'll implement these methods in the PhotoService.cs
class:
GetMainPhotoForUser()
returns the photo the given user has set to main.
SetMainPhotoForUser()
calls the above method to get the current main photo, sets it to false, and sets the photo from the parameter to main.
Adding SetMainPhoto() Method to the PhotosController
Test in Postman
Get a token for a user you know has multiple photos from previous testing for the /api/auth/login
endpoint.
Next, send a GET request to /api/users/{id}
to see their photos and which on is set to main. Make note of one of the non-main photo's id.
Finally, we can send a POST request to /api/users/{userId}/photos/{photoId}/setMain/
:
You should get a 204 No Content
response:
Resend the GET request to /api/users/{id}
to verify that the IsMain properties on the photos have been swapped:
Last updated