Umbau auf postgres 2. step
This commit is contained in:
@@ -4,42 +4,45 @@ import { Entity, Repository, Schema } from 'redis-om';
|
||||
import { ListingCriteria } from '../models/main.model.js';
|
||||
import { REDIS_CLIENT } from '../redis/redis.module.js';
|
||||
import { FileService } from '../file/file.service.js';
|
||||
import { User } from 'src/drizzle/schema.js';
|
||||
import { User } from 'src/models/db.model.js';
|
||||
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
||||
import { Logger } from 'winston';
|
||||
import { PG_CONNECTION } from 'src/drizzle/schema.js';
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres/driver.js';
|
||||
import * as schema from '../drizzle/schema.js';
|
||||
import { eq, sql,and } from 'drizzle-orm';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
userRepository:Repository;
|
||||
userSchema = new Schema('user',{
|
||||
id: { type: 'string' },
|
||||
firstname: { type: 'string' },
|
||||
lastname: { type: 'string' },
|
||||
email: { type: 'string' },
|
||||
phoneNumber: { type: 'string' },
|
||||
companyOverview:{ type: 'string' },
|
||||
companyWebsite:{ type: 'string' },
|
||||
companyLocation:{ type: 'string' },
|
||||
offeredServices:{ type: 'string' },
|
||||
areasServed:{ type: 'string[]' },
|
||||
names:{ type: 'string[]', path:'$.licensedIn.name' },
|
||||
values:{ type: 'string[]', path:'$.licensedIn.value' }
|
||||
}, {
|
||||
dataStructure: 'JSON'
|
||||
})
|
||||
constructor(private fileService:FileService){
|
||||
// this.userRepository = new Repository(this.userSchema, redis)
|
||||
// this.userRepository.createIndex();
|
||||
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
|
||||
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,private fileService:FileService) {
|
||||
}
|
||||
private getConditions(criteria: ListingCriteria): any[] {
|
||||
const conditions = [];
|
||||
if (criteria.state) {
|
||||
conditions.push();
|
||||
}
|
||||
return conditions;
|
||||
}
|
||||
async getUserById( id:string){
|
||||
const user = await this.userRepository.fetch(id) as User;
|
||||
const users = await this.conn.select().from(schema.users).where(sql`id = ${id}`) as User[]
|
||||
const user = users[0]
|
||||
user.hasCompanyLogo=this.fileService.hasCompanyLogo(id);
|
||||
user.hasProfile=this.fileService.hasProfile(id);
|
||||
return user;
|
||||
}
|
||||
async saveUser(user:any):Promise<User>{
|
||||
return await this.userRepository.save(user.id,user) as User
|
||||
if (user.id){
|
||||
const [updateUser] = await this.conn.update(schema.users).set(user).where(eq(schema.users.id, user.id)).returning();
|
||||
return updateUser as User;
|
||||
} else {
|
||||
const [newUser] = await this.conn.insert(schema.users).values(user).returning();
|
||||
return newUser as User;
|
||||
}
|
||||
}
|
||||
async findUser(criteria:ListingCriteria){
|
||||
return await this.userRepository.search().return.all();
|
||||
const users = await this.conn.execute(sql`SELECT * FROM users WHERE EXISTS (SELECT 1 FROM unnest(users."areasServed") AS area WHERE area LIKE '%' || ${criteria.state} || '%')`)
|
||||
return users.rows
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user