import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, lastValueFrom } from 'rxjs'; import { BusinessListing, CommercialPropertyListing } from '../../../../bizmatch-server/src/models/db.model'; import { BusinessListingCriteria, CommercialPropertyListingCriteria, ListingType, ResponseBusinessListingArray, ResponseCommercialPropertyListingArray } from '../../../../bizmatch-server/src/models/main.model'; import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root', }) export class ListingsService { private apiBaseUrl = environment.apiBaseUrl; constructor(private http: HttpClient) {} async getListings( criteria: BusinessListingCriteria | CommercialPropertyListingCriteria, listingsCategory: 'business' | 'professionals_brokers' | 'commercialProperty', ): Promise { const result = await lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/find`, criteria)); return result; } getNumberOfListings(criteria: BusinessListingCriteria | CommercialPropertyListingCriteria, listingsCategory: 'business' | 'commercialProperty'): Observable { return this.http.post(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/findTotal`, criteria); } async getListingsByPrompt(criteria: BusinessListingCriteria | CommercialPropertyListingCriteria): Promise { const result = await lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/listings/business/search`, criteria)); return result; } getListingById(id: string, listingsCategory?: 'business' | 'commercialProperty'): Observable { const result = this.http.get(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/${id}`); return result; } getListingsByEmail(email: string, listingsCategory: 'business' | 'commercialProperty'): Promise { return lastValueFrom(this.http.get(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/user/${email}`)); } getFavoriteListings(listingsCategory: 'business' | 'commercialProperty'): Promise { return lastValueFrom(this.http.get(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/favorites/all`)); } async save(listing: any, listingsCategory: 'business' | 'professionals_brokers' | 'commercialProperty') { if (listing.id) { return await lastValueFrom(this.http.put(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}`, listing)); } else { return await lastValueFrom(this.http.post(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}`, listing)); } } async deleteBusinessListing(id: string) { await lastValueFrom(this.http.delete(`${this.apiBaseUrl}/bizmatch/listings/business/listing/${id}`)); } async deleteCommercialPropertyListing(id: string, imagePath: string) { await lastValueFrom(this.http.delete(`${this.apiBaseUrl}/bizmatch/listings/commercialProperty/listing/${id}/${imagePath}`)); } async removeFavorite(id: string, listingsCategory?: 'business' | 'commercialProperty') { await lastValueFrom(this.http.delete(`${this.apiBaseUrl}/bizmatch/listings/${listingsCategory}/favorite/${id}`)); } }