Eu estava realizando a aula 07 no modulo 03 e minha funções de excluir e curtir não funcionam aparece esse erro no console:
AnimalService.ts:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of, throwError } from 'rxjs';
import { catchError, mapTo } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { TokenService } from '../autenticacao/token.service';
import { Animais, Animal } from './animais';
const API = environment.apiURL;
const NOT_MODIFIED = '304';
@Injectable({
providedIn: 'root',
})
export class AnimaisService {
constructor(private http: HttpClient, private tokenService: TokenService) {}
listaDoUsuario(nomeUsuario: string): Observable<Animais> {
return this.http.get<Animais>(`${API}/${nomeUsuario}/photos`);
}
buscaPorId(id: number): Observable<Animal> {
return this.http.get<Animal>(`${API}/photos/${id}`);
}
excluirAnimal(id: number): Observable<Animal> {
return this.http.delete<Animal>(`${API}/photos/${id}`);
}
curtir(id: number): Observable<boolean> {
return this.http
.post(`${API}/photos/${id}/like`, {}, { observe: 'response' })
.pipe(
mapTo(true),
catchError((error) => {
return error.status === NOT_MODIFIED ? of(false) : throwError(error);
})
);
}
}
detalhe-animal-component.ts:
<div class="bg-white border" *ngIf="animal$ | async as animal">
<div class="row">
<div class="col-lg-8">
<app-animal [url]="animal.url" [descricao]="animal.description"></app-animal>
</div>
<div class="col-lg-4">
<small>
<p class="text-left break-word">{{animal.description}}</p>
<i (click)="curtir()" class="fa fa-heart-o fa-2x mr-2 pull-left">{{animal.likes}}</i>
<i class="fa fa-comment-o fa-2x mr-2 ml-2">{{animal.comments}}</i>
<i (click)="excluir()" class="fa fa-trash-o pull-right">{{animal.comments}}</i>
</small>
</div>
</div>
</div>
detalhe-animal-component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { connectableObservableDescriptor } from 'rxjs/internal/observable/ConnectableObservable';
import { Animal } from '../animais';
import { AnimaisService } from '../animais.service';
@Component({
selector: 'app-detalhe-animal',
templateUrl: './detalhe-animal.component.html',
styleUrls: ['./detalhe-animal.component.css'],
})
export class DetalheAnimalComponent implements OnInit {
animalID!: number;
animal$!: Observable<Animal>;
constructor(
private animaisService: AnimaisService,
private activatedRoute: ActivatedRoute,
private router: Router
) {}
ngOnInit(): void {
this.animalID = this.activatedRoute.snapshot.params.animalId;
this.animal$ = this.animaisService.buscaPorId(this.animalID);
}
curtir() {
this.animaisService.curtir(this.animalID).subscribe((curtida) => {
if (curtida) {
this.animal$ = this.animaisService.buscaPorId(this.animalID);
}
});
}
excluir() {
this.animaisService.excluirAnimal(this.animalID).subscribe(
() => {
this.router.navigate(['/animais/']);
},
(error) => console.log(error)
);
}
}