mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
data persistance init
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { Column, Entity, OneToMany, PrimaryColumn } from 'typeorm';
|
||||
|
||||
import { ClientStatisticsEntity } from '../client-statistics/client-statistics.entity';
|
||||
import { TrackedEntity } from '../utils/TrackedEntity.entity';
|
||||
|
||||
@Entity()
|
||||
export class ClientEntity extends TrackedEntity {
|
||||
@PrimaryColumn({ length: 8, type: 'varchar' })
|
||||
id: string;
|
||||
|
||||
@Column({ type: 'datetime' })
|
||||
startTime: Date;
|
||||
|
||||
@Column()
|
||||
clientName: string;
|
||||
|
||||
@Column({ length: 62, type: 'varchar' })
|
||||
address: string;
|
||||
|
||||
@Column({ default: 0 })
|
||||
bestDifficulty: number
|
||||
|
||||
@OneToMany(
|
||||
() => ClientStatisticsEntity,
|
||||
clientStatistic => clientStatistic.client
|
||||
)
|
||||
clientStatistics?: ClientStatisticsEntity[];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { ClientEntity } from './client.entity';
|
||||
import { ClientService } from './client.service';
|
||||
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([ClientEntity])],
|
||||
providers: [ClientService],
|
||||
exports: [TypeOrmModule, ClientService],
|
||||
})
|
||||
export class ClientModule { }
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { ClientEntity } from './client.entity';
|
||||
|
||||
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class ClientService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(ClientEntity)
|
||||
private clientRepository: Repository<ClientEntity>
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public async save(client: Partial<ClientEntity>) {
|
||||
return await this.clientRepository.save(client);
|
||||
}
|
||||
|
||||
public async delete(id: string) {
|
||||
return await this.clientRepository.softDelete({ id });
|
||||
}
|
||||
|
||||
public async updateBestDifficulty(id: string, bestDifficulty: number) {
|
||||
return await this.clientRepository.update(id, { bestDifficulty });
|
||||
}
|
||||
public async connectedClientCount(): Promise<number> {
|
||||
return await this.clientRepository.count();
|
||||
}
|
||||
|
||||
public async getByAddress(address: string): Promise<ClientEntity[]> {
|
||||
return await this.clientRepository.find({
|
||||
where: {
|
||||
address
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public async getByAddressAndName(address: string, id: string): Promise<ClientEntity> {
|
||||
return await this.clientRepository.findOne({
|
||||
where: {
|
||||
address,
|
||||
id
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user