Start Umbau zu postgres
This commit is contained in:
@@ -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);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user