switching to sqlite
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "api",
|
||||
"version": "1.4.0",
|
||||
"version": "2.0.0-alpha",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
|
||||
@@ -38,7 +38,6 @@ import { ConfigService } from '@nestjs/config';
|
||||
}
|
||||
|
||||
async uploadFile(file: Express.Multer.File, containerName: string, logId: string): Promise<string> {
|
||||
console.log(file)
|
||||
this.containerName = containerName;
|
||||
|
||||
const blockBlobClient = await this.getBlobClient(`${logId}/${file.originalname}`);
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
||||
import { Observable, map } from 'rxjs';
|
||||
import { LogEntity } from '../log.entity';
|
||||
|
||||
export class LogInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, handler: CallHandler): Observable<any> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const authHeader = req.headers.authorization;
|
||||
const token = authHeader && authHeader.split(' ')[1];
|
||||
console.log(token)
|
||||
|
||||
if (!token) {
|
||||
return handler.handle().pipe(
|
||||
map((data) => {
|
||||
map((data: LogEntity[]) => {
|
||||
|
||||
if (data.length) {
|
||||
const logs = data.map((log) => {
|
||||
const logs = data.map((log: LogEntity) => {
|
||||
return {
|
||||
partitionKey: log.partitionKey,
|
||||
rowKey: log.rowKey,
|
||||
pilotId: log.pilotId,
|
||||
pilotName: log.pilotName,
|
||||
id: log.id,
|
||||
pilot: {
|
||||
name: log.pilot.name
|
||||
},
|
||||
date: log.date,
|
||||
aircraftMakeModel: log.aircraftMakeModel,
|
||||
routeFrom: log.routeFrom,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { PilotEntity } from "src/pilot/pilot.entity";
|
||||
import { TrackEntity } from "src/track/track.entity";
|
||||
|
||||
export class LogDto {
|
||||
pilotId: string;
|
||||
@@ -26,5 +25,4 @@ export class LogDto {
|
||||
pilotInCommand?: number;
|
||||
notes?: string;
|
||||
pilot?: PilotEntity;
|
||||
tracks?: TrackEntity[];
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@ export class LogService {
|
||||
where: { id: id },
|
||||
relations: ['pilot', 'tracks']
|
||||
});
|
||||
console.log(logEntity);
|
||||
|
||||
return logEntity;
|
||||
}
|
||||
|
||||
async findAll(): Promise<LogEntity[]> {
|
||||
return await this.logRepository.find();
|
||||
return await this.logRepository.find({
|
||||
relations: ['pilot', 'tracks']
|
||||
});
|
||||
}
|
||||
|
||||
async create(logDto: LogDto): Promise<InsertResult> {
|
||||
|
||||
@@ -29,7 +29,7 @@ export class TrackController {
|
||||
) {}
|
||||
|
||||
// @Get('id')
|
||||
// async fin(@Param('id') id: string): Promise<TrackEntity> {
|
||||
// async find(@Param('id') id: string): Promise<TrackEntity> {
|
||||
// try {
|
||||
// return await this.trackService.find(id);
|
||||
// } catch (error) {
|
||||
@@ -39,21 +39,22 @@ export class TrackController {
|
||||
// }
|
||||
// }
|
||||
|
||||
// @Get()
|
||||
// async findAdd(): Promise<TrackEntity[]> {
|
||||
// try {
|
||||
// return await this.trackService.findAll();
|
||||
// } catch (error) {
|
||||
// const customError = error as CustomError;
|
||||
@Get(':logId')
|
||||
async findAll(@Param('logId') logId: string): Promise<TrackEntity[]> {
|
||||
try {
|
||||
return await this.trackService.findAll(logId);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
// throw new HttpException(customError.message, customError.statusCode)
|
||||
// }
|
||||
// }
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Post(':logId/:order')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async create(@Param('logId') logId: string, @Param('order') order: number, @UploadedFile() file: Express.Multer.File): Promise<InsertResult> {
|
||||
try {
|
||||
console.log(file)
|
||||
return await this.trackService.create(logId, order, file);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
@@ -62,21 +63,12 @@ export class TrackController {
|
||||
}
|
||||
}
|
||||
|
||||
// @Put(':id')
|
||||
// async update(@Param('id') id: string, @Body() trackDto: TrackDto): Promise<UpdateResult> {
|
||||
// try {
|
||||
// return await this.trackService.update(id, trackDto);
|
||||
// } catch (error) {
|
||||
// const customError = error as CustomError;
|
||||
|
||||
// throw new HttpException(customError.message, customError.statusCode)
|
||||
// }
|
||||
// }
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: string): Promise<DeleteResult> {
|
||||
async delete(@Param('id') id: string, @Query('fileName') fileName: string, @Query('logId') logId: string): Promise<DeleteResult> {
|
||||
try {
|
||||
return await this.trackService.delete(id);
|
||||
|
||||
return await this.trackService.delete(id, logId, fileName);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
@@ -84,37 +76,10 @@ export class TrackController {
|
||||
}
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Post(':partitionKey/:rowKey/track')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
async createTrack(@Param('rowKey') rowKey: string, @UploadedFile() file: Express.Multer.File) {
|
||||
@Get(':logId/:fileName')
|
||||
async downloadTrack(@Param('logId') logId: string, @Param('fileName') fileName: string): Promise<string> {
|
||||
try {
|
||||
const containerName = 'tracks';
|
||||
const url = await this.fileService.uploadFile(file, containerName, rowKey);
|
||||
|
||||
return { url }
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
throw new HttpException(customError.message, customError.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
@Get(':partitionKey/:rowKey/track')
|
||||
async downloadTrack(@Param('rowKey') rowKey: string, @Query('fileName') fileName: string): Promise<string> {
|
||||
const containerName = 'tracks';
|
||||
const downloadedFile: string = await this.fileService.downloadFile(containerName, rowKey, fileName)
|
||||
|
||||
return downloadedFile;
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Delete(':partitionKey/:rowKey/track')
|
||||
async deleteTrack(@Param('rowKey') rowKey: string, @Query('fileName') fileName: string): Promise<void> {
|
||||
try {
|
||||
const containerName = 'tracks';
|
||||
|
||||
return await this.fileService.deleteFile(containerName, rowKey, fileName)
|
||||
return await this.trackService.downloadTrackFile(logId, fileName);
|
||||
} catch (error) {
|
||||
const customError = error as CustomError;
|
||||
|
||||
@@ -127,7 +92,3 @@ export class TrackController {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -10,32 +10,51 @@ import { FileService } from "src/file/file.service";
|
||||
|
||||
@Injectable()
|
||||
export class TrackService {
|
||||
private readonly containerName: string = 'tracks'
|
||||
|
||||
constructor(
|
||||
@InjectRepository(TrackEntity) private readonly trackRepository: Repository<TrackEntity>,
|
||||
private readonly fileService: FileService,
|
||||
private readonly logService: LogService
|
||||
) {}
|
||||
|
||||
// async find(id: string): Promise<TrackEntity> {
|
||||
// return await this.trackRepository.findOneBy({ id });
|
||||
// }
|
||||
async find(id: string): Promise<TrackEntity> {
|
||||
try {
|
||||
return await this.trackRepository.findOneBy({ id });
|
||||
} catch (error) {
|
||||
throw new CustomError('Track not found', 'Not found', 404)
|
||||
}
|
||||
}
|
||||
|
||||
// async findAll(logId: string): Promise<TrackEntity[]> {
|
||||
// try {
|
||||
// await this.trackRepository.findOneBy({ })
|
||||
// } catch (error) {
|
||||
async findAll(logId: string): Promise<TrackEntity[]> {
|
||||
try {
|
||||
const logEntity: LogEntity = await this.logService.find(logId);
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
if (logEntity) {
|
||||
console.log(logEntity)
|
||||
const tracks = await this.trackRepository.find({
|
||||
where: { log:
|
||||
{
|
||||
id: logEntity.id
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
console.log(tracks)
|
||||
return tracks;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw new CustomError('Tracks not found', 'Not found', 404);
|
||||
}
|
||||
}
|
||||
|
||||
async create(logId: string, order: number, file: Express.Multer.File): Promise<InsertResult> {
|
||||
try {
|
||||
const logEntity: LogEntity = await this.logService.find(logId);
|
||||
|
||||
if (logEntity) {
|
||||
const containerName = 'tracks';
|
||||
const url = await this.fileService.uploadFile(file, containerName, logId);
|
||||
const url = await this.fileService.uploadFile(file, this.containerName, logId);
|
||||
const track = this.trackRepository.create({
|
||||
log: logEntity,
|
||||
order: order,
|
||||
@@ -52,37 +71,31 @@ export class TrackService {
|
||||
}
|
||||
}
|
||||
|
||||
// async update(id: string, track: TrackDto): Promise<UpdateResult> {
|
||||
// return await this.trackRepository.update(id, track);
|
||||
// }
|
||||
|
||||
async delete(id: string): Promise<DeleteResult> {
|
||||
async update(id: string, track: TrackDto): Promise<UpdateResult> {
|
||||
try {
|
||||
|
||||
} catch (error) {
|
||||
|
||||
return await this.trackRepository.update(id, track);
|
||||
} catch(error) {
|
||||
throw error
|
||||
}
|
||||
return await this.trackRepository.delete({ id });
|
||||
}
|
||||
|
||||
// async createTrack()
|
||||
async delete(id: string, logId: string, fileName: string): Promise<DeleteResult> {
|
||||
try {
|
||||
await this.fileService.deleteFile(this.containerName, logId, fileName);
|
||||
|
||||
// async downloadTrack(): Promise<string> {
|
||||
// const containerName = 'tracks';
|
||||
// const downloadedFile: string = await this.fileService.downloadFile(containerName, rowKey, fileName);
|
||||
return await this.trackRepository.delete({ id });
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// return downloadedFile;
|
||||
// }
|
||||
async downloadTrackFile(logId: string, fileName: string): Promise<string> {
|
||||
try {
|
||||
const downloadedFile: string = await this.fileService.downloadFile(this.containerName, logId, fileName);
|
||||
|
||||
// async deleteTrack(): Promsie<void> {
|
||||
// try {
|
||||
// const containerName = 'tracks';
|
||||
|
||||
// return await this.fileService.deleteFile(containerName, rowKey, fileName)
|
||||
// } catch (error) {
|
||||
// const customError = error as CustomError;
|
||||
|
||||
// throw customError;
|
||||
// }
|
||||
// }
|
||||
return downloadedFile;
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user