Start Umbau zu postgres

This commit is contained in:
2024-04-14 22:52:19 +02:00
parent e784b424b0
commit 7d10080069
21 changed files with 5441 additions and 272 deletions

View File

@@ -4,51 +4,52 @@ import { convertStringToNullUndefined } from '../utils.js';
import { ListingsService } from './listings.service.js';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { ListingCriteria } from 'src/models/main.model.js';
@Controller('listings/business')
export class BusinessListingsController {
constructor(private readonly listingsService:ListingsService,@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {
constructor(private readonly listingsService:ListingsService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) {
}
@Get()
findAll(): any {
return this.listingsService.getAllBusinessListings();
this.logger.info(`start findAll Listing`);
return this.listingsService.findListings();
}
@Get(':id')
findById(@Param('id') id:string): any {
return this.listingsService.getBusinessListingById(id);
return this.listingsService.findById(id);
}
@Get('user/:userid')
findByUserId(@Param('userid') userid:string): any {
return this.listingsService.getBusinessListingByUserId(userid);
return this.listingsService.findByUserId(userid);
}
@Post('search')
find(@Body() criteria: any): any {
return this.listingsService.findBusinessListings(criteria);
find(@Body() criteria: ListingCriteria): any {
return this.listingsService.findByState(criteria.state);
}
/**
* @param listing creates a new listing
*/
@Post()
save(@Body() listing: any){
create(@Body() listing: any){
this.logger.info(`Save Listing`);
this.listingsService.saveListing(listing)
this.listingsService.createListing(listing)
}
@Put()
update(@Body() listing: any){
this.logger.info(`Save Listing`);
this.listingsService.updateListing(listing.id,listing)
}
/**
* @param id deletes a listing
*/
@Delete(':id')
deleteById(@Param('id') id:string){
this.listingsService.deleteBusinessListing(id)
this.listingsService.deleteListing(id)
}
@Delete('deleteAll')
deleteAll(){
this.listingsService.deleteAllBusinessListings()
}
// @Delete('deleteAll')
// deleteAll(){
// this.listingsService.deleteAllBusinessListings()
// }
}

View File

@@ -13,39 +13,39 @@ export class CommercialPropertyListingsController {
}
@Get(':id')
findById(@Param('id') id:string): any {
return this.listingsService.getCommercialPropertyListingById(id);
}
// @Get(':id')
// findById(@Param('id') id:string): any {
// return this.listingsService.getCommercialPropertyListingById(id);
// }
@Post('search')
find(@Body() criteria: any): any {
return this.listingsService.findCommercialPropertyListings(criteria);
}
// @Post('search')
// find(@Body() criteria: any): any {
// return this.listingsService.findCommercialPropertyListings(criteria);
// }
@Put('imageOrder/:id')
async changeImageOrder(@Param('id') id:string,@Body() imageOrder: ImageProperty[]) {
this.listingsService.updateImageOrder(id, imageOrder)
}
/**
* @param listing creates a new listing
*/
@Post()
save(@Body() listing: any){
this.logger.info(`Save Listing`);
this.listingsService.saveListing(listing)
}
// @Put('imageOrder/:id')
// async changeImageOrder(@Param('id') id:string,@Body() imageOrder: ImageProperty[]) {
// this.listingsService.updateImageOrder(id, imageOrder)
// }
// /**
// * @param listing creates a new listing
// */
// @Post()
// save(@Body() listing: any){
// this.logger.info(`Save Listing`);
// this.listingsService.saveListing(listing)
// }
/**
* @param id deletes a listing
*/
@Delete(':id')
deleteById(@Param('id') id:string){
this.listingsService.deleteCommercialPropertyListing(id)
}
@Delete('deleteAll')
deleteAll(){
this.listingsService.deleteAllcommercialListings()
}
// /**
// * @param id deletes a listing
// */
// @Delete(':id')
// deleteById(@Param('id') id:string){
// this.listingsService.deleteCommercialPropertyListing(id)
// }
// @Delete('deleteAll')
// deleteAll(){
// this.listingsService.deleteAllcommercialListings()
// }
}

View File

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Injectable, Module, OnModuleInit } from '@nestjs/common';
import { BusinessListingsController } from './business-listings.controller.js';
import { ListingsService } from './listings.service.js';
import { CommercialPropertyListingsController } from './commercial-property-listings.controller.js';
@@ -8,9 +8,15 @@ import { UnknownListingsController } from './unknown-listings.controller.js';
import { UserModule } from '../user/user.module.js';
import { BrokerListingsController } from './broker-listings.controller.js';
import { UserService } from '../user/user.service.js';
import { Client, Connection } from 'pg';
import { drizzle } from 'drizzle-orm/node-postgres';
import { businesses_json } from '../drizzle/businesses_json.model.js';
import { DrizzleModule } from '../drizzle/drizzle.module.js';
@Module({
imports: [RedisModule],
imports: [RedisModule,DrizzleModule
],
controllers: [BusinessListingsController, CommercialPropertyListingsController,UnknownListingsController,BrokerListingsController],
providers: [ListingsService,FileService,UserService],
exports: [ListingsService],

View File

@@ -10,177 +10,186 @@ import {
import { convertStringToNullUndefined } from '../utils.js';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { EntityData, EntityId, Repository, Schema, SchemaDefinition } from 'redis-om';
import { REDIS_CLIENT } from '../redis/redis.module.js';
import { EntityData, EntityId, Schema, SchemaDefinition } from 'redis-om';
import { eq, ilike, sql } from 'drizzle-orm';
import { BusinessesJson, PG_CONNECTION, businesses_json } from '../drizzle/schema.js';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import * as schema from '../drizzle/schema.js';
@Injectable()
export class ListingsService {
schemaNameBusiness:ListingCategory={name:'business'}
schemaNameCommercial:ListingCategory={name:'commercialProperty'}
businessListingRepository:Repository;
commercialPropertyListingRepository:Repository;
baseListingSchemaDef : SchemaDefinition = {
id: { type: 'string' },
userId: { type: 'string' },
listingsCategory: { type: 'string' },
title: { type: 'string' },
description: { type: 'string' },
country: { type: 'string' },
state:{ type: 'string' },
city:{ type: 'string' },
zipCode: { type: 'number' },
type: { type: 'string' },
price: { type: 'number' },
favoritesForUser:{ type: 'string[]' },
hideImage:{ type: 'boolean' },
draft:{ type: 'boolean' },
created:{ type: 'date' },
updated:{ type: 'date' }
}
businessListingSchemaDef : SchemaDefinition = {
...this.baseListingSchemaDef,
salesRevenue: { type: 'number' },
cashFlow: { type: 'number' },
employees: { type: 'number' },
established: { type: 'number' },
internalListingNumber: { type: 'number' },
realEstateIncluded:{ type: 'boolean' },
leasedLocation:{ type: 'boolean' },
franchiseResale:{ type: 'boolean' },
supportAndTraining: { type: 'string' },
reasonForSale: { type: 'string' },
brokerLicencing: { type: 'string' },
internals: { type: 'string' },
}
commercialPropertyListingSchemaDef : SchemaDefinition = {
...this.baseListingSchemaDef,
imageNames:{ type: 'string[]' },
}
businessListingSchema = new Schema(this.schemaNameBusiness.name,this.businessListingSchemaDef, {
dataStructure: 'JSON'
})
commercialPropertyListingSchema = new Schema(this.schemaNameCommercial.name,this.commercialPropertyListingSchemaDef, {
dataStructure: 'JSON'
})
constructor(@Inject(REDIS_CLIENT) private readonly redis: any, @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger){
this.businessListingRepository = new Repository(this.businessListingSchema, redis);
this.commercialPropertyListingRepository = new Repository(this.commercialPropertyListingSchema, redis)
this.businessListingRepository.createIndex();
this.commercialPropertyListingRepository.createIndex();
}
async saveListing(listing: BusinessListing | CommercialPropertyListing) {
const repo=listing.listingsCategory==='business'?this.businessListingRepository:this.commercialPropertyListingRepository;
let result
if (listing.id){
result = await repo.save(listing.id,listing as any)
} else {
result = await repo.save(listing as any)
listing.id=result[EntityId];
result = await repo.save(listing.id,listing as any)
}
return result;
}
async getCommercialPropertyListingById(id: string): Promise<CommercialPropertyListing>{
return await this.commercialPropertyListingRepository.fetch(id) as unknown as CommercialPropertyListing;
}
async getBusinessListingById(id: string) {
return await this.businessListingRepository.fetch(id)
}
async getBusinessListingByUserId(userid:string){
return await this.businessListingRepository.search().where('userId').equals(userid).return.all()
}
async deleteBusinessListing(id: string){
return await this.businessListingRepository.remove(id);
}
async deleteCommercialPropertyListing(id: string){
return await this.commercialPropertyListingRepository.remove(id);
}
async getAllBusinessListings(start?: number, end?: number) {
return await this.businessListingRepository.search().return.all()
}
async getAllCommercialListings(start?: number, end?: number) {
return await this.commercialPropertyListingRepository.search().return.all()
}
async findBusinessListings(criteria:ListingCriteria): Promise<any> {
// let listings = await this.getAllBusinessListings();
// return this.find(criteria,listings);
this.logger.info(`start findBusinessListings: ${JSON.stringify(criteria)}`);
const result = await this.redis.ft.search('business:index','*',{LIMIT:{from:0,size:50}});
this.logger.info(`start findBusinessListings: ${JSON.stringify(criteria)}`);
return result.documents;
}
async findCommercialPropertyListings(criteria:ListingCriteria): Promise<any> {
let listings = await this.getAllCommercialListings();
return this.find(criteria,listings);
}
async deleteAllBusinessListings(){
const ids = await this.getIdsForRepo(this.schemaNameBusiness.name);
this.businessListingRepository.remove(ids);
}
async deleteAllcommercialListings(){
const ids = await this.getIdsForRepo(this.schemaNameCommercial.name);
this.commercialPropertyListingRepository.remove(ids);
}
async getIdsForRepo(repoName:string, maxcount=100000){
let cursor = 0;
let ids = [];
do {
const reply = await this.redis.scan(cursor, {
MATCH: `${repoName}:*`,
COUNT: maxcount
});
cursor = reply.cursor;
// Extrahiere die ID aus jedem Schlüssel und füge sie zur Liste hinzu
ids = ids.concat(reply.keys.map(key => key.split(':')[1]).filter(id=>id!='index'));
} while (cursor !== 0);
return ids;
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,){
// this.businessListingRepository = new Repository(this.businessListingSchema, redis);
// this.commercialPropertyListingRepository = new Repository(this.commercialPropertyListingSchema, redis)
// this.businessListingRepository.createIndex();
// this.commercialPropertyListingRepository.createIndex();
}
async find(criteria:ListingCriteria, listings: any[]): Promise<any> {
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.state)){
console.log(criteria.state);
listings=listings.filter(l=>l.state===criteria.state);
}
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
// ##############################################################
// ##############################################################
async createListing(newListing: { id: string; data: BusinessesJson }): Promise<BusinessesJson> {
const [createdListing] = await this.conn.insert(businesses_json).values(newListing).returning();
return createdListing as BusinessesJson;
}
async updateImageOrder(id:string,imageOrder: ImageProperty[]){
const listing = await this.getCommercialPropertyListingById(id) as unknown as CommercialPropertyListing
listing.imageOrder=imageOrder;
this.saveListing(listing);
async updateListing(id: string, data: BusinessListing): Promise<BusinessesJson> {
const [updateListing] = await this.conn.update(businesses_json).set(data).where(eq(businesses_json.id, id)).returning();
return updateListing as BusinessesJson;
}
async deleteImage(listingid:string,name:string,){
const listing = await this.getCommercialPropertyListingById(listingid) as unknown as CommercialPropertyListing
const index = listing.imageOrder.findIndex(im=>im.name===name);
if (index>-1){
listing.imageOrder.splice(index,1);
this.saveListing(listing);
}
async deleteListing(id: string): Promise<void> {
await this.conn.delete(businesses_json).where(eq(businesses_json.id, id));
}
async addImage(id:string,imagename: string){
const listing = await this.getCommercialPropertyListingById(id) as unknown as CommercialPropertyListing
listing.imageOrder.push({name:imagename,code:'',id:''});
this.saveListing(listing);
async findByPriceRange(minPrice: number, maxPrice: number): Promise<BusinessesJson[]> {
return this.conn.select().from(businesses_json).where(sql`${businesses_json.data}->>'price' BETWEEN ${minPrice} AND ${maxPrice}`);
}
async findByState(state: string): Promise<BusinessesJson[]> {
return this.conn.select().from(businesses_json).where(sql`${businesses_json.data}->>'state' = ${state}`);
}
async findById(id: string): Promise<BusinessesJson[]> {
return this.conn.select().from(businesses_json).where(sql`${businesses_json.id} = ${id}`);
}
async findByUserId(userId: string): Promise<BusinessesJson[]> {
return this.conn.select().from(businesses_json).where(sql`${businesses_json.data}->>'userId' = ${userId}`);
}
// async findByTitleContains(title: string): Promise<BusinessesJson[]> {
// return this.conn.select().from(businesses_json).where(ilike(sql`${businesses_json.data}->>'title'`, `%${title}%`));
// }
async findByTitleContains(title: string): Promise<BusinessesJson[]> {
return this.conn.select().from(businesses_json).where(sql`${businesses_json.data}->>'title' ILIKE '%' || ${title} || '%'`);
}
async findListings(start = 0, size = 48): Promise<{ data: Record<string, any>[]; total: number }> {
// return this.conn.select({ data: businesses_json.data }).from(businesses_json).offset(start).limit(size);
const [data, total] = await Promise.all([
this.conn.select({ data: businesses_json.data }).from(businesses_json).offset(start).limit(size),
this.conn.select({ count: sql`count(*)` }).from(businesses_json).then((result) => Number(result[0].count)),
]);
return { data, total };
}
// ##############################################################
// ##############################################################
// async saveListing(listing: BusinessListing | CommercialPropertyListing) {
// const repo=listing.listingsCategory==='business'?this.businessListingRepository:this.commercialPropertyListingRepository;
// let result
// if (listing.id){
// result = await repo.save(listing.id,listing as any)
// } else {
// result = await repo.save(listing as any)
// listing.id=result[EntityId];
// result = await repo.save(listing.id,listing as any)
// }
// return result;
// }
// async getCommercialPropertyListingById(id: string): Promise<CommercialPropertyListing>{
// return await this.commercialPropertyListingRepository.fetch(id) as unknown as CommercialPropertyListing;
// }
// async getBusinessListingById(id: string) {
// return await this.businessListingRepository.fetch(id)
// }
// async getBusinessListingByUserId(userid:string){
// return await this.businessListingRepository.search().where('userId').equals(userid).return.all()
// }
// async deleteBusinessListing(id: string){
// return await this.businessListingRepository.remove(id);
// }
// async deleteCommercialPropertyListing(id: string){
// return await this.commercialPropertyListingRepository.remove(id);
// }
// async getAllBusinessListings(start?: number, end?: number) {
// return await this.businessListingRepository.search().return.all()
// }
// async getAllCommercialListings(start?: number, end?: number) {
// return await this.commercialPropertyListingRepository.search().return.all()
// }
// async findBusinessListings(criteria:ListingCriteria): Promise<any> {
// // let listings = await this.getAllBusinessListings();
// // return this.find(criteria,listings);
// const from=criteria.start?criteria.start:0
// const size=criteria.length?criteria.length:24
// this.logger.info(`start findBusinessListings: ${JSON.stringify(criteria)}`);
// const result = await this.redis.ft.search('business:index','*',{LIMIT:{from,size}});
// this.logger.info(`start findBusinessListings: ${JSON.stringify(criteria)}`);
// return result
// }
// async findCommercialPropertyListings(criteria:ListingCriteria): Promise<any> {
// let listings = await this.getAllCommercialListings();
// return this.find(criteria,listings);
// }
// async deleteAllBusinessListings(){
// const ids = await this.getIdsForRepo(this.schemaNameBusiness.name);
// this.businessListingRepository.remove(ids);
// }
// async deleteAllcommercialListings(){
// const ids = await this.getIdsForRepo(this.schemaNameCommercial.name);
// this.commercialPropertyListingRepository.remove(ids);
// }
// async getIdsForRepo(repoName:string, maxcount=100000){
// let cursor = 0;
// let ids = [];
// do {
// const reply = await this.redis.scan(cursor, {
// MATCH: `${repoName}:*`,
// COUNT: maxcount
// });
// cursor = reply.cursor;
// // Extrahiere die ID aus jedem Schlüssel und füge sie zur Liste hinzu
// ids = ids.concat(reply.keys.map(key => key.split(':')[1]).filter(id=>id!='index'));
// } while (cursor !== 0);
// return ids;
// }
// async find(criteria:ListingCriteria, listings: any[]): Promise<any> {
// 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.state)){
// console.log(criteria.state);
// listings=listings.filter(l=>l.state===criteria.state);
// }
// 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
// }
// async updateImageOrder(id:string,imageOrder: ImageProperty[]){
// const listing = await this.getCommercialPropertyListingById(id) as unknown as CommercialPropertyListing
// listing.imageOrder=imageOrder;
// this.saveListing(listing);
// }
// async deleteImage(listingid:string,name:string,){
// const listing = await this.getCommercialPropertyListingById(listingid) as unknown as CommercialPropertyListing
// const index = listing.imageOrder.findIndex(im=>im.name===name);
// if (index>-1){
// listing.imageOrder.splice(index,1);
// this.saveListing(listing);
// }
// }
// async addImage(id:string,imagename: string){
// const listing = await this.getCommercialPropertyListingById(id) as unknown as CommercialPropertyListing
// listing.imageOrder.push({name:imagename,code:'',id:''});
// this.saveListing(listing);
// }
}

View File

@@ -12,18 +12,18 @@ export class UnknownListingsController {
}
@Get(':id')
async findById(@Param('id') id:string): Promise<any> {
const result = await this.listingsService.getBusinessListingById(id);
if (result.id){
return result
} else {
return await this.listingsService.getCommercialPropertyListingById(id);
}
}
@Get('repo/:repo')
async getAllByRepo(@Param('repo') repo:string): Promise<any> {
return await this.listingsService.getIdsForRepo(repo);
}
// @Get(':id')
// async findById(@Param('id') id:string): Promise<any> {
// const result = await this.listingsService.getBusinessListingById(id);
// if (result.id){
// return result
// } else {
// return await this.listingsService.getCommercialPropertyListingById(id);
// }
// }
// @Get('repo/:repo')
// async getAllByRepo(@Param('repo') repo:string): Promise<any> {
// return await this.listingsService.getIdsForRepo(repo);
// }
}