Hello, I'm working with C # and I have the following problem:
Imagine that I have a route for my API api/estoque/inventario
where this route should calculate the inventory of the entire inventory of the application and on demand return the result. To mount this API route, I would have something similar to this:
namespace Api
{
public class Estoque { ... }
[Route("api/estoque/inventario")]
[ApiController]
public class EstoqueController: ControllerBase
{
[HttpGet]
public async Task<IActionResult<ICollection<Estoque>>> GetInventario()
{
try
{
// pega os produtos
// faz todas as regras de negócio (síncronas e assíncronas)
// retorna o estoque com o inventário (como?)
}
catch(Exception ex)
{
throw;
}
}
}
}
The problem is that even though I have an asynchronous task in my API, the response to my client is not asynchronous. On the Customer side, I use the RxJS library along with Angular and the service that makes the requests to the API has the return of an observable object, Thus:
export const API_URL = '...';
export interface IEstoque { ... }
@Injectable({
providedIn: 'root',
})
export class EstoqueService {
private baseUrl: string = "/api/estoque";
constructor(public http: HttpClient) { }
public getInventario(): Observable<IEstoque[]> {
return this.http.get('${API_URL}${this.baseUrl}/inventario');
}
}
My intention is to use the list of items processed in stock together with the AsyncPipe pipe that exists in the angle, which makes with which I can loop asynchronously based on the return of the service, thus:
<div *ngFor='let estoque of estoqueList | async'>
{{estoque | json}}
</div>
@Component({
selector: 'estoque-list',
templateUrl: 'estoque-list.component.html',
})
export class EstoqueList {
public estoqueList: Observable<IEstoque[]>;
constructor(public estoqueService: EstoqueService) {
this.estoqueList = estoqueService.getInventario();
}
}
What do I need to do to have my api/estoque/inventario
route send the inventory list asynchronously and continuously to my frontend based on processing?