Backend half
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
import { ICompanyRepository } from '../../../Repositories/interfaces/ICompanyRepository';
|
||||
import { CompanyResponseDto, CompanyBasicDto } from '../../../Database/dto/company.dto';
|
||||
import {
|
||||
GetCompanyByIdQuery,
|
||||
GetCompanyByNameQuery,
|
||||
GetAllCompaniesQuery,
|
||||
GetBasicCompanyByIdQuery,
|
||||
GetAllBasicCompaniesQuery
|
||||
} from './CompanyQuery';
|
||||
import { appLogger } from '../../../utils/logger';
|
||||
|
||||
export class CompanyQueryHandler {
|
||||
constructor(private readonly companyRepository: ICompanyRepository) {
|
||||
appLogger.info('CompanyQueryHandler initialized');
|
||||
}
|
||||
|
||||
// Get company by ID
|
||||
async handleGetById(query: GetCompanyByIdQuery): Promise<CompanyResponseDto | null> {
|
||||
try {
|
||||
appLogger.info('Getting company by ID', {
|
||||
companyId: query.id,
|
||||
action: 'get_company_by_id'
|
||||
});
|
||||
|
||||
// Validate ID
|
||||
if (!query.id || query.id <= 0) {
|
||||
throw new Error('Valid company ID is required');
|
||||
}
|
||||
|
||||
const result = await this.companyRepository.findById(query.id);
|
||||
|
||||
if (!result) {
|
||||
appLogger.warn('Company not found', {
|
||||
companyId: query.id
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
appLogger.info('Company found successfully', {
|
||||
companyId: result.CompanyId,
|
||||
name: result.Name
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error: any) {
|
||||
appLogger.errorEvent('Error getting company by ID', 'company_query', {
|
||||
error: error.message,
|
||||
companyId: query.id
|
||||
});
|
||||
throw new Error(`Failed to get company by ID: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Get company by name
|
||||
async handleGetByName(query: GetCompanyByNameQuery): Promise<CompanyResponseDto | null> {
|
||||
try {
|
||||
appLogger.info('Getting company by name', {
|
||||
companyName: query.name,
|
||||
action: 'get_company_by_name'
|
||||
});
|
||||
|
||||
// Validate name
|
||||
if (!query.name || query.name.trim() === '') {
|
||||
throw new Error('Valid company name is required');
|
||||
}
|
||||
|
||||
const result = await this.companyRepository.findByName(query.name.trim());
|
||||
|
||||
if (!result) {
|
||||
appLogger.warn('Company not found by name', {
|
||||
companyName: query.name
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
appLogger.info('Company found by name', {
|
||||
companyId: result.CompanyId,
|
||||
name: result.Name
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error: any) {
|
||||
appLogger.errorEvent('Error getting company by name', 'company_query', {
|
||||
error: error.message,
|
||||
companyName: query.name
|
||||
});
|
||||
throw new Error(`Failed to get company by name: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Get all companies
|
||||
async handleGetAll(query: GetAllCompaniesQuery): Promise<CompanyResponseDto[]> {
|
||||
try {
|
||||
appLogger.info('Getting all companies', {
|
||||
action: 'get_all_companies'
|
||||
});
|
||||
|
||||
const result = await this.companyRepository.findAll();
|
||||
|
||||
appLogger.info('Companies retrieved successfully', {
|
||||
count: result.length
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error: any) {
|
||||
appLogger.errorEvent('Error getting all companies', 'company_query', {
|
||||
error: error.message
|
||||
});
|
||||
throw new Error(`Failed to get all companies: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Get basic company by ID
|
||||
async handleGetBasicById(query: GetBasicCompanyByIdQuery): Promise<CompanyBasicDto | null> {
|
||||
try {
|
||||
appLogger.info('Getting basic company by ID', {
|
||||
companyId: query.id,
|
||||
action: 'get_basic_company_by_id'
|
||||
});
|
||||
|
||||
// Validate ID
|
||||
if (!query.id || query.id <= 0) {
|
||||
throw new Error('Valid company ID is required');
|
||||
}
|
||||
|
||||
const result = await this.companyRepository.findBasicById(query.id);
|
||||
|
||||
if (!result) {
|
||||
appLogger.warn('Basic company not found', {
|
||||
companyId: query.id
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
appLogger.info('Basic company found successfully', {
|
||||
companyId: result.CompanyId,
|
||||
name: result.Name
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error: any) {
|
||||
appLogger.errorEvent('Error getting basic company by ID', 'company_query', {
|
||||
error: error.message,
|
||||
companyId: query.id
|
||||
});
|
||||
throw new Error(`Failed to get basic company by ID: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Get all basic companies
|
||||
async handleGetAllBasic(query: GetAllBasicCompaniesQuery): Promise<CompanyBasicDto[]> {
|
||||
try {
|
||||
appLogger.info('Getting all basic companies', {
|
||||
action: 'get_all_basic_companies'
|
||||
});
|
||||
|
||||
const result = await this.companyRepository.findAllBasic();
|
||||
|
||||
appLogger.info('Basic companies retrieved successfully', {
|
||||
count: result.length
|
||||
});
|
||||
|
||||
return result;
|
||||
|
||||
} catch (error: any) {
|
||||
appLogger.errorEvent('Error getting all basic companies', 'company_query', {
|
||||
error: error.message
|
||||
});
|
||||
throw new Error(`Failed to get all basic companies: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user