diff --git a/src/app/card-resto/card-resto.component.ts b/src/app/card-resto/card-resto.component.ts index 59750a4..2c932c8 100644 --- a/src/app/card-resto/card-resto.component.ts +++ b/src/app/card-resto/card-resto.component.ts @@ -1,5 +1,6 @@ import { Component,EventEmitter, Input, Output, OnInit } from '@angular/core'; import { ApiBackService } from '../services/api-back.service'; +import { TokenService } from '../services/token.service'; @Component({ selector: 'app-card-resto', @@ -11,17 +12,20 @@ export class CardRestoComponent implements OnInit { @Input() restaurant : any ; distance : number; @Input() likeResto: any; - @Output() clickLike = new EventEmitter(); + //@Output() clickLike = new EventEmitter(); isLiked : boolean = false; priceRef = ["Indisponible","1-10€ ","11-20€","21-30€","31-40€"]; + private userId : number | null; - constructor(private apiBackService : ApiBackService) { + constructor(private apiBackService : ApiBackService, private tokenService : TokenService) { this.distance = 0 ; + this.userId = 0; } ngOnInit(): void { + this.userId = this.tokenService.getCurrentUserId(); this.distance = Math.round( this.apiBackService.setDistance( 48.86201110271593 , //latitude Simplon @@ -36,7 +40,13 @@ export class CardRestoComponent implements OnInit { onClickLike() { console.log('click'); this.restaurant.restauLike = !this.restaurant.restauLike; - this.clickLike.emit(this.restaurant.restauLike); + //this.clickLike.emit(this.restaurant.restauLike); + + if(this.restaurant.restauLike){ + this.apiBackService.addPreference(this.userId,this.restaurant.id).subscribe(); + }else{ + this.apiBackService.deletePreference(this.userId,this.restaurant.id).subscribe(); + } } } diff --git a/src/app/pages/models/preference.ts b/src/app/pages/models/preference.ts new file mode 100644 index 0000000..1dfad44 --- /dev/null +++ b/src/app/pages/models/preference.ts @@ -0,0 +1,11 @@ +export interface Preference { + preferencePK:{ + personne: { + id: number | null + }, + restau: { + id: number | undefined + }}, + note?: null, + favoris?: null +} diff --git a/src/app/pages/resto-page/resto-page.component.html b/src/app/pages/resto-page/resto-page.component.html index 1403da7..0a07482 100644 --- a/src/app/pages/resto-page/resto-page.component.html +++ b/src/app/pages/resto-page/resto-page.component.html @@ -5,6 +5,7 @@
- + +
diff --git a/src/app/pages/resto-page/resto-page.component.ts b/src/app/pages/resto-page/resto-page.component.ts index 40e8cfc..4dfc1cd 100644 --- a/src/app/pages/resto-page/resto-page.component.ts +++ b/src/app/pages/resto-page/resto-page.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'; import { forkJoin } from 'rxjs'; import { ApiBackService } from 'src/app/services/api-back.service'; import { TokenService } from 'src/app/services/token.service'; +import { Preference } from '../models/preference'; import { Restaurant } from '../models/restaurant'; @Component({ @@ -11,44 +12,39 @@ import { Restaurant } from '../models/restaurant'; }) export class RestoPageComponent implements OnInit { - public listRestaurants : any; + public listRestaurants : Restaurant[]; public listPref : any; public restaurantPref : any; public beforeRoute ?: string ; + private userId : number | null; constructor(private apiBackService : ApiBackService, private tokenService : TokenService) { this.listRestaurants = []; this.restaurantPref = []; this.beforeRoute = this.apiBackService.routeParam; + this.userId=0; } ngOnInit(): void { this.restauLiked(); - - // // arrivée sur la restau-page depuis filtres ou home(catégories) : appel a une méthode différente du service - // if(this.apiBackService.routeParam === "filtres"){ - // this.listRestaurants = this.apiBackService.restoFilter; - // }else if(this.apiBackService.routeParam === "categories"){ - // this.apiBackService.restoByCat.subscribe((restaurants: any[]) => { - // this.listRestaurants = restaurants; - // }); - // }else{ // si on arrive sur l'url /restaurants directement = tous les restau affichés - - - // this.restauLiked(); - - // } + this.userId = this.tokenService.getCurrentUserId(); } - onEventLike(){ - this.apiBackService.restoLiked$.next(true); - console.log(this.apiBackService.restoLiked$); + // onEventLike(liked : any, idRestau : number | undefined){ + // //this.apiBackService.restoLiked$.next(true); + // console.log(this.userId); + // console.log(idRestau); - } + // if(liked){ + // this.apiBackService.addPreference(this.userId,idRestau).subscribe(); + // }else{ + // this.apiBackService.deletePreference(this.userId,idRestau).subscribe(); + // } + // } saveRestauList(event : any){ console.log(event); diff --git a/src/app/services/api-back.service.ts b/src/app/services/api-back.service.ts index 0978966..28ed064 100644 --- a/src/app/services/api-back.service.ts +++ b/src/app/services/api-back.service.ts @@ -80,5 +80,13 @@ export class ApiBackService { this.restauAModif.emit(restau); } + + public addPreference(idUser : number|null, idRestau:number|undefined) : Observable{ + return this.httpClient.post(`${environment.apiUrl}/add-preference/${idUser}/${idRestau}`, {}); + } + + deletePreference(idUser : number|null, idRestau:number|undefined): Observable { + return this.httpClient.delete(`${environment.apiUrl}/delete-preference/${idUser}/${idRestau}`); + } }