init
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AdminService } from './admin.service';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Admin } from './entities/user.entity';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
|
||||
@Module({
|
||||
providers: [AdminService],
|
||||
controllers: [],
|
||||
imports: [MikroOrmModule.forFeature([Admin]), JwtModule],
|
||||
exports: [AdminService],
|
||||
})
|
||||
export class AdminModule {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Admin } from './entities/user.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(
|
||||
@InjectRepository(Admin)
|
||||
private readonly adminRepository: EntityRepository<Admin>,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async findByPhone(phone: string): Promise<Admin | null> {
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
async findById(id: number): Promise<Admin | null> {
|
||||
return this.adminRepository.findOne({ id });
|
||||
}
|
||||
|
||||
async create(phone: string): Promise<Admin> {
|
||||
const user = this.adminRepository.create({ phone });
|
||||
await this.em.persistAndFlush(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Entity, PrimaryKey, Property, OptionalProps } from '@mikro-orm/core';
|
||||
|
||||
@Entity({ tableName: 'admns' })
|
||||
export class Admin {
|
||||
[OptionalProps]?: 'id' | 'createdAt' | 'updatedAt';
|
||||
|
||||
@PrimaryKey()
|
||||
id!: number;
|
||||
|
||||
@Property({ nullable: true })
|
||||
firstName?: string;
|
||||
|
||||
@Property({ nullable: true })
|
||||
lastName?: string;
|
||||
|
||||
@Property({ unique: true })
|
||||
phone!: string;
|
||||
|
||||
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
|
||||
createdAt: Date = new Date();
|
||||
|
||||
@Property({ onUpdate: () => new Date(), defaultRaw: 'now()', columnType: 'timestamptz' })
|
||||
updatedAt: Date = new Date();
|
||||
}
|
||||
Reference in New Issue
Block a user