Ihave 2 api ( users and category ), and I have relationship between this api in category id, how can I make join this api like inner join in sql.
My Code :
- frist API ( name in posts ):
{
"list": [
{
"id": 1,
"title": "samsung",
"body": "samsung is ......",
"userId": "1"
},
{
"id": 2,
"title": "google",
"body": "google is ......",
"userId": "1"
},
{
"id": 1,
"title": "yahoo",
"body": "yahoo is ......",
"userId": "2"
}
],
"count": 3,
"success": true
}
- second API ( name in users):
{
"list": [
{
"userId": 1,
"username": "Michil"
},
{
"userId": 2,
"username": "Alix"
},
{
"userId": 3,
"username": "Jon"
}
],
"count": 3,
"success": true
}
- and I created 2 interfaces for this API like this:
import { PostsList } from "./PostsList "
export interface Posts{
list: PostsList[]
count: number
success: boolean
}
export interface PostsList {
id: number
title: string
body: string
userId: string
}
import { UsersList} from "./UsersList"
export interface Users{
list: List[]
count: number
success: boolean
}
export interface UsersList{
userId: number
username: string
}
- and I created a service to get data from the APIS URL like this:
getPosts(): Observable<Posts>{
return this.http.get<Posts>(`https://api.api.com/post/list`).pipe(
tap(posts=> console.log(posts)),
);
}
getUsers(): Observable<Users>{
return this.http.get<Users>(`https://api.api.com/users/list`).pipe(
tap(users => console.log(users)),
);
}
- and I called this service in my component.ts like this:
export class UsersComponent implements OnInit{
displayedColumns: string[] = ['id', 'title', 'body', 'userId', 'username'];
users:any;
constructor(private myService: MyService){ }
ngOnInit(): void {
this.onGetUsers();
}
onGetPosts(): void{
this.myService.getPosts().subscribe(
(response => {
this.users = new MatTableDataSource<Posts>(response);
})
);
}
onGetUsers(): void{
this.myService.getUsers().subscribe(
(response => {
this.Posts = new MatTableDataSource<Posts>(response);
this.Users = new MatTableDataSource<Users>(response);
})
);
}
- and view this data in the material table like this:
<table mat-table [dataSource]="users" class="mat-elevation-z8">
Position Column
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> id</th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="title">
<th mat-header-cell *matHeaderCellDef> title</th>
<td mat-cell *matCellDef="let element"> {{element.title}} </td>
</ng-container>
<ng-container matColumnDef="body">
<th mat-header-cell *matHeaderCellDef> body</th>
<td mat-cell *matCellDef="let element"> {{element.body}} </td>
</ng-container>
<ng-container matColumnDef="userId">
<th mat-header-cell *matHeaderCellDef> userId </th>
<td mat-cell *matCellDef="let element"> {{element.userId}} </td>
</ng-container>
<ng-container matColumnDef="username">
<th mat-header-cell *matHeaderCellDef> username </th>
<td mat-cell *matCellDef="let element"> {{element.username}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
How can I display username from another api ?