Create the GET Endpoint
Learn and practice how to implement a GET endpoint.
Get address by using the id endpoint
We created the address module in previous lessons with a controller and service. It’s time to implement our first API endpoint!
Add the service method
First, let’s create a new method in AddressService.
Press + to interact
@Injectable()export class AddressService {private addressDataStore: AddressDto[] = [];// Retrieves an address by its unique ID.getById(id: number) {// Finds an address in the 'addresses' store// where the 'id' property matches the provided 'id'.return this.addressDataStore.find(t => t.id === id);}}
Here, we use the addressDataStore private property to store the address data. This is a temporary work-around, and we’ll implement the data persistent to the MySQL database later in the course.
Notice that the getById method doesn’t have a modifier in the code snippet above. We didn’t add a public modifier for the method because all class members default as public in TypeScript, which means they’re accessible from outside the class. ...
Ask