260326:1347 Fixing Refactor ADR-019 Naming convention uuid #01
This commit is contained in:
@@ -1,36 +1,15 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
PrimaryGeneratedColumn,
|
||||
OneToMany,
|
||||
BeforeInsert,
|
||||
} from 'typeorm';
|
||||
import { v7 as uuidv7 } from 'uuid';
|
||||
import { Exclude, Expose } from 'class-transformer';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
|
||||
import { Exclude } from 'class-transformer';
|
||||
import { UuidBaseEntity } from '../../../common/entities/uuid-base.entity';
|
||||
import { Contract } from '../../contract/entities/contract.entity';
|
||||
|
||||
@Entity('projects')
|
||||
export class Project extends BaseEntity {
|
||||
export class Project extends UuidBaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
@Exclude()
|
||||
id!: number;
|
||||
|
||||
@Expose({ name: 'id' })
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
unique: true,
|
||||
nullable: false,
|
||||
comment: 'UUID Public Identifier (ADR-019)',
|
||||
})
|
||||
uuid!: string;
|
||||
|
||||
@BeforeInsert()
|
||||
generateUuid(): void {
|
||||
if (!this.uuid) {
|
||||
this.uuid = uuidv7();
|
||||
}
|
||||
}
|
||||
// publicId inherited from UuidBaseEntity (DB column: uuid)
|
||||
|
||||
@Column({ name: 'project_code', unique: true, length: 50 })
|
||||
projectCode!: string;
|
||||
|
||||
@@ -91,21 +91,23 @@ export class ProjectService {
|
||||
return project;
|
||||
}
|
||||
|
||||
async findOneByUuid(uuid: string) {
|
||||
async findOneByUuid(publicId: string) {
|
||||
const project = await this.projectRepository.findOne({
|
||||
where: { uuid },
|
||||
where: { publicId },
|
||||
relations: ['contracts'],
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
throw new NotFoundException(`Project UUID ${uuid} not found`);
|
||||
throw new NotFoundException(
|
||||
`Project with publicId ${publicId} not found`
|
||||
);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
async update(uuid: string, updateDto: UpdateProjectDto) {
|
||||
const project = await this.findOneByUuid(uuid);
|
||||
async update(publicId: string, updateDto: UpdateProjectDto) {
|
||||
const project = await this.findOneByUuid(publicId);
|
||||
|
||||
// Merge ข้อมูลใหม่ใส่ข้อมูลเดิม
|
||||
this.projectRepository.merge(project, updateDto);
|
||||
@@ -113,14 +115,14 @@ export class ProjectService {
|
||||
return this.projectRepository.save(project);
|
||||
}
|
||||
|
||||
async remove(uuid: string) {
|
||||
const project = await this.findOneByUuid(uuid);
|
||||
async remove(publicId: string) {
|
||||
const project = await this.findOneByUuid(publicId);
|
||||
// ใช้ Soft Delete
|
||||
return this.projectRepository.softRemove(project);
|
||||
}
|
||||
|
||||
async findContracts(uuid: string) {
|
||||
const project = await this.findOneByUuid(uuid);
|
||||
async findContracts(publicId: string) {
|
||||
const project = await this.findOneByUuid(publicId);
|
||||
return project.contracts;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user