Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

---- typeorm example ----- #133

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 86 additions & 56 deletions apps/core-rest-api/src/app/adapters/controllers/api/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,111 @@ import { AuthModule } from '../../auth/auth.module';
import { CryptographyModule } from '../../cryptography/cryptography.module';
import { envSchema } from '../../env/env';
import { EnvModule } from '../../env/env.module';
import { UpdatePsychologistController } from './use-cases/psychologist/update-psychologist/update-psychologist.controller';
// import { UpdatePsychologistController } from './use-cases/psychologist/update-psychologist/update-psychologist.controller';

import { BcryptHasherService } from '../../../core/shared/cryptography/use-cases/bcrypt-hasher.service';
import { PostgreSqlPrismaOrmService } from '../../database/infra/prisma/prisma.service';
// import { BcryptHasherService } from '../../../core/shared/cryptography/use-cases/bcrypt-hasher.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Appointment } from '../../database/infra/typeorm/entities/Appointment';
import { Clinic } from '../../database/infra/typeorm/entities/Clinic';
import { Patient } from '../../database/infra/typeorm/entities/Patient';
import { PatientAppointmentRegistry } from '../../database/infra/typeorm/entities/PatientAppointmentRegistry';
import { Psychologist } from '../../database/infra/typeorm/entities/psychologist.entitie';
import { PostgresTypeOrmRepository } from '../../database/repositories/psychologist/teste';
import { DatabaseRepositoriesModule } from '../../database/repositories/repositories.module';
import { TestetController } from './use-cases/teste-controller';

import { CreateClinicController } from './use-cases/clinic/create-clinic/create-clinic.controller';
import { DeleteClinicController } from './use-cases/clinic/delete-clinic/delete-clinic.controller';
import { UpdateClinicController } from './use-cases/clinic/update-clinic/update-clinic.controller';
import { CreatePatientController } from './use-cases/patient/create-patient/create-patient.controller';
import { DeletePatientController } from './use-cases/patient/delete-patient/delete-patient.controller';
import { AuthenticatePsychologistController } from './use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.controller';
import { CreatePsychologistController } from './use-cases/psychologist/create-psychologist/create-psychologist.controller';
import { DeletePsychologistController } from './use-cases/psychologist/delete-psychologist/delete-psychologist.controller';
// import { CreateClinicController } from './use-cases/clinic/create-clinic/create-clinic.controller';
// import { DeleteClinicController } from './use-cases/clinic/delete-clinic/delete-clinic.controller';
// import { UpdateClinicController } from './use-cases/clinic/update-clinic/update-clinic.controller';
// import { CreatePatientController } from './use-cases/patient/create-patient/create-patient.controller';
// import { DeletePatientController } from './use-cases/patient/delete-patient/delete-patient.controller';
// import { AuthenticatePsychologistController } from './use-cases/psychologist/authenticate-psychologist/authenticate-psychologist.controller';
// import { CreatePsychologistController } from './use-cases/psychologist/create-psychologist/create-psychologist.controller';
// import { DeletePsychologistController } from './use-cases/psychologist/delete-psychologist/delete-psychologist.controller';

