Files
noahspan-flying/api/src/log/log.service.ts
noahspannbauer f98a2ab127 96 switch from azure table storage to sqlite (#97)
* adding typeorm to api

* switching to sqlite

* switching to sqlite

* switching to sqlite

* migrating to sqlite

* updating terraform

* updating infrastructure
2025-11-23 10:42:31 -06:00

64 lines
1.8 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { LogEntity } from './log.entity';
import { DeleteResult, InsertResult, Repository, UpdateResult } from 'typeorm';
import { LogDto } from './log.dto';
import { PilotService } from 'src/pilot/pilot.service';
import { PilotEntity } from 'src/pilot/pilot.entity';
import { CustomError } from 'src/error/customError';
import { FileService } from 'src/file/file.service';
@Injectable()
export class LogService {
constructor(
@InjectRepository(LogEntity) private readonly logRepository: Repository<LogEntity>,
private readonly fileService: FileService,
private readonly pilotService: PilotService,
) {}
async find(id: string): Promise<LogEntity> {
const logEntity: LogEntity = await this.logRepository.findOne({
where: { id: id },
relations: ['pilot', 'tracks']
});
return logEntity;
}
async findAll(): Promise<LogEntity[]> {
return await this.logRepository.find({
relations: ['pilot', 'tracks']
});
}
async create(logDto: LogDto): Promise<InsertResult> {
try{
const pilotEntity: PilotEntity = await this.pilotService.find(logDto.pilotId);
if (pilotEntity) {
const { pilotId, ...newLogDto } = logDto;
const log = this.logRepository.create({
...newLogDto,
pilot: pilotEntity
})
return this.logRepository.insert(log);
} else {
throw new CustomError('Pilot not found', 'Not found', 404);
}
} catch (error) {
throw error
}
}
async update(id: string, log: LogDto): Promise<UpdateResult> {
return await this.logRepository.update(id, log);
}
async delete(id: string): Promise<DeleteResult> {
await this.fileService.deleteFolder('tracks', id);
return await this.logRepository.delete({ id });
}
}