initial release

This commit is contained in:
2024-02-29 10:23:41 -06:00
commit 5146c8e919
210 changed files with 11040 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
import { Inject, Injectable } from '@nestjs/common';
import {
BusinessListing,
InvestmentsListing,
ListingCriteria,
ProfessionalsBrokersListing,
} from '../models/main.model.js';
import { LISTINGS, RedisService } from '../redis/redis.service.js';
import { convertStringToNullUndefined } from '../utils.js';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
@Injectable()
export class ListingsService {
// private readonly logger = new Logger(ListingsService.name);
constructor(private redisService: RedisService,@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {}
async setListing(
value: BusinessListing | ProfessionalsBrokersListing | InvestmentsListing,
id?: string,
) {
if (!id) {
id = await this.redisService.getId(LISTINGS);
value.id = id;
this.logger.info(`No ID - creating new one:${id}`)
} else {
this.logger.info(`ID available:${id}`)
}
this.redisService.setJson(id, value);
}
async getListingById(id: string) {
return await this.redisService.getJson(id, LISTINGS);
}
deleteListing(id: string){
this.redisService.delete(id);
this.logger.info(`delete listing with ID:${id}`)
}
async getAllListings(start?: number, end?: number) {
const searchResult = await this.redisService.search(LISTINGS, '*');
const listings = searchResult.slice(1).reduce((acc, item, index, array) => {
// Jedes zweite Element (beginnend mit dem ersten) ist ein JSON-String des Listings
if (index % 2 === 1) {
try {
const listing = JSON.parse(item[1]); // Parsen des JSON-Strings
acc.push(listing);
} catch (error) {
console.error('Fehler beim Parsen des JSON-Strings: ', error);
}
}
return acc;
}, []);
return listings;
}
//criteria.type,criteria.location,criteria.minPrice,criteria.maxPrice,criteria.realEstateChecked,criteria.listingsCategory
//async find(type:string,location:string,minPrice:string,maxPrice:string,realEstateChecked:boolean,listingsCategory:string): Promise<any> {
async find(criteria:ListingCriteria): Promise<any> {
let listings = await this.getAllListings();
listings=listings.filter(l=>l.listingsCategory===criteria.listingsCategory);
if (convertStringToNullUndefined(criteria.type)){
console.log(criteria.type);
listings=listings.filter(l=>l.type===criteria.type);
}
if (convertStringToNullUndefined(criteria.location)){
console.log(criteria.location);
listings=listings.filter(l=>l.location===criteria.location);
}
if (convertStringToNullUndefined(criteria.minPrice)){
console.log(criteria.minPrice);
listings=listings.filter(l=>l.price>=Number(criteria.minPrice));
}
if (convertStringToNullUndefined(criteria.maxPrice)){
console.log(criteria.maxPrice);
listings=listings.filter(l=>l.price<=Number(criteria.maxPrice));
}
if (convertStringToNullUndefined(criteria.realEstateChecked)){
console.log(criteria.realEstateChecked);
listings=listings.filter(l=>l.realEstateIncluded);
}
if (convertStringToNullUndefined(criteria.category)){
console.log(criteria.category);
listings=listings.filter(l=>l.category===criteria.category);
}
return listings
}
}