import { CreateAppointmentController } from './use-cases/appointment/create-appointment/create-appointment.controller';
import { NestjsCreateAppointmentService } from './use-cases/appointment/create-appointment/nestjs-create-appointment.service';
import { NestjsCreateClinicService } from './use-cases/clinic/create-clinic/nestjs-create-clinic.service';
import { NestjsDeleteClinicService } from './use-cases/clinic/delete-clinic/nestjs-delete-clinic.service';
import { NestjsUpdateClinicService } from './use-cases/clinic/update-clinic/nestjs-update-clinic.service';
import { CreatePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/create-patient-appointment-registry/create-patient-appointment-registry.controller';
import { NestjsCreatePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/create-patient-appointment-registry/nestjs-create-patient-appointment-registry.service';
import { DeletePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/delete-patient-appointment-registry/delete-patient-appointment-registry.controller';
import { NestjsDeletePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/delete-patient-appointment-registry/nestjs-delete-patient-appointment-registry.service';
import { NestjsUpdatePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/update-patient-appointment-registry/nestjs-update-patient-appointment-registry.service';
import { UpdatePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/update-patient-appointment-registry/update-patient-appointment-registry.controller';
import { NestjsCreatePatientService } from './use-cases/patient/create-patient/nestjs-create-patient.service';
import { NestjsDeletePatientService } from './use-cases/patient/delete-patient/nestjs-delete-patient.service';
import { NestjsAuthenticatePsychologistService } from './use-cases/psychologist/authenticate-psychologist/nestjs-authenticate-psychologist.service';
import { NestjsCreatePsychologistService } from './use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service';
import { NestjsDeletePsychologistService } from './use-cases/psychologist/delete-psychologist/nestjs-delete-psychologist.service';
import { NestjsUpdatePsychologistService } from './use-cases/psychologist/update-psychologist/nestjs-update-psychologist.service';
// import { typeOrmService } from '../../database/infra/typeorm/typeorm.service';
// import { CreateAppointmentController } from './use-cases/appointment/create-appointment/create-appointment.controller';
// import { NestjsCreateAppointmentService } from './use-cases/appointment/create-appointment/nestjs-create-appointment.service';
// import { NestjsCreateClinicService } from './use-cases/clinic/create-clinic/nestjs-create-clinic.service';
// import { NestjsDeleteClinicService } from './use-cases/clinic/delete-clinic/nestjs-delete-clinic.service';
// import { NestjsUpdateClinicService } from './use-cases/clinic/update-clinic/nestjs-update-clinic.service';
// import { CreatePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/create-patient-appointment-registry/create-patient-appointment-registry.controller';
// import { NestjsCreatePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/create-patient-appointment-registry/nestjs-create-patient-appointment-registry.service';
// import { DeletePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/delete-patient-appointment-registry/delete-patient-appointment-registry.controller';
// import { NestjsDeletePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/delete-patient-appointment-registry/nestjs-delete-patient-appointment-registry.service';
// import { NestjsUpdatePatientAppointmentRegistryService } from './use-cases/patient-appointment-registry/update-patient-appointment-registry/nestjs-update-patient-appointment-registry.service';
// import { UpdatePatientAppointmentRegistryController } from './use-cases/patient-appointment-registry/update-patient-appointment-registry/update-patient-appointment-registry.controller';
// import { NestjsCreatePatientService } from './use-cases/patient/create-patient/nestjs-create-patient.service';
// import { NestjsDeletePatientService } from './use-cases/patient/delete-patient/nestjs-delete-patient.service';
// import { NestjsAuthenticatePsychologistService } from './use-cases/psychologist/authenticate-psychologist/nestjs-authenticate-psychologist.service';
// import { NestjsCreatePsychologistService } from './use-cases/psychologist/create-psychologist/nestjs-create-psychologist.service';
// import { NestjsDeletePsychologistService } from './use-cases/psychologist/delete-psychologist/nestjs-delete-psychologist.service';
// import { NestjsUpdatePsychologistService } from './use-cases/psychologist/update-psychologist/nestjs-update-psychologist.service';

@Module({
imports: [
ConfigModule.forRoot({
validate: (env) => envSchema.parse(env),
isGlobal: true,
}),
TypeOrmModule.forFeature([
Psychologist,
Appointment,
Clinic,
Patient,
PatientAppointmentRegistry,
]),
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'cliniccontrol',
password: 'cliniccontrolpassword',
database: 'core-rest-api',
entities: [__dirname + '../../database/infra/typeorm/entities/*.{js,ts}'],
synchronize: false,
autoLoadEntities: true,
}),
DatabaseRepositoriesModule,
EnvModule,
AuthModule,
CryptographyModule,
],
controllers: [
AuthenticatePsychologistController,
CreatePsychologistController,
UpdatePsychologistController,
DeletePsychologistController,
CreateClinicController,
UpdateClinicController,
DeleteClinicController,
CreatePatientController,
DeletePatientController,
CreatePatientAppointmentRegistryController,
CreateAppointmentController,
DeletePatientAppointmentRegistryController,
UpdatePatientAppointmentRegistryController
TestetController,
// UpdatePsychologistController,
// AuthenticatePsychologistController,
// CreatePsychologistController,
// UpdatePsychologistController,
// DeletePsychologistController,
// CreateClinicController,
// UpdateClinicController,
// DeleteClinicController,
// CreatePatientController,
// DeletePatientController,
// CreatePatientAppointmentRegistryController,
// CreateAppointmentController,
// DeletePatientAppointmentRegistryController,
// UpdatePatientAppointmentRegistryController,
],
providers: [
BcryptHasherService,
PostgreSqlPrismaOrmService,
NestjsAuthenticatePsychologistService,
NestjsCreatePsychologistService,
NestjsUpdatePsychologistService,
NestjsDeletePsychologistService,
NestjsCreateClinicService,
NestjsUpdateClinicService,
NestjsDeleteClinicService,
NestjsCreatePatientService,
NestjsDeletePatientService,
NestjsCreatePatientAppointmentRegistryService,
NestjsCreateAppointmentService,
NestjsDeletePatientAppointmentRegistryService,
NestjsUpdatePatientAppointmentRegistryService
PostgresTypeOrmRepository,
// BcryptHasherService,
// // PostgreSqlPrismaOrmService,
// NestjsAuthenticatePsychologistService,
// NestjsCreatePsychologistService,
// NestjsUpdatePsychologistService,
// NestjsDeletePsychologistService,
// NestjsCreateClinicService,
// NestjsUpdateClinicService,
// NestjsDeleteClinicService,
// NestjsCreatePatientService,
// NestjsDeletePatientService,
// NestjsCreatePatientAppointmentRegistryService,
// NestjsCreateAppointmentService,
// NestjsDeletePatientAppointmentRegistryService,
// NestjsUpdatePatientAppointmentRegistryService,
],
exports: [],
})
export class ApiModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Get } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Public } from '../../../auth/public';

import { GlobalAppHttpException } from '../../../../shared/errors/globalAppHttpException';
import { PostgresTypeOrmRepository } from '../../../database/repositories/psychologist/teste';

@ApiTags('teste')
@ApiBearerAuth()
@Controller({
path: 'teste',
})
export class TestetController {
constructor(private postgresTypeOrmRepository: PostgresTypeOrmRepository) {}

@Get()
@Public()
async execute() {
try {
const teste = await this.postgresTypeOrmRepository.getDataSource();
return teste;
} catch (error: unknown) {
console.log(error);
throw new GlobalAppHttpException(error);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { PaymentMethod } from '../../../../../core/shared/interfaces/payments';
import { Clinic } from './Clinic';
import { Patient } from './Patient';
import { Psychologist } from './psychologist.entitie';

@Index('appointment_pkey', ['id'], { unique: true })
@Entity('appointment', { schema: 'public' })
export class Appointment {
@Column('text', { primary: true, name: 'id' })
id!: string;

@Column('timestamp with time zone', { name: 'date' })
date!: Date;

@Column('boolean', { name: 'online', default: () => 'false' })
online!: boolean;

@Column('boolean', { name: 'confirmed', default: () => 'false' })
confirmed!: boolean;

@Column('timestamp with time zone', { name: 'confirmation_date', nullable: true })
confirmationDate!: Date | null;

@Column('boolean', { name: 'cancelled', default: () => 'false' })
cancelled!: boolean;

@Column('timestamp with time zone', { name: 'cancellation_date', nullable: true })
cancellationDate!: Date | null;

@Column('boolean', { name: 'done', default: () => 'false' })
done!: boolean;

@Column('boolean', { name: 'missed', nullable: true })
missed!: boolean | null;

@Column('boolean', { name: 'paid', nullable: true, default: () => 'false' })
paid!: boolean | null;

@Column('enum', {
name: 'payment_method',
enum: PaymentMethod,
default: () => PaymentMethod.HEALTH_INSURANCE,
})
paymentMethod!: PaymentMethod;

@Column('timestamp with time zone', {
name: 'created_at',
default: () => 'CURRENT_TIMESTAMP',
})
createdAt!: Date;

@Column('timestamp with time zone', {
name: 'updated_at',
default: () => 'CURRENT_TIMESTAMP',
})
updatedAt!: Date;

@ManyToOne(() => Clinic, (clinic) => clinic.appointments, {
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
})
@JoinColumn([{ name: 'clinic_id', referencedColumnName: 'id' }])
clinic!: Clinic;

@ManyToOne(() => Patient, (patient) => patient.appointments, {
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
})
@JoinColumn([{ name: 'patient_id', referencedColumnName: 'id' }])
patient!: Patient;

@ManyToOne(() => Psychologist, (psychologist) => psychologist.appointments, {
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
})
@JoinColumn([{ name: 'psychologist_id', referencedColumnName: 'id' }])
psychologist!: Psychologist;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { Appointment } from './Appointment';
import { Patient } from './Patient';
import { Psychologist } from './psychologist.entitie';

@Index('clinic_pkey', ['id'], { unique: true })
@Entity('clinic', { schema: 'public' })
export class Clinic {
@Column('text', { primary: true, name: 'id' })
id!: string;

@Column('text', { name: 'name' })
name!: string;

@Column('text', { name: 'address', nullable: true })
address!: string | null;

@Column('text', { name: 'city' })
city!: string;

@Column('text', { name: 'state' })
state!: string;

@Column('timestamp with time zone', {
name: 'created_at',
default: () => 'CURRENT_TIMESTAMP',
})
createdAt!: Date;

@Column('timestamp with time zone', {
name: 'updated_at',
default: () => 'CURRENT_TIMESTAMP',
})
updatedAt!: Date;

@OneToMany(() => Appointment, (appointment) => appointment.clinic)
appointments!: Appointment[];

@ManyToOne(() => Psychologist, (psychologist) => psychologist.clinics, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
@JoinColumn([{ name: 'psychologist_id', referencedColumnName: 'id' }])
psychologist!: Psychologist;

@OneToMany(() => Patient, (patient) => patient.clinic)
patients!: Patient[];
}
Loading
Loading